1.2.15 · D3Introduction to Programming (Python)

Worked examples — if - elif - else — syntax, indentation rules

3,040 words14 min readBack to topic

This page is a drill. The parent topic note taught the rule; here we hit every kind of case the rule can face, so you never meet a situation you haven't already seen solved.


A tiny bit of shorthand (read this first)

To talk about the rule compactly, we use a few plain-English abbreviations. You never type these in Python — they are just note-taking labels:


The scenario matrix

Think of every if-decision as a machine that takes some input and must land in exactly one branch. What can that input be? Let's enumerate the "shapes" of situation:

Cell Case class What it stress-tests
A First test True Chain stops immediately, later tests never run
B A middle elif True Reaching block 2: test 1 False, test 2 True
C All tests False → else The fallback catches everything left over
D Boundary / equality value >= vs > at the exact edge (the "off-by-one" trap)
E Zero / empty input (truthiness) 0, "", [] behave as False
F No else present Possibility that no branch runs
G Nested block A decision inside a decision, two indent levels
H Separate ifs vs elif Multiple branches running & overwriting
I₁ Compound condition with and Both sides must be True
I₂ Compound condition with or (short-circuit) Either side True is enough; stops early
J Real-world word problem Translating English into a chain
K Exam twist Order-of-tests bug you must spot

The 11 examples below cover every cell, one cell per example. The label after each title says which one.


Worked examples


Recall One-line summary of the whole matrix

Every input lands in exactly one branch: the first test that is True (truthiness included), or the else-block if none are — provided your tests are ordered specific-first and you use elif, not stacked ifs.


Active recall

Cell A — if temp=5 in the cold/mild/warm chain, output?
cold (first test True, rest never checked).
Cell D — if marks > 40 ... elif marks >= 40 with marks=40, output?
exactly pass (> excludes the edge, >= includes it).
Cell E — is an empty list [] truthy or falsy?
Falsy — it counts as False in a condition.
Cell H — two separate ifs both True, which assignment wins?
The last one (later blocks overwrite earlier results).
Cell I₂ — in A or B with A True, does Python evaluate B?
No — or short-circuits and returns True without reading B.
Cell K — why does if x>10 ... elif x>100 mislabel 500?
The broad test x>10 fires first and stops the chain; put the larger threshold first.

Connections

  • 1.2.15 if - elif - else — syntax, indentation rules (Hinglish) — the parent topic note
  • Booleans and comparison operators<, <=, ==, >=, > produce the conditions
  • Logical operators and, or, not — building the compound tests in Examples 9 and 10
  • Truthiness in Python — why [], 0, "" act as False (Example 5)
  • Indentation and code blocks — how nesting in Example 7 is parsed
  • while and for loops — conditions reused to control repetition
  • match-case statement — a cleaner form when branching on one value's cases
  • Ternary conditional expression — the one-line a if cond else b shortcut