Skip to main content

29. elif and else

1. Introduction

The if statement checks a single condition.
But in real programs, you often need to check multiple possibilities.
That’s where elif (else if) and else come in.


2. The else Clause

else runs when all previous conditions are false.

age = 16

if age >= 18:
print("You are an adult")
else:
print("You are a minor")

Output:

You are a minor

3. The elif Clause

elif lets you check additional conditions.

score = 85

if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")

Output:

Grade: B

4. Multiple elif Clauses

You can chain as many elif clauses as needed, but only one else.

temperature = 25

if temperature > 30:
print("Hot")
elif temperature > 20:
print("Warm")
elif temperature > 10:
print("Cool")
else:
print("Cold")

5. Order of Conditions Matters

Python checks conditions top to bottom.
As soon as one condition is true, the rest are ignored.

x = 15

if x > 10:
print("Greater than 10")
elif x > 5:
print("Greater than 5")

Output:

Greater than 10

6. Nested Conditionals vs elif

Instead of multiple nested if statements, elif makes code cleaner.

❌ Nested:

x = 10
if x >= 0:
if x == 0:
print("Zero")
else:
print("Positive")

✅ Better:

x = 10
if x < 0:
print("Negative")
elif x == 0:
print("Zero")
else:
print("Positive")

7. Using Boolean Operators with if

You can combine conditions using and, or, and not.

age = 20
has_id = True

if age >= 18 and has_id:
print("Access granted")

8. Short-Circuiting

Python stops evaluating conditions as soon as the result is known.

x = 0

# This won't cause ZeroDivisionError
if x != 0 and (10 / x > 1):
print("Condition met")

Here, since x != 0 is False, Python never checks the second condition.


9. Membership Tests in Conditions

You can use in and not in directly.

fruits = ["apple", "banana", "cherry"]

if "apple" in fruits:
print("Apple found")
elif "orange" not in fruits:
print("No orange here")

10. Conditional Expressions (Ternary Operator)

Python has a compact way to write if-else inside an expression:

age = 18
status = "adult" if age >= 18 else "minor"
print(status) # adult

11. Common Beginner Mistakes

  • Forgetting the colon (:):
    if x > 5   # ❌ SyntaxError
  • Using = instead of ==:
    if x = 5:  # ❌ invalid
  • Misordering conditions:
    if x > 0:
    print("Positive")
    elif x > 10: # ❌ unreachable, since >10 is already >0
    print("Greater than 10")

12. Best Practices

  • Keep conditions simple and readable.
  • Avoid deep nesting; prefer elif.
  • Use parentheses in complex conditions for clarity.
  • If multiple conditions lead to the same action, group them with or.

13. Practical Example

marks = 72

if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 50:
grade = "C"
else:
grade = "F"

print("Your grade is", grade)

14. Next Steps

✅ You now understand how to use elif and else, along with advanced condition-handling techniques.
In the next chapter, we’ll look at nested conditionals in more detail.