MOD-03 · Sequence #3 · Python Basics

Python Variables & Dynamic Binding

In Python, variables are not memory containers holding values; they are name tags (references) bound to heap objects. Understanding the difference between variable rebinding and object mutation is the single most important concept in undergraduate Python.

Live Interactive Code Snippet (Python 3.12 CPython WASM) Open in Practice Mode →
x, y, z = "Orange", "Banana", "Cherry"
print(x, y, z)

Core Principles & Mental Models

1. Names vs. Boxes: The Python Object Model

Names Bound to Heap Entities

  • In languages like C, int x = 5 creates an 8-byte box labeled x where 5 resides.
  • In Python, x = 5 creates an integer object 5 in heap memory and binds the identifier "x" to point to it.
  • When you do y = x, Python does NOT copy the object 5. It simply binds a second identifier "y" to the identical object in memory.
  • You can inspect object identity in RAM using the built-in id() function.
x = [1, 2, 3]
y = x  # y is an alias referencing the exact same list

print("id(x) == id(y):", id(x) == id(y))
y.append(4)
print("x after y.append:", x)  # x is mutated!
💡 Key Takeaway: Assignment (=) in Python never clones objects; it binds a name to an object reference.

2. Multiple Assignment & Tuple Unpacking

Idiomatic Value Swapping

  • Python allows multiple assignments on a single line: a, b = 1, 2.
  • This enables swapping variables without an auxiliary temporary variable: a, b = b, a.
  • The right-hand side is evaluated as a tuple first, then unpacked into the left-hand identifiers.
a = 10
b = 20
a, b = b, a  # Elegant atomic swap
print(f"a: {a}, b: {b}")
💡 Key Takeaway: The idiom a, b = b, a creates a temporary tuple (b, a) in memory and unpacks it cleanly.

Interactive Walkthrough Examples

Variable Binding & Memory Inspection (Basic)

Inspecting memory pointers using id() to verify shared object identity.

a = 1000
b = a
print("Initial IDs identical?", id(a) == id(b))

# Rebinding 'a' to a new integer
a = a + 1
print("After rebinding a:", a, "b remains:", b)
print("IDs still identical?", id(a) == id(b))

Explanation: Integers are immutable. Doing a = a + 1 creates a new integer 1001 and rebinds name a. Name b still points to 1000.

Common Misconceptions & Exam Traps

Common Pitfall: Assuming Assignment Creates a Copy of Lists

Assignment (=) binds names to the same reference. Mutating backup mutates original.

Buggy Code
original = [10, 20, 30]
backup = original  # NOT a copy! Just another reference.
backup.clear()
print("Original list:", original)  # Prints []!
Correct Solution
original = [10, 20, 30]
backup = original.copy()  # or original[:]
backup.clear()
print("Original list preserved:", original)  # [10, 20, 30]

Exam Trap Note: A classic exam question asks: "What is printed after b = a; b.append(4)?" Students mistakenly think b is an independent copy.

Conceptual Knowledge Self-Check Quiz

Q1: What does the assignment operation `a = b` do in Python?

  1. Allocates a new memory block and duplicates the bytes of b
  2. Binds the name a to the same object in memory that b references
  3. Deletes object b
  4. Creates a pointer to pointer structure

Python assignment is name-binding. It points identifier a to the existing object reference.

Algorithmic Practice Challenges

Idiomatic Best Practices

  • ✅ Use descriptive, snake_case variable names conforming to PEP 8.
  • ✅ Always use .copy() or slice [:] when you need an independent clone of a mutable list.

All 26 Curriculum Modules