Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo
Introduction to Python Programming

3.3 Variables revisited

Introduction to Python Programming3.3 Variables revisited

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
  2. 3
  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
  2. <class 'float'>
  3. 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?
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.