if - elif - else — syntax, indentation rules
WHY does this exist?
WHAT it is
HOW the syntax looks
if condition1:
# block A — runs if condition1 is True
statement_a
elif condition2:
# block B — runs if condition1 False AND condition2 True
statement_b
else:
# block C — runs if both above were False
statement_cWhy must exactly one run (with else)? The conditions partition all possibilities: either some is the first True one, or none are → else. The set of cases is covered with no overlap. This is why a clean if/elif/else chain is exhaustive and exclusive.

Derivation: how Python reads indentation
Worked examples
Common mistakes (Steel-man + fix)
Recall Feynman: explain to a 12-year-old
Imagine a hallway with doors. You walk and ask the first question on a door: "Is it true?" If yes, you go through that door and the hallway ends — you don't check the other doors. If no, you walk to the next door (elif) and ask again. If you reach the end and no door said yes, you go through the special last door marked else. The way you push your sentences a few spaces to the right is how you tie a set of actions to a particular door — everything pushed in together happens after you walk through that door.
Active recall
What punctuation must every if, elif, and else line end with?
:How does Python know which lines belong inside an if block?
How many elif blocks can one if chain have?
How many else blocks can one if chain have?
In an if/elif/elif/else chain, how many blocks run?
else if none are).What error do you get from mixing tabs and spaces?
TabError: inconsistent use of tabs and spaces.Difference between = and == in a condition?
= assigns a value; == tests equality. Conditions need ==.Why prefer elif over multiple separate ifs?
elif makes branches mutually exclusive and stops at the first match; separate ifs all run and can overwrite results.For score=70 in a chain checking >=90, >=80, >=70, else, what is selected?
>=70 branch (first True one).What is the standard number of spaces per indentation level in Python?
Connections
- Booleans and comparison operators — conditions evaluate to
True/False - Logical operators and, or, not — building compound conditions
- Truthiness in Python — non-bool values in
if(0, "", [] are False) - Indentation and code blocks — the same rule governs loops and functions
- while and for loops — also use
:and indented blocks - match-case statement — alternative branching (Python 3.10+)
- Ternary conditional expression —
a if cond else bone-liner
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, normally Python code upar se neeche, line by line chalta hai. Lekin asli zindagi mein decisions condition pe depend karte hain — "agar baarish ho rahi hai to chhata le lo". Yahi kaam if / elif / else karta hai: program ko ek raasta choose karne ki power deta hai. if pehla sawaal poochta hai, agar True mila to uska block chalta hai aur baaki sab skip. Agar False hua to elif (else-if) wala agla sawaal check hota hai, aur agar koi bhi match nahi hua to else wala fallback chalta hai.
Sabse important baat: exactly ek hi branch chalta hai — jo pehle True mila wahi jeet gaya, baaki check hote hi nahi. Isliye grade waale example mein score=85 ke liye >=90 fail hua, >=80 True hua, "B" mil gaya, aur >=70 ko check hi nahi kiya. Yahi reason hai ki alag-alag if lagane se bachna chahiye — wahan saare if chalte hain aur baad waala result ko overwrite kar deta hai. elif use karo taaki branches mutually exclusive rahein.
Indentation Python ka grammar hai, decoration nahi. Jo lines ek branch ke andar hain unko 4 spaces andar push karte hain, aur har header line (if, elif, else) ke end mein colon : lagana zaroori hai — curly braces {} yahan nahi hote. Tab aur space mat mix karo, warna TabError aata hai. Aur dhyaan rakho: comparison ke liye == lagta hai, = to assignment hota hai. Yeh chhoti cheezein hi exams aur real coding dono mein sabse zyaada galtiyan karwati hain, isliye COIN mnemonic yaad rakho.