Excellent choice.
DAY 3 is the day where most Python confusion permanently disappears.

If you truly absorb today, you’ll:

  • Instantly predict outputs
  • Never fear is vs ==
  • Understand mutability at CPython memory level
  • Sound like a senior engineer in interviews

🧠 DAY 3 — Variables, Names, References & Object Identity

(The most misunderstood topic in Python)


🔑 CORE TRUTH (MEMORIZE THIS)

Python variables do NOT store values.
They store REFERENCES (names) to objects.

This single sentence explains 90% of Python behavior.


1️⃣ Variables Are NOT Boxes (Kill This Mental Model)

❌ Wrong (C / Java mindset)

a ──▶ [ 10 ]

✅ Correct (Python)

a ──▶ (object: 10)
  • a is just a label
  • 10 is an object living in memory

2️⃣ Everything in Python Is an Object

Yes — everything.

x = 10

What exists in memory:

  • An int object
  • A name x pointing to it
print(type(10))       # <class 'int'>
print(type(type(10))) # <class 'type'>

Even types are objects.


3️⃣ Object Identity vs Equality (INTERVIEW FAVORITE)

ConceptMeaningOperator
IdentitySame object in memoryis
EqualitySame value==
a = [1, 2]
b = [1, 2]

a == b   # True (same value)
a is b   # False (different objects)

4️⃣ id() — Your Window Into Memory

a = 10
b = 10

print(id(a), id(b))
  • id() = memory identity (address-like)
  • Same id ⇒ same object

5️⃣ Why a is b Sometimes Surprises You

a = 256
b = 256
print(a is b)   # True

But:

a = 257
b = 257
print(a is b)   # False

WHY? (CPython internals)

  • CPython pre-allocates integers from -5 to 256
  • These objects are cached & reused

This is called integer interning

⚠️ Implementation detail, not language guarantee


6️⃣ Mutability vs Immutability (FOUNDATIONAL)

TypeMutable?
int
float
str
tuple
list
dict
set

Immutable = object cannot change

Mutable = object can change in-place


7️⃣ Mutation vs Rebinding (CRITICAL DIFFERENCE)

a = [1, 2]
b = a

Memory:

a ──▶ [1, 2]
b ──▶ [1, 2]

Now:

a.append(3)

Result:

a ──▶ [1, 2, 3]
b ──▶ [1, 2, 3]

Same object mutated.


Now rebinding:

a = a + [4]

What happens?

  • New list created
  • a points to new object
  • b still points to old object

8️⃣ Why Strings Behave “Weirdly”

s = "hello"
s += "world"

This does NOT mutate the string.

Instead:

  1. New string created
  2. s is rebound

Why?

  • Strings are immutable
  • Mutation would break hashing & safety

9️⃣ Pass-by-Object-Reference (NOT pass-by-value)

Python uses:

Call by sharing (object reference passing)

def f(x):
    x.append(100)

lst = [1, 2]
f(lst)

print(lst)  # [1, 2, 100]

Because:

  • x points to same object as lst

But this:

def g(x):
    x = x + [100]

lst = [1, 2]
g(lst)

print(lst)  # [1, 2]

Because:

  • x is rebound to a new object

10️⃣ Python Object Internals (CPython Level)

Every object contains:

  • Reference count
  • Type pointer
  • Value

Conceptually:

PyObject
 ├── ob_refcnt
 ├── ob_type
 └── data

This is why:

  • Assignment is cheap
  • Mutation is dangerous
  • Copies matter

11️⃣ Visual Memory Model (IMPORTANT)

Image
Image
Image
Image

🔥 INTERVIEW TRAPS (DAY 3)

Q1

a = [1, 2, 3]
b = a
a = a + [4]

print(b)

✔ Answer: [1, 2, 3]
(New object created)


Q2

a = [1, 2, 3]
b = a
a += [4]

print(b)

✔ Answer: [1, 2, 3, 4]
(In-place mutation)


Q3

x = 10
y = 10
x += 1

print(y)

✔ Answer: 10
(int is immutable)


Q4

print([] is [])

✔ Answer: False
(New object every time)


🧠 DAY 3 MENTAL CHECKLIST

Before predicting output, ask:

  1. Is the object mutable?
  2. Is this mutation or rebinding?
  3. Are two names pointing to same object?
  4. Is CPython caching involved?

📝 DAY 3 ASSIGNMENT (DO NOT SKIP)

1️⃣ Predict output (NO RUNNING)

a = [1, 2]
b = a
c = b[:]

a.append(100)

print(a, b, c)

2️⃣ Explain in words:

  • Why Python doesn’t have pointers
  • Why immutability helps hashing

3️⃣ Disassemble thinking:

Why is this dangerous?

def f(x=[]):
    x.append(1)
    return x

(Hint: object lifetime)


🔜 DAY 4 PREVIEW (🔥🔥🔥)

DAY 4 — Python Memory Management & Garbage Collection

You will learn:

  • Reference counting
  • Cyclic garbage
  • Why memory “leaks” happen
  • When del lies to you

When ready, say 👉 “START DAY 4”