1.2.15Introduction to Programming (Python)

if - elif - else — syntax, indentation rules

2,049 words9 min readdifficulty · medium4 backlinks

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_c

Why must exactly one run (with else)? The conditions partition all possibilities: either some ckc_k 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.

Figure — if  -  elif  -  else — syntax, indentation rules

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?
A colon :
How does Python know which lines belong inside an if block?
By their indentation (leading whitespace), not braces.
How many elif blocks can one if chain have?
Zero or more.
How many else blocks can one if chain have?
At most one (and it's optional).
In an if/elif/elif/else chain, how many blocks run?
Exactly one — the first whose condition is True (or 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?
The >=70 branch (first True one).
What is the standard number of spaces per indentation level in Python?
4 spaces.

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 expressiona if cond else b one-liner

Concept Map

limited to

solved by

evaluates

True runs

first test

extra tests

fallback

ends with

ends with

ends with

4 spaces marks

no braces uses

guarantees

exhaustive and exclusive

Top to bottom flow

One fixed sequence

if elif else branching

Boolean expression True or False

Indented block of code

if mandatory once

elif checked only if above False

else when all False

Colon then indent

Indentation defines scope

Exactly one branch runs

Program reacts to data

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.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections