Python if, elif and else Statements with Real Examples for Beginners
P5.3 Python elif: Checking Many Conditions
Why. Real rules have more than two outcomes: grades A/B/C, delivery bands, risk levels.
What. elif means "else if". Python checks from top to bottom and runs only the first True block.
How.
Write a chain
- Start with the most specific (highest) check.
- Add
eliffor each next band. - End with
elsefor everything left. - Test with a value from each band.
Worked example A — Delivery speed bands.
Step 1 — What we do. A courier in Manila labels deliveries.
Step 2 — Type and run this code.
minutes = 25
if minutes <= 15:
band = "Super fast"
elif minutes <= 30:
band = "Fast"
elif minutes <= 60:
band = "Normal"
else:
band = "Slow"
print(band)
Step 3 — Output you should see.
Fast
Step 4 — What it means. 25 is not <= 15, but it is <= 30, so "Fast". Python stops after the first match.
Worked example B — Order matters.
Step 1 — What we do. We write the same rule in the wrong order to see the problem.
Step 2 — Type and run this code.
minutes = 10
if minutes <= 60:
band = "Normal"
elif minutes <= 15:
band = "Super fast"
else:
band = "Slow"
print(band)
Step 3 — Output you should see.
Normal
Step 4 — What it means. 10 minutes should be "Super fast" but we got "Normal", because the wide check came first. Always order from narrow to wide.
Worked example C — Loan risk from a score.
Step 1 — What we do. A bank in Lagos rates customers by credit score.
Step 2 — Type and run this code.
score = 710
if score >= 750:
risk = "Low"
elif score >= 650:
risk = "Medium"
else:
risk = "High"
print(f"Score {score}: {risk} risk")
Step 3 — Output you should see.
Score 710: Medium risk
Step 4 — What it means. 710 falls in the 650–749 band, so Medium.
Ravindra Bagale's Tip
Write your bands in a small table on paper first (from, to, label). Then the elif chain writes itself.
Ravindra Bagale's Tip – मराठी
आधी कागदावर bands चा छोटा table लिहा (पासून, पर्यंत, label). मग elif chain आपोआप लिहिली जाते.
Ravindra Bagale's Tip – हिंदी
पहले कागज़ पर bands की छोटी table लिखो (से, तक, label). फिर elif chain अपने आप लिख जाती है.