Excellent.
DAY 12 is where you stop writing control flow by habit and start writing it by understanding how Python actually executes it.
This day directly improves:
- Code clarity
- Performance intuition
- Interview answers (“why
foroverwhile?”)
🧠 DAY 12 — Control Flow Deep Dive
(if / for / while / bytecode & execution cost)
🔑 CORE IDEA OF DAY 12
Python control flow is not syntax magic — it is bytecode + stack manipulation + iterator protocol.
Once you see that, everything becomes predictable.
1️⃣ Control Flow in Python = Conditional Jumping
At runtime, Python:
- Evaluates expressions
- Pushes results on stack
- Uses jump instructions to move execution
There is no special CPU instruction for if or for.
It’s all bytecode + jumps.
2️⃣ if Statement — What REALLY Happens
if x > 10:
y = 1
else:
y = 2
Execution steps:
- Evaluate
x > 10 - Result is
TrueorFalse - Conditional jump based on truthiness
- Execute one branch
Bytecode View (Simplified)
import dis
def f(x):
if x > 10:
return 1
else:
return 2
dis.dis(f)
You’ll see instructions like:
COMPARE_OP
POP_JUMP_IF_FALSE
RETURN_VALUE
🧠 if is just a conditional jump based on truthiness
3️⃣ Truthiness Revisited (Control Flow Depends on It)
In if x: Python does:
- Call
x.__bool__()if exists - Else call
x.__len__() - Else assume
True
This means:
if []:
...
does NOT check equality with False.
It checks truthiness protocol.
4️⃣ for Loop — NOT What You Think
❌ Common misconception
foris index-based looping
✅ Reality
Python
foris iterator-based looping
for x in iterable:
...
Internally:
iter_obj = iter(iterable)
while True:
try:
x = next(iter_obj)
except StopIteration:
break
5️⃣ Why for Is Preferred Over while
for x in lst:
...
vs
i = 0
while i < len(lst):
x = lst[i]
i += 1
Reasons for is better:
- Fewer bytecode instructions
- No repeated
len()calls - No index errors
- Cleaner semantics
- Faster in practice
6️⃣ Bytecode Comparison: for vs while




for loop uses:
GET_ITER
FOR_ITER
STORE_FAST
while loop uses:
COMPARE_OP
POP_JUMP_IF_FALSE
👉 for delegates complexity to C-level iterator → faster.
7️⃣ break, continue, pass (Not the Same)
break
- Exits loop immediately
- Skips
elseblock (important!)
continue
- Skips current iteration
- Goes to next
next()
pass
- Does nothing
- Placeholder, syntactic
8️⃣ Loop else (INTERVIEW FAVORITE)
for x in lst:
if x == 5:
break
else:
print("not found")
else runs ONLY IF:
- Loop finishes naturally
- No
breakexecuted
🧠 This is not a bug — it’s intentional.
9️⃣ range() Is NOT a List
r = range(10)
- Lazy
- Immutable
- Constant memory
- Produces numbers on demand
list(range(10)) # creates list
Use range directly whenever possible.
🔟 Nested Loops & Time Complexity (Silent Killer)
for i in range(n):
for j in range(n):
...
Time complexity:
O(n²)
Python won’t warn you.
You must think algorithmically.
11️⃣ Short-Circuit Evaluation (VERY IMPORTANT)
if a and b:
Python evaluates:
a- If
ais falsy → stop - Else evaluate
b
This allows:
if obj is not None and obj.value > 10:
Without crashing.
12️⃣ Control Flow Cost Model (Mental Model)
| Construct | Cost |
|---|---|
if | Cheap |
for | Efficient |
while | Risky if misused |
| nested loops | Dangerous |
break | Cheap |
| exceptions | Expensive |
🔥 INTERVIEW TRAPS (DAY 12)
Q1
if []:
print("yes")
else:
print("no")
✔ Output: no
Q2
for i in range(3):
print(i)
else:
print("done")
✔ Output:
0
1
2
done
Q3
i = 0
while i < 3:
print(i)
i += 1
else:
print("done")
✔ else still runs
(Only skipped if break)
Q4
Why is for preferred in Python?
✔ Iterator protocol
✔ Fewer errors
✔ Faster
✔ Cleaner bytecode
🧠 DAY 12 MENTAL CHECKLIST
Before writing loops:
- Is this iterable?
- Can I avoid indexing?
- Is nesting necessary?
- Can comprehension replace this?
- Will this terminate safely?
📝 DAY 12 ASSIGNMENT (MANDATORY)
1️⃣ Predict output (NO RUNNING):
for i in range(5):
if i == 3:
break
else:
print("finished")
print("end")
2️⃣ Explain clearly:
- Why Python
foris not index-based - Why
for-elseexists
3️⃣ Think like interviewer:
Why does Python avoid
do-whileloops?
🔜 DAY 13 PREVIEW
DAY 13 — Functions Internals (Stack Frames, LEGB & Call Mechanics)
You’ll learn:
- How function calls work internally
- Stack frames & local scopes
- Why recursion is expensive in Python
- How Python resolves variable names
When ready, say 👉 “START DAY 13”