Question bank — Nested conditionals
Before you start, recall the one rule that governs almost everything here: in Python, how far a line is pushed to the right (indentation) decides which if it belongs to, and an else pairs with the nearest unmatched if at its own indentation level. Everything that follows is a variation on people forgetting that.
True or false — justify
Each item: decide true or false, then give the reason. Bare "true"/"false" is not an answer.
An else always pairs with the closest if written physically above it.
if at the same indentation, not the physically closest one. A less-indented if far above can be the real mate.If the outer condition is False, Python still checks the inner condition once.
False outer condition skips the entire outer body, so the nested if is never even looked at. See Conditional statements (if-elif-else).if a and b: and if a: \n if b: always produce identical behaviour.
else branches. Once either level needs its own else/message, the shapes diverge.Deeper indentation always means "runs less often".
Every nested if can be rewritten with elif instead.
elif handles mutually exclusive alternatives at one level; nesting handles dependent sub-questions. They solve different shapes of logic.An inner else can attach to the outer if by accident.
else to the outer level and it silently becomes the outer's else. See Code readability and refactoring.If both branches of an inner if/else print the same thing, the nesting is pointless there.
Nesting three conditions with and is always worse style than three nested ifs.
and is cleaner when you want one outcome; nesting is only better when each level needs its own branch or message.Spot the error
For each snippet: name what is wrong (or say "nothing wrong") and why.
if user == "admin"
print("hi")::: Missing colon : after the condition — Python needs the : to know a block follows, so this raises a SyntaxError.
if x = 0:
print("zero")::: = is assignment, not comparison — a condition needs ==. This is a SyntaxError. See Comparison operators.
if a > 0:
if b > 0:
print("both")
else:
print("a not positive")::: The else is indented to the outer if a > 0, so it means "a not positive" — likely not the intent if you wanted an else for b. Logic-legal but a common misread; indent it deeper to attach to the inner if.
if logged_in:
print("welcome")::: The body print("welcome") is not indented — Python requires the block under an if to be indented, so this raises an IndentationError. See Indentation in Python.
if score >= 90:
print("A")
elif score >= 90:
print("also A?")::: The elif condition is unreachable — any score hitting the second is already caught by the first identical test. Not an error, but dead code / a logic bug.
if temp > 30:
if temp > 40:
print("hot")
print("warm")::: Nothing is broken, but note print("warm") runs for all temps over 30 (it's at the outer body level), while "hot" only prints over 40 — easy to misread the indentation as if both were nested together.
Why questions
Answer with the mechanism, not a restatement.
Why does indentation, and not brackets, decide nesting in Python?
Why can flattening with and improve readability?
if each line belongs to. See Boolean logic and operators.Why might you deliberately keep nesting even when and would work?
else branch — flattening collapses those separate outcomes into one.Why is a nested check sometimes safer than a combined one?
and short-circuits too, but nesting makes the dependency explicit.)Why does the inner condition "not make sense" before the outer passes?
Why does a wrongly-attached else fail silently rather than crash?
Edge cases
Boundary and degenerate inputs — the scenarios the topic must still handle.
What happens if the outer if has no else and the outer condition is False?
If an inner if has no else, and the inner condition is False, what runs?
else means "do nothing in the false case".With n = 0 in a if n >= 0: / inner if n == 0: structure, which branch fires?
0 >= 0 is true (enter outer), then 0 == 0 is true, so the boundary value lands on the equality case, not "positive".Can a nested conditional have zero statements that ever run for some input?
False and there's no outer else, or the inner is False with no inner else, the whole thing can produce no action at all.Is if True: \n if False: \n ... \n else: — which if owns the else?
if True: owns it, because the else is at the outer indentation. Since outer is True, that else never runs.What if two nested conditions are logically contradictory, like if x > 5: inside if x < 0:?
Recall
Verdict alone is worthless here — could you reconstruct the reason for every item above without peeking? If yes, re-read the parent 1.2.16 Nested conditionals (Hinglish) once and you're done with D5.
Connections
- Conditional statements (if-elif-else) — the branch semantics every trap relies on.
- Boolean logic and operators — flatten-with-
andquestions. - Indentation in Python — the root cause of dangling-else confusion.
- Comparison operators —
==vs=trap. - Code readability and refactoring — arrow code and dead-branch smells.