Python elif Statement
Table of Contents
In the last lesson, we learned about the Python if-else Statement. Now let’s handle situations with several possible outcomes.
What If We Have More Than Two Choices?
In the last article, we learned about if-else. It gives us two paths. One for true. One for false.
But life is not always that simple.
Think about grades.
- 90 or above: A
- 80 to 89: B
- 70 to 79: C
- 60 to 69: D
- Below 60: F
That is five possibilities. Not two.
How do we handle that in Python?
That is where elif comes in.
What Is elif in Python?
elif is short for “else if.”
It lets us check multiple conditions. One after another. Until one of them is true.
Think of it like a series of questions.
- Is it A? No.
- Is it B? No.
- Is it C? Yes! Do this.
- Stop checking.
That is how elif works.
The Structure of if-elif-else in Python
if condition1: # do this if condition1 is trueelif condition2: # do this if condition1 is false and condition2 is trueelif condition3: # do this if condition1 and condition2 are false and condition3 is trueelse: # do this if all conditions are falseWe can have as many elif as we want. And the else at the end is optional.
Let me show us a real example.
A Simple Python elif Example
Let’s check if a number is positive, negative, or zero.
number = 0
if number > 0: print("The number is positive")elif number < 0: print("The number is negative")else: print("The number is zero")Output
The number is zeroLet me explain what happened.
- First, it checked
number > 0. Is 0 greater than 0? No. So it skipped. - Then it checked
number < 0. Is 0 less than 0? No. So it skipped. - Then it went to
else. Since no other condition was true, it ran the else block.
Now if number was 5:
number = 5
if number > 0: print("The number is positive")elif number < 0: print("The number is negative")else: print("The number is zero")Output
The number is positiveBecause 5 > 0 is True. So the first block ran. And Python skipped the rest.
That is the key thing about elif. Once one condition is true, Python stops checking the rest.
Python elif: A Real-Life Example — Grades
Let’s use the grade example.
score = 85
if score >= 90: print("Grade: A")elif score >= 80: print("Grade: B")elif score >= 70: print("Grade: C")elif score >= 60: print("Grade: D")else: print("Grade: F")Output
Grade: BBecause 85 is not >= 90. But it is >= 80. So it stopped there. And printed Grade B.
Now if score was 95:
score = 95Output
Grade: ABecause 95 >= 90 is True. So it stopped at the first check.
And if score was 45:
score = 45Output
Grade: FBecause none of the conditions were true. So the else block ran.
Order Matters in Python elif
This is very important.
The order of our conditions matters. Python checks them from top to bottom. And it stops at the first true one.
Look at this:
score = 85
if score >= 60: print("Grade: D")elif score >= 70: print("Grade: C")elif score >= 80: print("Grade: B")elif score >= 90: print("Grade: A")else: print("Grade: F")What will this print?
If we said “Grade: D”, we are right.
Because 85 >= 60 is True. So Python stops there. It never checks the other conditions.
But that is wrong. 85 should be a B, not a D.
So we must put the highest condition first. Or the most specific condition first.
Correct order:
if score >= 90: print("Grade: A")elif score >= 80: print("Grade: B")elif score >= 70: print("Grade: C")elif score >= 60: print("Grade: D")else: print("Grade: F")Now it works correctly.
Let’s always think about the order. Put the strictest condition first.
Python elif: Another Example — Days of the Week
day = 3
if day == 1: print("Monday")elif day == 2: print("Tuesday")elif day == 3: print("Wednesday")elif day == 4: print("Thursday")elif day == 5: print("Friday")elif day == 6: print("Saturday")elif day == 7: print("Sunday")else: print("Invalid day number")Output
WednesdayIf day was 10, output would be:
Invalid day numberBecause none of the conditions matched. So the else block ran.
Python elif with input()
Let’s build a simple age classifier.
age = int(input("Enter your age: "))
if age < 0: print("Invalid age")elif age < 13: print("You are a child")elif age < 20: print("You are a teenager")elif age < 60: print("You are an adult")else: print("You are a senior")If we enter 25, output is:
You are an adultIf we enter 8, output is:
You are a childIf we enter -5, output is:
Invalid ageSee how each condition handles a different range? That is the power of elif.
We Do Not Always Need else
The else block is optional. We can have just if and elif.
score = 75
if score >= 90: print("Excellent")elif score >= 70: print("Good")elif score >= 50: print("Average")If score is 30, nothing prints. Because none of the conditions are true. And there is no else.
That is fine. Sometimes we do not need a fallback.
How Many elif Can We Have?
As many as we want.
We can have one if. Then ten elif. Then one else.
But if we have too many, our code becomes hard to read. In that case, we might want to use a different approach. We will learn about that later.
For now, let’s just know that elif can be repeated as many times as we need.
Common Mistakes with Python elif
Mistake 1: Writing else if Instead of elif
if score >= 90: print("A")else if score >= 80: print("B")Error! Python does not use else if. It uses elif.
So what do we do? We use elif.
elif score >= 80:Mistake 2: Wrong Order
if score >= 60: print("D")elif score >= 90: print("A")This will never print “A”. Because if score is 95, it matches >= 60 first. And stops.
So what do we do? We put the highest condition first.
if score >= 90: print("A")elif score >= 60: print("D")Mistake 3: Forgetting the Colon
elif score >= 80 print("B")Error! Missing colon.
So what do we do? We add the colon.
elif score >= 80: print("B")Mistake 4: Forgetting Indentation
elif score >= 80:print("B")Error! The print line needs indentation.
So what do we do? We indent it.
elif score >= 80: print("B")A Complete Python elif Example
Let’s build a simple calculator that handles different operations.
num1 = float(input("Enter first number: "))num2 = float(input("Enter second number: "))operation = input("Enter operation (+, -, *, /): ")
if operation == "+": result = num1 + num2 print(f"Result: {result}")elif operation == "-": result = num1 - num2 print(f"Result: {result}")elif operation == "*": result = num1 * num2 print(f"Result: {result}")elif operation == "/": if num2 == 0: print("Cannot divide by zero") else: result = num1 / num2 print(f"Result: {result}")else: print("Invalid operation")This uses if, elif, else, and even a nested if inside one of the elif blocks.
Let’s run it with different inputs. See how it behaves.
Next, we will learn about Python Nested Conditions and place one decision inside another.