Decisions in Python: if, elif, else, and Truthy Lies
How Python makes decisions with if, elif, and else, why indentation is law, and which values Python secretly treats as True or False.
You'll learn how if, elif, and else steer your Python script, why indentation is mandatory, and how Python decides whether empty strings or zero are True or False. Assumes Python 3 installed and basic variable familiarity.
1. Programs need forks in the road
Without decisions, a program is just a train on a straight track—it runs the exact same lines from top to bottom every time. An if statement asks a yes-or-no question and only runs the indented block when the answer is True.
In Python, yes-or-no values are called booleans (bool), and there are only two: True and False (always capitalized).
coffees = 0
if coffees == 0:
print("Gerald is not speaking until further notice.")Gerald is not speaking until further notice.Look closely at two tiny details:
- The colon
:at the end of theifline. - The 4 spaces before
print(...). Python does not use curly braces{}like JavaScript or Go; indentation is how Python knows which lines belong inside theif. Forget the spaces and Python refuses to run.
2. Chaining choices with elif and else
When life gives you more than two outcomes, chain elif (short for "else if") and end with else as the fallback:
battery = 15
if battery >= 80:
print("Plenty of juice.")
elif battery >= 20:
print("You'll probably survive the meeting.")
elif battery > 0:
print("Find a charger immediately.")
else:
print("Too late. Goodbye.")Find a charger immediately.Python checks from top to bottom and runs only the first branch that matches. Even though 15 > 0, if battery were 90, Python would print "Plenty of juice." and skip the rest.
Change battery to 0 and 100 in a .py file and run python3 yourfile.py to confirm only one branch prints each time.
3. Combining checks with and, or, not
and— both sides must beTrueor— at least one side must beTruenot— flipsTruetoFalseandFalsetoTrue
is_weekend = False
on_vacation = True
if is_weekend or on_vacation:
print("Alarm stays off.")Alarm stays off.Watch out for the classic = vs == trap: = attaches a variable name; == compares two values.
4. Truthy and falsy: shortcuts that surprise people
You don't always have to write if name != "" or if len(items) > 0. Python treats empty or zero values as falsy (False in an if), and almost everything else as truthy:
- Falsy:
False,None,0,0.0,""(empty string),[](empty list),{}(empty dict) - Truthy: non-zero numbers, non-empty strings (even
"False"or"0"!), non-empty lists
username = ""
if username:
print(f"Welcome, {username}")
else:
print("Please type a username.")Please type a username.Common mistakes
| Error / Bug | What it means | Fix |
|---|---|---|
IndentationError: expected an indented block | Missing 4 spaces under if, elif, or else | Indent the body lines consistently with 4 spaces |
SyntaxError: expected ':' | Forgot : at the end of the if line | Add : after the condition |
if x = 5: raises SyntaxError | Used assignment = instead of comparison == | Change = to == |
Practice
- What does
if "False": print("yes")print, and why? - If
x = 10, what doesif x > 2: print("A")followed byelif x > 5: print("B")print?
Answers:
yes—"False"is a non-empty string, so Python treats it as truthy!Aonly, because Python stops after the first matching branch.
What you learned
if,elif, andelserun at most one matching branch using 4-space indentation.==compares values;=assigns variables.- Empty values (
"",[],0,None) are falsy; non-empty values are truthy.