1.2.16 · D5Introduction to Programming (Python)

Question bank — Nested conditionals

1,360 words6 min readBack to topic

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.
False — it pairs with the nearest unmatched 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 — a 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.
False in general — they match only when there is a single outcome and no else branches. Once either level needs its own else/message, the shapes diverge.
Deeper indentation always means "runs less often".
True in spirit — deeper code sits behind more gates, so more conditions must pass to reach it. Each nesting level is another gate you must clear.
Every nested if can be rewritten with elif instead.
Falseelif 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.
True — this is the dangling-else trap: mis-indent the 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.
True — if the inner decision changes nothing, the check is dead logic and should be removed for readability.
Nesting three conditions with and is always worse style than three nested ifs.
False — usually 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?
Python uses indentation as its block syntax — the visual structure is the logical structure, which forces readable layout but punishes stray spaces. See Indentation in Python.
Why can flattening with and improve readability?
It removes a whole level of rightward "arrow" nesting, so the eye reads one condition instead of tracing which if each line belongs to. See Boolean logic and operators.
Why might you deliberately keep nesting even when and would work?
When each level needs its own message or else branch — flattening collapses those separate outcomes into one.
Why is a nested check sometimes safer than a combined one?
The outer gate can guard against an unsafe inner operation (e.g. don't divide until you've confirmed the denominator is nonzero) — order of evaluation matters. (Python's and short-circuits too, but nesting makes the dependency explicit.)
Why does the inner condition "not make sense" before the outer passes?
The inner question is conditional on the outer being true — asking "is the user an admin?" is meaningless for someone who isn't even logged in.
Why does a wrongly-attached else fail silently rather than crash?
The mis-indented code is still valid Python — it runs fine, just with the wrong logic, so only your output reveals the bug.

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?
Nothing runs — control simply falls through past the whole structure with no output, which is valid and often intended.
If an inner if has no else, and the inner condition is False, what runs?
Nothing from that inner block — control continues after it. Absence of else means "do nothing in the false case".
With n = 0 in a if n >= 0: / inner if n == 0: structure, which branch fires?
The inner "zero" branch0 >= 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?
Yes — if the outer is 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?
The outer 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:?
The inner block is unreachable — no value satisfies both, so it's dead code even though Python accepts it without error. See Code readability and refactoring.

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-and questions.
  • Indentation in Python — the root cause of dangling-else confusion.
  • Comparison operators== vs = trap.
  • Code readability and refactoring — arrow code and dead-branch smells.