Nested conditionals
WHAT is a nested conditional?
if outer_condition:
if inner_condition:
# runs only if BOTH are True
...
else:
# outer True, inner False
...
else:
# outer False — inner is never even checked
...The key word is indentation. In Python, how deep a line is indented decides which if it belongs to. That is the whole game.
WHY do we need them?
You could sometimes flatten nesting into combined conditions with and, but nesting wins when:
- You want different messages at each level (not just one final yes/no).
- The inner check is expensive or unsafe unless the outer passed first (e.g. don't divide unless the denominator check passed).
HOW Python reads it — the indentation rule

Worked Example 1 — Login then role
logged_in = True
is_admin = False
if logged_in: # outer gate
if is_admin: # inner gate (only reached if logged_in)
print("Welcome, admin!")
else:
print("Welcome, user!")
else:
print("Please log in.")Trace:
logged_inisTrue→ enter outer body. Why? The outerelseis skipped entirely.is_adminisFalse→ take innerelse. Why? Innerifwas reached because we passed the outer gate.- Output:
Welcome, user!
Worked Example 2 — Number sign and magnitude
n = -7
if n >= 0:
if n == 0:
print("zero")
else:
print("positive")
else:
if n < -10:
print("very negative")
else:
print("negative") # ← this runsTrace:
n >= 0?-7 >= 0isFalse→ jump to outerelse. Why? Whole first branch (including its innerif) is skipped.- Inside
else:n < -10?-7 < -10isFalse→ innerelse. Why? We're now choosing within the negative case. - Output:
negative
Worked Example 3 — Nesting vs flattening with and
# Nested
if age >= 18:
if has_license:
print("Can drive")
# Flattened — same result, less depth
if age >= 18 and has_license:
print("Can drive")Forecast-then-Verify
Common Mistakes
Flashcards
What is a nested conditional?
if/elif/else placed inside the body of another conditional; the inner one runs only if the outer branch was taken.In Python, what decides which if an else belongs to?
else pairs with the nearest unmatched if at the same indentation.When should you flatten nesting into and instead?
Output of if False: ... else: if True: print("x")?
x — outer is False so we take the outer else, then inner True prints x.Why does the inner condition sometimes never get checked?
if.What error does if x = 5: raise and why?
= is assignment; comparison needs ==.Recall Feynman: explain to a 12-year-old
Imagine a treasure box inside another box. To get the treasure you must first open the big box. Only after the big box is open do you even see the small box and try its lock. If the big box stays shut, you never touch the small one. Nested ifs are exactly that: the inside question is only asked after the outside question says "yes." The amount you push your line to the right (indentation) tells Python which box a line is inside.
Connections
- Conditional statements (if-elif-else) — the building block being nested.
- Boolean logic and operators —
and/orcan flatten some nests. - Indentation in Python — the rule that makes nesting unambiguous.
- Comparison operators —
==,<,>=used in the conditions. - Code readability and refactoring — avoiding deep "arrow code".
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Nested conditional ka matlab hai ek if ke andar doosra if. Socho ek darwaza (outer condition) hai — pehle wahi khulna chahiye, tabhi andar wala sawaal (inner condition) poocha jaayega. Agar outer condition False hai, toh Python poora andar ka block chhod deta hai — inner if ko chhuta tak nahi. Yahi reason hai ki "logged_in ho? agar haan, toh admin ho?" jaisa logic naturally nested banta hai.
Sabse important cheez Python mein hai indentation (line ko kitna right push kiya hai). Yahi decide karta hai ki koi line kis if ke andar hai, aur else kis if ke saath pair hoga. else hamesha usi if ke saath jodta hai jo same indentation par ho. Galti se indentation idhar-udhar ho gaya, toh code chalega zaroor par answer galat aayega — Python error nahi dega, bas logic bigad jaayega. Isiliye "same line, same mate" yaad rakho.
Kab nesting use karein aur kab nahi? Jab dono conditions True honi chahiye aur sirf ek hi outcome chahiye, tab and se flatten kar do: if age>=18 and has_license: — saaf aur padhne mein easy. Lekin jab har level par alag message ya alag branch chahiye, tab nesting sahi hai. Bahut zyada nesting (3-4 level deep) ko "arrow code" kehte hain — wo padhne mein mushkil hota hai, usse bachna chahiye. 80/20 rule: bas indentation aur else-matching pakka kar lo, baaki sab usi se clear ho jaata hai.