Concepts / Performing Operations on Variables

Performing Operations on Variables

Use print(variable) to display the value stored in a variable and see it on the console.

  • Programming

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.

python
Output
17
3.141592653589793
valuedisplaysn17print(n)Console17
How does the value move from a variable to the console when print(variable) runs?

Value 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.

holdsclassified asnvariable17int
How are the value held by a variable and the type of that value connected?

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.

python
Output
<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

OperationWhat it examinesWhat you see
print(variable)The value held by the variableThe value, such as 17
type(variable)The type of the value held by the variableA 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.

python
Output
5
5
<class 'int'>
<class 'str'>
displaysreturnsprint(number)type(number)5int
What is the difference between using print(variable) to display a value and using type(variable) to inspect its type?

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

EASY

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?

  • 17
  • <class 'int'>
  • <class 'str'>
Reveal answer

Answer: <class 'int'>

The variable's value is 17, and Python classifies that value as an integer, or int.

Key Takeaways

  1. Use print(variable) to display the value held by a variable on the console.
  2. Use type(variable) to inspect the type classification of the value.
  3. A variable's type affects how Python interprets its value and what operations can be performed on it.
  4. Values that look similar, such as 5 and '5', can have different types and different behavior.
  5. 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.