1.2 Variables & Python's Object Memory Model
Understand the #1 mental model that separates confused beginners from confident Python developers: variables are sticky-note labels pointing to objects in memory, not fixed boxes.
1. The Everyday Analogy: Name Tags, Not Storage Boxes
In many older languages (like C or Java), textbooks tell you to imagine a variable as a fixed cardboard box with a label painted on the outside (int age = 25;).
In Python, that box analogy will confuse you later when we reach Lists and Functions! Instead, use the true Python mental model:
Every value in Python is an Object floating in memory (RAM), and a variable is just a Sticky-Note Name Tag pointing to that object.
When you write age = 25, Python:
- Creates an integer object
25in RAM (which knows its own typeintand its own value25). - Sticks the name tag
ageonto that object.
Because the type lives on the object (not on the sticky note), you can peel the sticky note age off 25 and stick it onto "twenty-five" at any time! This is called Dynamic Typing.
score = 100 # score points to an int object (100)
print(type(score)) # <class 'int'>
score = "A+" # Now score points to a str object ("A+")
print(type(score)) # <class 'str'>
2. Peeking Inside Memory with id() and type()
Python gives you two built-in X-ray tools to inspect any variable:
type(x)tells you what kind of objectxis currently pointing to (int,float,str,bool, etc.).id(x)returns the unique memory identity (address in CPython) of that object!
a = 500
b = a # Both 'a' and 'b' point to the EXACT SAME object in RAM!
print(id(a)) # e.g., 140234892104112
print(id(b)) # 140234892104112 (Identical!)
print(a is b) # True
3. What Happens When You Reassign a Variable? (Aliasing & Garbage Collection)
Look closely at this classic 3-line puzzle that trips up freshers:
x = 10
y = x
x = 20
print(y) # What does this print? 10 or 20?
It prints 10! Why? Because x = 20 does not overwrite the object 10. It simply peels the sticky note x off 10 and sticks it onto a brand-new object 20. Meanwhile, y is still stuck to 10!
And what if later you also move y away (y = 99)? Now zero sticky notes point to 10 (refcount == 0). Python's built-in Garbage Collector automatically sweeps that unreachable object out of RAM for you!
4. Interactive Trace: Watch Name Tags Move in Real Time
Step through this code line by line to see how variables are created, shared, reassigned, and swapped in Python:
5. Variable Naming Rules & Clean Conventions (PEP 8)
Strict Rules (Breaking These Causes SyntaxError)
- Must start with a letter (
a–z,A–Z) or an underscore (_). - Can contain letters, digits (
0–9), and underscores — never spaces or hyphens (-). - Cannot be a reserved Python keyword (
if,for,class,True,None,return, etc.).
| Name | Valid? | Why |
|---|---|---|
user_age | ✓ Yes (Best!) | Clean snake_case — standard Python convention |
_internal_id | ✓ Yes | Leading underscore signals "internal use" |
MAX_RETRIES | ✓ Yes | ALL_CAPS signals a constant (do not change) |
2nd_place | ✗ No | Cannot start with a digit |
first-name | ✗ No | Hyphen - is interpreted as subtraction! |
class | ✗ No | Reserved Python keyword |
Beginner Trap: Never Name Variables After Built-Ins!
Python lets you overwrite built-in names like list, str, sum, or print — and it will break your code later!
# ✗ Dangerous trap!
str = "Hello"
# Now calling str(42) crashes with: TypeError: 'str' object is not callable!
# ✓ Safe alternative
greeting = "Hello"
6. Multiple Assignment Shortcuts
Python lets you assign multiple variables cleanly in one line:
# Assign different values at once (unpacking)
name, age, is_student = "Meera", 22, True
# Assign the same immutable value to multiple labels
x = y = z = 0
print(name, age, is_student) # Meera 22 True
print(x, y, z) # 0 0 0
Quick Quiz: Module 1.2 — Variables & Object Memory
a = 100 b = a a = 250 print(b)
Keep Exploring: Related Lessons & Tools
All Courses →Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.
Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.