if / elif / else
Prerequisites: Conditional statements
elif ("else if") checks another condition when the first one is false;
else catches everything else.
Syntax
if condition_1:
...
elif condition_2:
...
else:
...
Example
age = 15
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
Expected output
Teenager
Python checks each condition top to bottom and runs only the first
block whose condition is True — the rest are skipped entirely, even if
they'd also be true.
Common mistakes
- Using several separate
ifstatements whenelifwas intended — with separateifs, more than one block can run; withelif, at most one does.
Practice
Write a program that assigns a score variable and prints "Pass" if
it's 50 or above, otherwise "Fail".
Show solution
score = 42
if score >= 50:
print("Pass")
else:
print("Fail")