Python · Module 01: Getting Started & How Python ThinksTutorial 2 of 30
All Tutorials·~1.5 hrs

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.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

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:

  1. Creates an integer object 25 in RAM (which knows its own type int and its own value 25).
  2. Sticks the name tag age onto that object.
Visual: Variables in Python Are Sticky-Note Labels Pointing to Objects in RAM
✗ Common Myth (C / Java Box)box named "age"25Makes aliasing & mutabilityconfusing later in Python!✓ How Python Actually Works (Name Tag → Heap Object)Name TagageHeap Object · id: 0x7f9a10value = 25type: int · refcount: 1The type lives on the OBJECT (25), not on the variable name (age)!

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.

PYTHON
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 object x is currently pointing to (int, float, str, bool, etc.).
  • id(x) returns the unique memory identity (address in CPython) of that object!
PYTHON
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:

PYTHON
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!

Visual: What Happens in RAM During `x = 10`, `y = x`, then `x = 20`
1) x = 10 ; y = xxy10refs: 2Both tags share one object!2) x = 20xy20refs: 110refs: 1y STILL equals 10!3) y = 99 (GC!)10refs: 0 🗑Garbage CollectorObjects with 0 name tagsare freed automatically!

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:

Tracing Variable Reassignment & Pythonic Swap
Interactive Line-by-Line Execution Trace · Step 1 of 5
1→x = 10
2y = x
3x = 20
4x, y = y, x
5print(x, y)
Variables in Memory
x10int
Console Output
(nothing printed yet)
What just happened: Python creates integer object 10 in RAM and attaches the label x to it.

5. Variable Naming Rules & Clean Conventions (PEP 8)

Strict Rules (Breaking These Causes SyntaxError)

  1. Must start with a letter (a–z, A–Z) or an underscore (_).
  2. Can contain letters, digits (0–9), and underscores — never spaces or hyphens (-).
  3. Cannot be a reserved Python keyword (if, for, class, True, None, return, etc.).
NameValid?Why
user_age✓ Yes (Best!)Clean snake_case — standard Python convention
_internal_id✓ YesLeading underscore signals "internal use"
MAX_RETRIES✓ YesALL_CAPS signals a constant (do not change)
2nd_place✗ NoCannot start with a digit
first-name✗ NoHyphen - is interpreted as subtraction!
class✗ NoReserved 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!

PYTHON
# ✗ 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:

PYTHON
# 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

Q1.What does this snippet print?
a = 100
b = a
a = 250
print(b)
Q2.Which of the following is a VALID Python variable name following PEP 8 style?
Q3.In Python, where is the data type (like int or str) stored?

Keep Exploring: Related Lessons & Tools

All Courses →