You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

3.3 Variables revisited

2 min readjune 24, 2024

Variables in Python are like labels that point to objects in memory. They act as references, allowing multiple variables to refer to the same . Understanding this relationship is crucial for efficient programming and memory management.

Objects are instances of data types with unique identities, types, and values. Memory diagrams help visualize how variables reference objects, while key properties like , , and define an object's characteristics. This knowledge forms the foundation for working with Python's data structures.

Variables and Objects

Variables and objects relationship

Top images from around the web for Variables and objects relationship
Top images from around the web for Variables and objects relationship
  • Variables serve as names that reference objects stored in memory
    • Act as labels or pointers to objects (x = 42)
    • Multiple variables can reference the same object (x = y = 10)
  • Objects are instances of specific data types
    • Integers, floats, strings, lists (42, 3.14, "hello", [1, 2, 3])
    • Have unique identity, type, and value

Memory diagrams for object references

  • Visualize the relationship between variables and objects
  • Objects represented by boxes, variables as arrows pointing to objects
  • assignment creates an arrow from to object
  • Multiple variables referencing the same object shown as multiple arrows
  • Unreferenced objects become eligible for
    • Automatic memory management process in Python
    • Uses to track object usage

Key properties of objects

  • Identity: Unique identifier of an object in memory
    • Obtained using
      [id()](https://www.fiveableKeyTerm:id())
      function (
      id(x)
      )
    • Objects with the same identity are the same object
  • Type: of an object
    • Obtained using
      [type()](https://www.fiveableKeyTerm:type())
      function (
      type(x)
      )
    • Determines available operations and methods (integer arithmetic, string methods)
  • Value: Actual data stored within an object
    • Numbers, strings, lists, etc. (42, "hello", [1, 2, 3])
    • Objects with the same value may have different identities
  • : Some objects cannot be changed after creation (e.g., strings, tuples)

Memory and Scope

  • : Python manages memory dynamically for object creation
  • : Mapping between names and objects in a given context
  • : Determines the visibility and accessibility of variables in different parts of the code
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary