Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

3.3: Variables Revisited

( \newcommand{\kernel}{\mathrm{null}\,}\)

Learning Objectives

By the end of this section you should be able to

  • Distinguish between variables, objects, and references.
  • Draw memory diagrams with integers, floats, and strings.

References to objects

In Python, every variable refers to an object. The assignment statement message = "Hello" makes the variable message refer to the object "Hello". Multiple variables may refer to the same object. Ex: greeting = message makes greeting refer to the same object as message. A memory diagram shows the relationship between variables and objects.

Checkpoint: Example memory diagram
Concepts in Practice: Variables and objects
1.
How many assignment statements are in the above animation?
  1. 2
  • 3
  • 4
  • 2.
    Which of the following best describes the objects assigned?
    1. two float objects
    2. two int objects, one float object
    3. one int object, one float object
    3.
    What symbol is used to show a variable's current value?
    1. an arrow
    2. a small black box
    3. a rounded box
    Exploring further

    Python Tutor is a free online tool for visualizing code execution. A user can enter any Python code, click Visualize Execution, and then click the Next button to run the code one line at a time. Here is the rating and score example from the animation above.

    Python Tutor is also useful for drawing memory diagrams similar to the ones in this book. Before clicking Visualize Execution, change the middle option from "inline primitives, don't nest objects [default]" to "render all objects on the heap (Python/Java)" as shown in the following screenshot:

    Screenshot of Python tutor settings, with the middle dropdown list open to the last setting.
    Figure 3.2

    Properties of objects

    Every object has an identity, a type, and a value:

    • An object's identity is a unique integer associated with the object. Generally, this integer refers to the memory location where the object is stored. Once created, an object's identity never changes. The built-in id() function returns the object's identity.
    • An object's type determines the possible values and operations of an object. Ex: Integers and floats can be "divided" using the / operator, but strings cannot. The built-in type() function returns the object's type.
    • An object's value represents the current state of the object. Many objects, such as numbers and strings, cannot be modified once created. Some objects, such as lists (introduced later), are designed to be modified.
    Checkpoint: Identity, type, and value
    Concepts in Practice: id() and type()
    4.
    Which value might be returned by id(rating)?
    1. 9793344
  • <class 'float'>
  • 10.0
  • 5.
    Which value might be returned by type(rating)?
    1. 10.0
    2. "float"
    3. <class 'float'>
    6.
    What expression returns the value of an object?
    1. value(rating)
    2. rating
    3. "rating"
    Exploring further

    As shown in a memory diagram, variables and objects are two separate ideas. Calling a function like id() or type() returns information about an object, not a variable. In fact, a variable doesn't have an identity or a type, as shown in this example:

        >>> rating = 10  # Integer object somewhere in memory.
        >>> type(rating)
        <class 'int'>
        >>> id(rating)
        9793344
        >>> rating = "ten"  # String object somewhere else in memory.
        >>> type(rating)
        <class 'str'>
        >>> id(rating)
        140690967388272

    One might incorrectly think that the rating variable's type or identity changes. However, the only thing that changes is which object the rating variable refers to.

    Try It: Three variables
    1. Draw a memory diagram for the following code: a = 1 b = 2 c = b b = a a = c
    2. Run the code on Python Tutor to check your answer.
    3. Based on your diagram, answer these questions:
      • What is the final value of a, b, and c?
      • How many integer objects are created?
    Try It: Different types
    1. Draw a memory diagram for the following code: name = "Chocolate" length = len(name) price = 1.99 lower = min(length, price) product = name name = name * 2
    2. Run the code on Python Tutor to check your answer.
    3. Based on your diagram, answer these questions:
      • What is the type and value of each object?
      • Which object does each variable reference?

    This page titled 3.3: Variables Revisited is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?