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.
Adapted from Introduction to Python Programming by OpenStax (openstax.org), licensed under CC BY-NC-SA 4.0. Changes were made. License: CC-BY-NC-SA-4.0.
These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.