The Quiet Terminal

← Beginner

if, elif, else: teaching your program to decide

2026-04-30 · 3 min read

A script with no decisions is a calculator: same input, same output, every run. The first if you write is the moment the program starts looking at the world and reacting to it. This post is the shape of if, the operators that feed it, and one surprise about emptiness that cost me an hour once.

The shape: a grade classifier

score = 83

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print(grade)    # B

Python checks the conditions top to bottom and stops at the first true one: 83 fails the A check, passes the B check, and the rest never even runs. That's why order matters — put the >= 60 band first and every paper in the room comes back a D. An elif is just "else, if": another question, asked only when the previous ones said no. The else at the bottom has no condition because by then everything else has already failed.

The questions: comparisons

score == 90     # equal to — two signs!
score != 90     # not equal
score < 90      # strictly less
score >= 90     # greater or equal

One confusion to head off early: = assigns, == asks. A single = inside an if doesn't do anything sneaky — Python refuses to start at all:

SyntaxError: invalid syntax. Maybe you meant '==' instead of '='?

One of the friendliest error messages in the language — on recent Pythons, at least. Older versions just say invalid syntax and leave you comparing the line character by character.

One nicety other languages lack: comparisons chain, so 0 <= score <= 100 does exactly what it reads like — both comparisons have to hold.

Combining questions: and, or, not

age = 17
with_adult = True

if age >= 12 and with_adult:
    print("allowed on the ride")

if not with_adult or age < 12:
    print("come back with a grown-up")

and needs both sides true, or needs at least one, not flips. Conditions chain nicely, but a line with three operators and a squint is a line I rewrite: naming the condition — is_supervised = with_adult and age >= 12 — costs one extra line, and the if below it reads like the sentence it implements.

The surprise: empty is false

name = ""
if name:
    print("hello", name)     # never runs — "" counts as false

cart = []
if not cart:
    print("cart is empty")   # the idiomatic emptiness check

An empty string counts as false — and so do 0, an empty list, an empty dict, and None. That's a feature and a trap in the same coat. The feature: if not cart: is the clean way to say "nothing in here". The trap: if 0 is a real value in your program — a count, a score — if count: will treat it as if it weren't there. When zero and absent need different handling, the honest check is if count is not None:. Choose that one on purpose, not by reflex.

Between the classifier and the emptiness check, most of my day-to-day decisions are covered: compare, combine, and remember that empty counts as false. Everything else is decoration.