Performing Operations on Variables
Use print(variable) to display the value stored in a variable and see it on the console.
Seeing Stored Values
When a program does not behave as expected, one of the first questions to ask is: what value does this variable hold right now? Python's print() function gives you a direct view of a variable's value. If n holds the integer 17, print(n) displays 17 on the console. If pi holds 3.141592653589793, print(pi) displays that decimal value.
17
3.141592653589793Value and Type
A variable's value and its type describe different aspects of what the variable holds. The value is the data you can display, such as 17, 3.141592653589793, or the text stored in message. The type classifies that value. Python uses classifications such as int for an integer, str for a string, and float for a floating-point number.
The type of a variable determines what operations you can perform on it and how Python interprets it.
Two values can look similar while having different types. The integer 5 and the text '5' are fundamentally different: one is an int and the other is a str. This distinction matters when a program performs operations. Two integers can be added together, but an integer and a string cannot be added in the same way.
Inspecting Types
Use type(variable) to check the data type of a variable. The type() function takes a variable as input and returns the type classification of the value it holds. This lets you confirm whether a value is an int, str, float, or another type supported by Python.
<class 'str'>
<class 'int'>
<class 'float'>The type() call produces the type information. In this example, print() is used around each type() call so that the returned type information is displayed on the console. The angle brackets and the word class are Python's formal notation for identifying a type.
Display or Inspect
| Operation | What it examines | What you see |
|---|---|---|
| print(variable) | The value held by the variable | The value, such as 17 |
| type(variable) | The type of the value held by the variable | A type classification, such as int |
| print(type(variable)) | The type returned by type(variable) | The type notation, such as <class 'int'> |
Suppose a variable contains something that looks like the number 5. print(variable) helps you see the value. type(variable), often displayed with print(type(variable)), helps you determine whether Python treats that value as an integer or as text. The first operation checks what the variable contains; the second checks how Python classifies what it contains.
Checking a Similar-Looking Value
Determine what each inspection tells you about a variable containing the integer 5 and a variable containing the text '5'.
Display the values: Printing both variables shows 5 and 5. Their displayed values look similar.
Inspect the types: Checking the types distinguishes the values: the first is an int and the second is a str.
Use the distinction: The type information explains why the two values do not behave the same way in operations.
The displayed value alone may not reveal the important difference. Inspecting the type shows whether Python treats the value as an integer or as a string.
5
5
<class 'int'>
<class 'str'>Debugging Checks
Use both operations when debugging a program. First, display a variable's value to verify what the program currently holds. Then inspect its type when you need to understand which operations Python can perform on that value or why the program is behaving unexpectedly.
Using print(variable) when you need to know the type
The displayed value does not identify whether Python classifies it as int or str.
Fix:
Use type(variable), and use print(type(variable)) when you want the type information displayed on the console.Assuming similar-looking values have the same type
The first is an integer and the second is a string, so Python does not treat them the same way in operations.
Fix:
Inspect the type whenever the intended operation depends on whether a value is numeric or textual.Ignoring type information during debugging
The value may look reasonable even when its type is not the type you expected.
Fix:
Display the value and check its type as separate verification steps.
Practice Check
A variable named item contains the text '5'. Write one operation that displays its value and one operation that checks its type. Then state what kind of difference you would expect between this variable and one containing the integer 5.
Hints
- Use print(variable) to display a stored value.
- Use type(variable) to inspect the classification of that value.
- Compare str with int.
What do you think happens?
If a variable contains the integer 17, what does print(type(variable)) display?
Reveal answer
Answer: <class 'int'>
The variable's value is 17, and Python classifies that value as an integer, or int.
Key Takeaways
- Use print(variable) to display the value held by a variable on the console.
- Use type(variable) to inspect the type classification of the value.
- A variable's type affects how Python interprets its value and what operations can be performed on it.
- Values that look similar, such as 5 and '5', can have different types and different behavior.
- Displaying values and checking types are fundamental tools for debugging and verifying a program.
Key Takeaways
- print(variable) displays the value stored in a variable.
- type(variable) identifies the type of that value, such as int, str, or float.
- The value and its type provide different information about a variable.
- Checking both value and type helps verify program behavior and debug unexpected results.