Python elif Statement
elif Statement
Often a decision has more than two possible outcomes. A grade can be A, B, C, or F. A traffic light can be red, yellow, or green. A menu can offer several options. The elif statement lets you check several conditions in sequence and run the block that belongs to the first condition that is true.
What Is elif?
elif is short for "else if". It adds another condition to an if statement.
- If the
ifcondition isFalse, Python checks the firstelifcondition. - If that is also
False, Python checks the nextelif, and so on. - As soon as one condition is
True, its block runs and all remaining conditions are skipped.
Syntax
if condition1:
# runs if condition1 is True
elif condition2:
# runs if condition1 is False and condition2 is True
elif condition3:
# runs if the conditions above are False and condition3 is True
else:
# optional: runs if none of the conditions are True
An elif must always follow an if (or another elif); it cannot appear on its own.
Basic Example of elif
x = 45
y = 60
if x > y:
print("x is greater than y")
elif x < y:
print("x is smaller than y")
Expected output:
x is smaller than y
Explanation:
- The
ifcondition checks whetherxis greater thany:45 > 60isFalse. - Python moves on and evaluates the
elifcondition:45 < 60isTrue. - The
elifblock runs and prints the message.
What happens if x and y are equal? Both conditions are False, so nothing is printed. You can handle that case with another elif x == y: or with an else block.
Using Multiple elif Conditions
You can include any number of elif statements. Python checks them in order, from top to bottom, and runs the first one that matches.
Example: Student Result Evaluation
marks = 68
if marks >= 85:
print("Distinction")
elif marks >= 70:
print("First Class")
elif marks >= 55:
print("Second Class")
elif marks >= 40:
print("Pass")
Expected output:
Second Class
How Python evaluates it:
| Step | Condition | Result |
|---|---|---|
| 1 | 68 >= 85 | False — move on |
| 2 | 68 >= 70 | False — move on |
| 3 | 68 >= 55 | True — print "Second Class" and stop |
| 4 | 68 >= 40 | Not checked |
Notice that 68 >= 40 is also true, but it is never evaluated. Only the first true condition runs.
How elif Execution Works
Python follows these rules:
- Conditions are checked from top to bottom.
- At most one block is executed.
- Once a condition is true, the rest are skipped.
Even if several conditions could be true, only the first one runs. This is why the order of conditions matters.
Why Order Matters
Compare the correct order above with this incorrect version:
marks = 92
if marks >= 40:
print("Pass")
elif marks >= 85:
print("Distinction")
Expected output:
Pass
A score of 92 deserves "Distinction", but marks >= 40 is checked first and is already true, so the more specific condition is never reached. When conditions use ranges, check the most restrictive condition first.
Adding an else Block
An else block at the end catches every case that none of the conditions matched.
Example: Age-Based Classification
user_age = 32
if user_age < 12:
print("Child category")
elif user_age < 18:
print("Teen category")
elif user_age < 60:
print("Adult category")
else:
print("Senior category")
Expected output:
Adult category
32 < 12 and 32 < 18 are both False; 32 < 60 is True, so "Adult category" is printed.
Why else is useful:
- It catches all remaining cases.
- It guarantees that exactly one block runs, so the program always produces a result.
In the student-results example earlier, a mark below 40 would print nothing. Adding else: print("Fail") would cover that case.
When Should You Use elif?
Use elif when:
- The conditions are mutually exclusive — only one outcome makes sense.
- Only one block should execute.
- You want clear and efficient logic.
elif vs Multiple if Statements
Using separate if statements instead of elif causes every condition to be checked, and can run more than one block:
marks = 92
if marks >= 85:
print("Distinction")
if marks >= 70:
print("First Class")
if marks >= 55:
print("Second Class")
Expected output:
Distinction
First Class
Second Class
All three conditions are true, so all three messages print — which is wrong for a grading system. With elif, only "Distinction" would print, and the remaining checks would be skipped.
| Feature | if / elif chain | Separate if statements |
|---|---|---|
| Blocks that can run | At most one | Any number |
| Conditions checked | Until the first true one | All of them |
| Best for | Mutually exclusive choices | Independent checks |
Example: Menu Selection Program
option = 4
if option == 1:
print("Start Game")
elif option == 2:
print("Load Game")
elif option == 3:
print("Settings")
elif option == 4:
print("Exit")
else:
print("Invalid option")
Expected output:
Exit
Explanation:
- Only one menu option is executed.
- Python stops checking as soon as
option == 4matches. - The
elseblock handles any number outside 1–4.
Tip: For long chains that compare one value against many fixed options, Python 3.10 and later also offer the
matchstatement, covered in the Python Match lesson.
Practical Example: Electricity Bill Slabs
units = 230
if units <= 100:
rate = 3.0
elif units <= 200:
rate = 4.5
elif units <= 300:
rate = 6.0
else:
rate = 8.0
bill = units * rate
print(f"Units: {units}, Rate: {rate}, Bill: {bill}")
Expected output:
Units: 230, Rate: 6.0, Bill: 1380.0
Each branch assigns a value to rate. After the chain finishes, rate is used in the calculation. Because every possible value of units reaches exactly one branch, rate is always defined.
Common Mistakes
| Mistake | Problem |
|---|---|
Writing else if | SyntaxError — Python uses elif |
| Putting a broad condition before a narrow one | The narrow condition is never reached |
Using separate if statements for exclusive choices | Several blocks may run |
Forgetting an else for unexpected values | Some inputs produce no result |
Writing elif without a preceding if | SyntaxError |
Related Concepts
- Python if Statement — the basics of conditional execution
- Python else Statement — handling the "otherwise" case
- Python Match — pattern matching for many fixed options
- Python Nested If — decisions inside decisions