Python’s if and else let a program choose between blocks of code. Add elif when there are more conditions to check. In each chain, Python runs only the suite for the first condition that is true, or the optional else suite if none is true.
How an if-else statement works
Start with if, write a condition, and end the line with a colon. Indent the code that should run when the condition is true. An optional else clause provides the alternative when the condition is false.
As an Amazon Associate I earn from qualifying purchases.
age = 17
if age >= 18:
print("Adult")
else:
print("Not an adult")
Here, age >= 18 is false, so Python runs the indented else body. The colon marks the start of each clause’s body; indentation shows which statements belong to it.
How to check more than two cases with elif
Use elif (“else if”) to add another condition between if and else. Python checks the clauses from top to bottom and selects only the first suite whose condition is true. The else clause is optional, and a chain can contain zero or more elif clauses, as described in the Python 3.14 tutorial.
#1 Best Overall
score = 82
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "Below B"
print(grade)
This prints B: the first test is false, and the second is true. Put more specific or higher-threshold conditions first when they overlap. For example, if the score >= 80 test came before score >= 90, a score of 95 would match the former and the later branch would never be reached.
When to use a conditional expression
If the choice is just between two values, Python also offers a conditional expression, sometimes called a ternary expression:
Rank #2
label = "adult" if age >= 18 else "minor"
It follows the form x if condition else y: Python evaluates the condition, then evaluates and returns x if it is true or y if it is false. It is an expression, so it can appear where a value is expected. The Python language reference defines this behavior. Use a regular multi-line statement when a branch needs several actions or the one-line form would be hard to read.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallQuick Recap
Best Value
Errors and readability checks
- Missing colon: Put a colon after each
if,elif, andelseheader. - Inconsistent indentation: Indent each branch body consistently so Python can identify the suite it belongs to.
- More than one else: A chain can have only one
elseat that level. Useeliffor additional conditions. - Overlapping conditions in the wrong order: Check narrower or higher-threshold conditions before broader ones.
- Assignment instead of comparison: Use
==to test whether two values are equal;=assigns a value. - Overcomplicated one-liner: Prefer an ordinary
if/elif/elsestatement when it makes the logic easier to follow.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




