
When you need your program to choose between more than two possible paths, the Python if elif else statement is what you reach for. You already know that a plain if statement runs a block when a condition is true and skips it otherwise. Adding an else gives you a fallback. But when you have three, four, or more distinct conditions to check — like grading a score, routing user input, or classifying a value — you need elif. This guide covers exactly how Python if elif else works, how the chain is evaluated, and how to write clean, correct conditional logic using it.
elif is short for "else if." It lets you attach additional conditions to an if statement that Python only checks when the previous condition was False. Each elif branch is a new condition, evaluated in order from top to bottom. The moment Python finds a condition that is True, it runs that block and skips everything else in the chain.
Without elif, you would have to write separate if statements or deeply nested code. elif keeps everything flat and readable.
score = 72
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
Output
Grade: C
Python checks each condition top to bottom. score >= 90 is False, score >= 80 is False, score >= 70 is True — so it prints Grade: C and stops. The else block is never reached.
The full syntax for an if elif else chain looks like this:
if condition_one:
# runs if condition_one is True
elif condition_two:
# runs if condition_one is False and condition_two is True
elif condition_three:
# runs if both above are False and condition_three is True
else:
# runs if none of the above conditions are True
A few rules to keep in mind:
humidity = 45
if humidity < 30:
print("Air is dry")
elif humidity < 60:
print("Air is comfortable")
elif humidity < 80:
print("Air is humid")
else:
print("Air is very humid")
Output
Air is comfortable
Python evaluates conditions one by one from top to bottom. It stops as soon as it finds the first True condition and runs only that block. This means the order of your elif branches matters.
Consider this example where order makes a real difference:
points = 85
if points >= 60:
print("Passed")
elif points >= 80:
print("Distinction")
else:
print("Failed")
Output
Passed
Even though points is 85 and the second condition would also be True, Python already matched the first condition (points >= 60) and stopped. If you meant to catch distinctions separately, you need to put the stricter condition first:
points = 85
if points >= 80:
print("Distinction")
elif points >= 60:
print("Passed")
else:
print("Failed")
Output
Distinction
Always write your most specific or strictest conditions first when conditions overlap.
elif works with any valid Python condition, including equality comparisons using ==. This is common when routing based on user input, category names, or status codes.
day = "Wednesday"
if day == "Monday":
print("Start of the work week")
elif day == "Wednesday":
print("Midweek")
elif day == "Friday":
print("End of the work week")
else:
print("Another day")
Output
Midweek
Each elif checks a different value of the same variable. Python evaluates them in order and runs the first match.
You can chain as many elif branches as the situation requires. There is no practical limit. This example classifies an integer into five categories:
number = -7
if number > 100:
print("Large positive number")
elif number > 0:
print("Positive number")
elif number == 0:
print("Zero")
elif number > -100:
print("Small negative number")
else:
print("Large negative number")
Output
Small negative number
Python checks each condition in sequence. The first four conditions are False, and number > -100 is True (since -7 > -100), so that branch runs.
The else clause at the end is optional. If no condition in the chain matches and there is no else, Python simply moves on without running any block.
temperature = 20
if temperature > 35:
print("Very hot")
elif temperature > 28:
print("Hot")
elif temperature > 22:
print("Warm")
Output
The temperature is 20, which does not match any of the three conditions, and there is no else fallback. Python executes nothing and continues with the rest of the program. This is valid and sometimes exactly what you want — only act when a condition matches, silently do nothing otherwise.
You can combine conditions inside any if or elif branch using the logical operators and, or, and not. This lets a single branch handle more complex rules without adding more elif lines.
age = 25
has_license = True
if age < 16:
print("Too young to drive")
elif age < 18 and not has_license:
print("Under 18 and no license — cannot drive")
elif age >= 18 and has_license:
print("Qualified to drive")
else:
print("Check requirements")
Output
Qualified to drive
The third elif checks two things at once: age >= 18 and has_license must both be True. Since both are True here, that branch runs.
A common mistake beginners make is writing separate if statements when they should use elif. These behave differently.
With separate if statements, every condition is checked independently, even after one has already matched:
score = 95
if score >= 70:
print("Passed")
if score >= 90:
print("Excellent")
Output
Passed
Excellent
Both blocks run because Python checks each if independently. This is sometimes what you want, but when you want exactly one branch to run, you need elif:
score = 95
if score >= 90:
print("Excellent")
elif score >= 70:
print("Passed")
Output
Excellent
With elif, once the first condition matches, Python skips all remaining branches. Use elif when your conditions are mutually exclusive — when only one outcome should happen per execution.
You can use any expression that evaluates to True or False inside an elif condition. String methods like .startswith(), .endswith(), and .lower() are frequently used.
filename = "report.pdf"
if filename.endswith(".txt"):
print("Text file")
elif filename.endswith(".pdf"):
print("PDF document")
elif filename.endswith(".jpg") or filename.endswith(".png"):
print("Image file")
else:
print("Unknown file type")
Output
PDF document
This program takes a monthly sales figure and classifies the salesperson's performance tier, then prints a corresponding message.
def classify_sales(sales):
if sales >= 100000:
tier = "Platinum"
message = "Outstanding performance. Maximum bonus awarded."
elif sales >= 75000:
tier = "Gold"
message = "Excellent result. High bonus awarded."
elif sales >= 50000:
tier = "Silver"
message = "Good performance. Standard bonus awarded."
elif sales >= 25000:
tier = "Bronze"
message = "Meets expectations. Partial bonus awarded."
else:
tier = "Unranked"
message = "Below target. No bonus this month."
print(f"Tier: {tier}")
print(f"Message: {message}")
classify_sales(82000)
classify_sales(43000)
classify_sales(18000)
Output
Tier: Gold
Message: Excellent result. High bonus awarded.
Tier: Silver
Message: Good performance. Standard bonus awarded.
Tier: Unranked
Message: Below target. No bonus this month.
Each call to classify_sales runs the entire if elif else chain from top to bottom, finds the first matching condition, sets the tier and message, and prints the result. The three calls produce three different outcomes because each sales value falls into a different tier.
For more on Python's conditional expressions and control flow tools, refer to the official Python documentation on if statements.