Expressions and Operations
Values are not all the same. Python needs to know whether a value is a whole number, text, or a decimal number so it can apply the appropriate rules.
Values at the Starting Line
A value is one of the basic things a program works with, such as a letter or a number. In a simple Python program, the values 1, 2, and 'Hello, World!' are already enough to show the raw material that programs can manipulate, store, and display. Before working with larger programs, learn to recognize what a value is and how Python classifies it.
Values are not all the same. Python needs to know whether a value is a whole number, text, or a decimal number so it can apply the appropriate rules.
Three Ways to Represent Data
Python organizes values into types. The three types in this lesson are integers, strings, and floats. An integer is a whole number, including negative whole numbers and zero. A string is text enclosed in quotation marks. A float is a decimal number, identified by the presence of a decimal point.
Quotation Marks as Boundaries
Quotation marks tell Python where a string begins and ends. They also tell Python that the enclosed content is text, not a variable or a number. This is why '42' and 42 are different values: the first is text because it is enclosed in quotation marks, while the second is an integer.
Checking a Value with type()
When you are unsure about a value's type, use the type() function. It takes a value as input and returns the type that value belongs to. For example, type(42) identifies an integer, type('42') identifies a string, and type(42.0) identifies a float.
Classifying three values
Identify the type of 17, '17', and 17.0, then verify each classification with type().
- 1First value
17 has no decimal point and no quotation marks, so it is an integer. Python identifies it as int.
- 2Second value
'17' is enclosed in quotation marks, so it is a string. Python identifies it as str.
- 3Third value
17.0 contains a decimal point, so it is a float. Python identifies it as float.
- 4Verification
Apply
type()to each value:type(17),type('17'), andtype(17.0). The function verifies the three classifications.
17 is an integer, '17' is a string, and 17.0 is a float.
Reading Values Correctly
Common mistake Treating '42' as the integer 42
What it looks like'42' has quotation marks, while 42 does not.
Why it failsQuotation marks make the first value a string. Python treats strings and integers as different types.
Do this insteadCheck whether quotation marks are present before deciding that a value is a number.
Common mistake Treating 5.0 as the integer 5
What it looks like5.0 and 5 look similar, but only 5 has no decimal point.
Why it failsA decimal point makes 5.0 a float, even when its decimal part is zero.
Do this insteadLook for the decimal point. No decimal point means integer; a decimal point means float.
Common mistake Forgetting quotation marks around text
What it looks likeWriting Hello instead of 'Hello'.
Why it failsQuotation marks tell Python that the content is text rather than a number or variable.
Do this insteadEnclose every string in quotation marks.
Common mistake Guessing a type instead of checking it
What it looks likeA value may look like a number but still be text, as in '42'.
Why it failsVisual similarity does not make two values the same type.
Do this insteadUse type() whenever you are uncertain.
When reviewing a value, use a three-part check: look for quotation marks, look for a decimal point, and use type() to verify your conclusion. This habit helps you find incorrectly represented values before they cause unexpected program behavior.
Type Identification Practice
For each value, decide whether it is an integer, string, or float. Then verify your answers with type(): -5, 'abc123', 0.5, 0, 'Hello!', and 99.99.
Hints
- A whole number with no decimal point is an integer.
- Quotation marks identify a string.
- A decimal point identifies a float.
A Reliable Foundation
- 1A value is one of the basic things a program works with, such as a letter or a number.
- 2Integers are whole numbers, strings are quoted text, and floats are decimal numbers.
- 3Quotation marks distinguish text from numbers and other kinds of values.
- 4The values 42, '42', and 42.0 look related but belong to int, str, and float respectively.
- 5The
type()function verifies the type of a value when you are uncertain.
Key Takeaways
- Values are the basic pieces of data that programs work with.
- Python distinguishes integers, strings, and floats.
- Quotation marks identify strings and prevent text from being treated as a number or variable.
- A decimal point makes a value a float, even when the decimal part is zero.
- Use
type()to verify the type of any value.