PY-01-006-P · Primer — Conditionals: if / elif / else
The idea in one sentence
if / elif / else let your program choose which lines to run based on whether a condition is true, and in Python the lines that belong to each choice are marked not by curly braces but by indentation — the spaces at the front of the line.
Why it exists
A program that always does exactly the same thing isn't very useful; the moment you want it to react — charge a discount if the cart is over $50, show an error if the password is blank, greet a returning user but else a new one — you need a way to fork the path. if is that fork: "run this block only when this is true." elif (short for "else if") lets you chain several forks so the program checks them in order and stops at the first match, which is exactly how a grading scale or a shipping-tier table works. else is the catch-all that runs when nothing above it matched, so you're never left with an unhandled case. Together they turn a flat list of instructions into a decision tree that can respond to whatever data shows up at runtime.
How it works
You write if condition: — note the colon — and then indent the lines that should run when the condition is true; that indented group is called a block. Python checks each condition in order: the first if/elif whose condition is truthy wins, its block runs, and every other branch is skipped, so ordering matters. If none match, the optional else: block runs. The standard indent is four spaces, and it must be consistent — Python uses the indentation itself to know where a block begins and ends, so a stray space is a syntax error, not a style nit. For a simple either/or that produces a value, Python also offers a compact one-line form, the conditional expression (often called the ternary): value_if_true if condition else value_if_false.
score = 82
if score >= 90:
grade = "A"
elif score >= 80: # only checked because 82 >= 90 was False
grade = "B"
elif score >= 70:
grade = "C"
else: # runs only if every test above failed
grade = "F"
print(grade) # B
print("pass" if score >= 60 else "fail") # pass (the ternary, on one line)
The one gotcha
Order is not cosmetic — the first matching branch wins, so a too-broad condition placed too early swallows everything below it. If you had written if score >= 70 before elif score >= 80, then an 82 would match the >= 70 line first, be labeled "C", and the >= 80 check would never even run. The fix is to arrange conditions from most specific / most restrictive to least, so each branch only catches what the ones above it didn't. A close cousin of this bug is using several separate if statements when you meant an elif chain: separate ifs are each evaluated independently, so more than one block can run, whereas an if/elif/else chain guarantees exactly one branch executes. And remember indentation is real syntax — mixing tabs and spaces, or forgetting to indent the block under a colon, raises IndentationError before your logic ever gets a chance to be wrong.
See it — how Python walks an if / elif / else chain
flowchart TD
A["Start: score = 82"] --> B{"score >= 90?"}
B -->|"True"| C["grade = 'A'"]
B -->|"False"| D{"score >= 80?"}
D -->|"True"| E["grade = 'B'"]
D -->|"False"| F{"score >= 70?"}
F -->|"True"| G["grade = 'C'"]
F -->|"False"| H["else: grade = 'F'"]
C --> Z["Skip the rest,<br/>continue program"]
E --> Z
G --> Z
H --> Z
The one-line takeaway
if/elif/else picks the first branch whose condition is true and runs only that one, indentation defines each branch's block, and the ternary A if cond else B is the compact one-line version for choosing a value.
Where you'll meet it
Conditionals are the first tool you reach for in nearly every program: validating input ("if the field is empty, show an error"), routing users ("if admin … elif editor … else viewer"), classifying data into buckets, or deciding whether a loop should keep going. You'll pair them constantly with the comparison and boolean operators (==, <, and, or, not) you're learning alongside, and you'll see nested conditionals — an if inside another if's block — when one decision depends on the outcome of an earlier one. In real code the ordering gotcha shows up in pricing rules, permission checks, and grading logic often enough that "put the most specific condition first" becomes a reflex. Master this and you've unlocked the core of control flow; loops, functions, and error handling all build directly on the branching instinct you develop here.
Named concepts: if / elif / else branching, indentation and blocks, nested and chained conditions, the ternary conditional expression
