Python elif Statement
Table of Contents + −
In the last lesson you learned the if else Statement. That one picks between two paths. One path runs when a condition is true. The other runs when it is false. But real life is rarely just two paths. A student’s marks could land in many bands. It could be an A, a B, a C, or a fail. That is where elif comes in.
🤔 Why elif?
Here is the pain. With only if and else you get two outcomes. But a grade can be one of several things. It is not just “pass or fail”. You need a way to check one condition. Then another. Then another. You keep going until one fits.
That is exactly what elif does. The word elif is short for “else if”. It lets you line up several conditions in order. Then Python picks the first one that is true.
🧩 What elif Looks Like
Think of a security guard at a gate. He checks IDs one by one. He looks at the first ID. If it matches, he lets that person in and stops checking. If not, he moves to the next one. He keeps going until something matches. Or he runs out.
elif works the same way. You start with one if. Then you add as many elif checks as you want. At the end you can put an optional else for “none of the above”.
Here is the basic shape:
if condition1: # runs if condition1 is trueelif condition2: # runs if condition1 was false and condition2 is trueelif condition3: # runs if the ones above were false and condition3 is trueelse: # runs if none of the above were trueHere are the things to notice:
- You always start with one
if. - You can add as many
elifblocks as you need. - The
elseat the end is optional. It catches everything that did not match. - Each condition ends with a colon. The body is indented underneath.
📝 A Real Example: Grades
Let’s turn marks into a letter grade. Say 90 and above is an A. 75 and above is a B. 50 and above is a C. Anything below that is a Fail.
This program checks the marks and prints the matching grade:
marks = 82
if marks >= 90: print("Grade: A")elif marks >= 75: print("Grade: B")elif marks >= 50: print("Grade: C")else: print("Grade: Fail")Output
Grade: B
Let’s walk through what happened with marks = 82:
- Python checks
marks >= 90first. Is 82 greater than or equal to 90? No. So it skips that block. - Next it checks
marks >= 75. Is 82 greater than or equal to 75? Yes. So it printsGrade: B. - Once a match is found, Python stops. It does not even look at the
elif marks >= 50or theelse.
That last point is the heart of it. Python reads top to bottom. It stops at the first true condition.
⬇️ Order Matters
Here is something that trips up a lot of people. Python stops at the first match. So the order of your conditions changes the result.
Look at the same grade logic. This time the conditions are flipped around:
marks = 95
# ❌ Avoid: loosest condition firstif marks >= 50: print("Grade: C")elif marks >= 75: print("Grade: B")elif marks >= 90: print("Grade: A")else: print("Grade: Fail")Output
Grade: C
A student who scored 95 just got a C. That is wrong. But Python did exactly what we told it. The value 95 is greater than or equal to 50. So the very first condition matched. And Python stopped right there.
The fix is simple. Check the strictest condition first. Then work your way down:
marks = 95
# ✅ Good: strictest condition firstif marks >= 90: print("Grade: A")elif marks >= 75: print("Grade: B")elif marks >= 50: print("Grade: C")else: print("Grade: Fail")Output
Grade: A
When your conditions overlap like this, always start from the top end and move down. That way each elif only catches the values the ones above it missed.
🆚 elif vs Many Separate ifs
You might wonder this. Why not just write a separate if for each band? Like this:
marks = 82
# ❌ Avoid: separate ifs, every one gets checkedif marks >= 90: print("Grade: A")if marks >= 75: print("Grade: B")if marks >= 50: print("Grade: C")Output
Grade: B Grade: C
See the problem? With separate if statements, Python checks every single one. They are not connected. So 82 matched both the B line and the C line. It printed two grades. That is not what we want.
With elif, the checks are tied together as one chain. The moment one matches, the rest are skipped. So you get exactly one grade.
Tip
Use elif when only one of several outcomes should happen. Use separate if statements only when the checks are truly independent and more than one could be true at the same time.
Here is a quick side-by-side of the two approaches:
| Approach | How many run | Best for |
|---|---|---|
if / elif / else | Only the first match | Picking one outcome from many |
Many separate ifs | Every true one | Independent checks |
⚠️ Common Mistakes
Some slip-ups show up again and again. Watch for these:
- Putting the loosest condition first. As you saw, that makes an early condition swallow values meant for a later one. Order from strictest to loosest.
- Using separate
ifs when you meant a chain. That lets more than one block run. If only one outcome should happen, useelif. - Forgetting the colon. Every
if,elif, andelseline ends with a colon. Miss it and Python raises aSyntaxError. - Writing
else ifinstead ofelif. Python does not understandelse ifas one keyword. It is one word:elif.
Here is that last one shown wrong and right:
# ❌ Avoid: "else if" is not valid Python# else if marks >= 75:
# ✅ Good: it is one wordelif marks >= 75: print("Grade: B")✅ Best Practices
Keep these habits and your conditions will stay clear:
- Order your conditions so the strictest one comes first when the ranges overlap.
- Add an
elseat the end to catch anything you did not plan for. It saves you from silent gaps. - Keep each condition simple and readable. If a check gets long, give it a clear variable name first.
- Reach for
elifwhenever you are picking one result from a list of possibilities, like grades, sizes, or status labels.
🧩 What You’ve Learned
✅ elif (short for “else if”) lets you check several conditions in order.
✅ Python reads top to bottom and stops at the first condition that is true.
✅ Because it stops at the first match, the order of your conditions matters.
✅ Put the strictest condition first when your ranges overlap.
✅ An if / elif / else chain runs only one block, unlike many separate ifs, which each get checked on their own.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
In an if / elif / elif / else chain, how many blocks run?
Why: Python checks top to bottom and runs only the first block whose condition is true, then stops.
- 2
Why does checking `marks >= 50` before `marks >= 90` give the wrong grade for a score of 95?
Why: Since 95 is greater than or equal to 50, the first condition matches and Python stops, never reaching the stricter check.
- 3
What is the correct keyword in Python for an 'else if' check?
Why: Python uses the single keyword `elif`; writing `else if` raises a SyntaxError.
- 4
When should you use several separate `if` statements instead of an elif chain?
Why: Separate `if`s suit independent checks where more than one block may need to run; elif is for picking a single outcome.
🚀 What’s Next?
So far each condition has been a single check. But what if a decision depends on a condition inside another condition? That is where you put one if inside another. And it is the next step.