Login
📚 Introduction to Python Programming
Chapters ▾
⇩ Download ▾

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

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.