Foundations — Nested conditionals
This is a foundations page for Nested conditionals. It assumes you have seen nothing. Read top to bottom; each piece is used by the next.
The whole machine in one picture
Before we name the parts, look at what we are building toward.

Two gates in a row. The outer gate (cyan) opens only if its condition is true. Walk through it, and you reach the inner gate (amber) — a second question you could not even see until the first one let you through. That "gate behind a gate" picture is the entire topic. Everything below is just the vocabulary to describe it.
Symbol 1 — a value and its two special members: True and False
The picture. Think of a light switch. It is either ON (True) or OFF (False) — never both, never neither. There is no "maybe" bulb.
Why the topic needs it. A gate is either open or shut. True/False are the only two things a gate can be. Every condition you write will eventually boil down to one of these two switches.
Symbol 2 — comparison operators, the machines that produce True/False
The picture. A weighing scale. You put n on the left pan and 0 on the right pan; the scale doesn't tell you a number — it lights a lamp that says True if the tilt matches your question.

Why == and not =? This is the single most important distinction on this page.
Why we need the difference. A gate must ask a question, not change your data. If a condition used =, it would silently overwrite your value while pretending to test it. Python forbids this: if x = 5: is a SyntaxError. See Comparison operators.
Worked micro-checks (each is a whole condition on its own):
-7 >= 0→False(−7 is not bigger-or-equal to 0).5 > 10→False.5 > 0→True.n == 0whenn = -7→False.
Symbol 3 — a condition (a Boolean expression)
The picture. A condition is the question written on a gate. Reading it gives you a switch state; that state decides if the gate opens.
Why the topic needs it. Both the outer gate and the inner gate are labelled with a condition. Nesting is literally "a gate labelled with a condition, standing behind another gate labelled with a condition."
Symbol 4 — combining conditions: and, or
The picture. and is two gates on one path, back to back — you pass only if both open. or is two doorways into the same room — any one open lets you in.
Why the topic needs it. The parent's Example 3 shows you can sometimes replace nesting if age >= 18: if has_license: with the single flat condition if age >= 18 and has_license:. To judge when that's allowed, you must know exactly what and means. See Boolean logic and operators.
Recall
True and False evaluates to what?
::: False — and needs both true.
Symbol 5 — the colon : and the block it opens
The picture. The : is the doorway of the gate; the block is the room behind the door.
Why the topic needs it. The parent's whole structure is if ...: then an indented body. Miss the colon and Python cannot tell where the room begins — it raises a SyntaxError.
Symbol 6 — indentation: the depth that says "which gate owns me"
This is the pivot on which the entire topic turns.
The picture. Each step of indentation is one step deeper inside a nested box. Lines at the same depth are roommates in the same box; a line indented further has stepped into a smaller box inside.

Why the topic needs it. Nesting is indentation. The inner if is "inside" the outer if only because it is indented one level deeper. And the rule that makes else unambiguous is:
All cases of the else-attachment rule — walk each one:
Case A — else deep (inner):
if a > 0:
if b > 0:
print("both pos")
else: # indented level 2 → mates with 'if b > 0'
print("a pos, b not")The else sits at the same depth as if b > 0, so it pairs with it.
Case B — else shallow (outer):
if a > 0:
if b > 0:
print("both pos")
else: # indented level 1 → mates with 'if a > 0'
print("a not positive")Same code, else dedented one level. Now it pairs with the outer if. This is the dangling-else trap the parent warns about: nothing errors, but the meaning flipped. See Indentation in Python.
Symbol 7 — if, elif, else: the three gate-words
The picture. A row of gates you try in order. The first one whose condition opens lets you through, and you skip all the rest. If none open, you fall into the final else room. See Conditional statements (if-elif-else).
Why the topic needs it. These three words are the raw bricks. A nested conditional is just one of these structures placed inside the block of another.
Putting it together — read one nested block using every symbol
n = -7
if n >= 0: # gate 1: condition uses >= (Symbol 2)
if n == 0: # gate 2, indented (Symbol 6) — reached only if gate 1 opened
print("zero")
else: # mates with 'if n == 0' by depth
print("positive")
else: # mates with 'if n >= 0' by depth
if n < -10:
print("very negative")
else:
print("negative")Trace, naming the piece used at each step:
n >= 0is-7 >= 0→ comparison (Sym 2) →False.- First gate shut → skip its whole block (Sym 5), fall to the
elsethat mates by indentation (Sym 6) withif n >= 0. - Inside:
n < -10is-7 < -10→False. - That inner
elseruns → outputnegative.
Prerequisite map
Equipment checklist
You are ready for Nested conditionals if you can answer each:
The two truth values, spelled exactly
True and False (capital first letter).Difference between = and ==
= stores a value (assignment); == asks if two values are equal (comparison), returning True/False.What if x = 5: produces and why
==, not assign with =.Value of -7 >= 0
False.Value of True and False
False — and needs both true.What the colon : after if condition announces
if.What decides which if an else belongs to
if at the same level, not the visually closest one.Why the inner if may never be checked
False, Python skips the entire outer block, including the nested if inside it.