elif and else
When there are more than two possibilities — elif chains let you handle them all cleanly, and else catches everything left over.
"Life rarely has just two choices. elif gives your program the same flexibility your brain already has."
— ShurAIThe Problem with Just if...else
Imagine you are building a ticket machine. The price depends on age — child, teen, adult, or senior. With only if and else, you get a messy chain:
# ❌ Ugly — deeply nested, hard to read
if age < 5:
print("Free")
else:
if age < 12:
print("Rs.80")
else:
if age < 60:
print("Rs.200")
else:
print("Rs.120")
The same logic using elif is flat, clean, and easy to read at a glance:
# ✅ Clean — flat structure, instantly readable
if age < 5:
print("Free")
elif age < 12:
print("Rs.80")
elif age < 60:
print("Rs.200")
else:
print("Rs.120")
How Python Walks Through elif
Python checks conditions from top to bottom. The moment one is True, it runs that block and jumps out. Everything below is ignored:
Example 1 — Weather Advice 🌤️
temp = int(input("Temperature today (°C): "))
if temp >= 40:
print("🔥 Extreme heat! Stay at home.")
elif temp >= 30:
print("☀️ Hot day. Carry water.")
elif temp >= 20:
print("😊 Perfect weather. Enjoy!")
elif temp >= 10:
print("🧥 Bit chilly. Wear a jacket.")
else:
print("🥶 Very cold! Dress in layers.")
Example 2 — ATM Withdrawal 🏧
balance = 5000
amount = int(input("How much to withdraw? Rs. "))
if amount <= 0:
print("❌ Invalid amount. Enter a positive number.")
elif amount % 100 != 0:
print("❌ Please enter a multiple of 100.")
elif amount > 10000:
print("❌ Maximum withdrawal is Rs.10,000 per day.")
elif amount > balance:
print(f"❌ Insufficient funds. Balance: Rs.{balance}")
else:
balance = balance - amount
print(f"✅ Dispensing Rs.{amount}. Remaining: Rs.{balance}")
Put the most specific condition first, widest last. In the ATM example, invalid amounts are caught before checking the balance. If you put amount > balance first, the validation logic would be skipped. When writing elif chains, always ask: "What should I check first?"
Example 3 — Season Finder 📅
month = int(input("Enter month number (1-12): "))
if month == 12 or month <= 2:
print("❄️ Winter — cold and cosy")
elif month <= 5:
print("🌸 Spring — flowers bloom")
elif month <= 8:
print("☀️ Summer — hot and sunny")
elif month <= 11:
print("🍂 Autumn — leaves falling")
else:
print("⚠️ Invalid month. Enter 1 to 12.")
else — Your Safety Net
else is optional but powerful. It runs when none of the conditions above it matched. It is the perfect place to handle unexpected or invalid input:
direction = input("Enter direction (N/S/E/W): ").upper()
if direction == "N":
print("⬆️ Heading North")
elif direction == "S":
print("⬇️ Heading South")
elif direction == "E":
print("➡️ Heading East")
elif direction == "W":
print("⬅️ Heading West")
else:
# catches everything else — typos, wrong input
print(f"'{direction}' is not a valid direction.")
Real Example — Mobile Data Plan Selector 📱
print("=== 📱 Data Plan Recommender ===")
usage = int(input("How much data do you use per day (GB)? "))
if usage <= 0:
print("🤔 You barely use data!")
print("Recommended: Basic Plan — Rs.99/month (1 GB/day)")
elif usage <= 1:
print("📧 Light user — calls and messages")
print("Recommended: Starter Plan — Rs.149/month (1.5 GB/day)")
elif usage <= 2:
print("📱 Regular user — social media and browsing")
print("Recommended: Smart Plan — Rs.249/month (2 GB/day)")
elif usage <= 5:
print("🎬 Heavy user — streaming and gaming")
print("Recommended: Power Plan — Rs.449/month (2 GB/day + extras)")
else:
print("🚀 Super heavy user! You need unlimited.")
print("Recommended: Unlimited Plan — Rs.699/month")
=== 📱 Data Plan Recommender ===
How much data do you use per day (GB)? 3
🎬 Heavy user — streaming and gaming
Recommended: Power Plan — Rs.449/month (2 GB/day + extras)
"elif is just a tidier way to write nested ifs. Flat is better than nested — your future self will thank you."
— ShurAI🧠 Quiz — Question 1
What does elif stand for?
🧠 Quiz — Question 2
Given score = 85, what prints?if score >= 90: print("A")
elif score >= 80: print("B")
elif score >= 70: print("C")
else: print("D")
🧠 Quiz — Question 3
When does the else block run?
🧠 Quiz — Question 4
Is else required at the end of an elif chain?