PYTHON BASICS • LESSON 12
Conditional Statements — if, elif and else
Learn how Python makes decisions using conditions. You will work with if, elif, and else and apply them to real-world data analytics situations.
1. What is a Conditional Statement?
A conditional statement allows Python to make a decision based on whether a condition is True or False.
age = 20
if age >= 18:
print("Adult")Python checks the condition age >= 18. If it is True, the indented code runs.
2. The if Statement
The if statement executes a block of code only when its condition is True.
sales = 120000
if sales > 100000:
print("Target achieved")Notice the colon : after the condition and the indentation before print().
CHECK YOUR UNDERSTANDING
What happens when an if condition is False?
3. Predict the Output
marks = 75
if marks >= 50:
print("Pass")CHECK YOUR UNDERSTANDING
What will this program print?
4. The else Statement
Use else when you want Python to execute another block when the if condition is False.
marks = 42
if marks >= 50:
print("Pass")
else:
print("Fail")CHECK YOUR UNDERSTANDING
What will be printed if marks = 42?
Complete the statement: if age >= 18 ___
5. The elif Statement
Use elif when you need to check another condition after the previous condition was False.
marks = 82
if marks >= 90:
print("Excellent")
elif marks >= 75:
print("Very Good")
else:
print("Keep Practicing")CHECK YOUR UNDERSTANDING
What will the code print when marks = 82?
6. Multiple Conditions
Conditional statements become especially useful when combined with comparison and logical operators.
sales = 150000
profit = 30000
if sales >= 100000 and profit >= 25000:
print("Strong Performance")
else:
print("Review Performance")CHECK YOUR UNDERSTANDING
What will this program print?
7. Nested if Statements
An if statement can be placed inside another if statement. This is called a nested conditional.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Access Allowed")
else:
print("ID Required")
else:
print("Underage")CHECK YOUR UNDERSTANDING
What will the nested if example print?
8. Conditional Logic in Data Analytics
Analysts frequently use conditions to classify records, identify targets, flag unusual values, and create business categories.
revenue = 125000
if revenue >= 100000:
status = "High"
else:
status = "Low"
print(status)Here, Python converts a numerical value into a business category.
CHECK YOUR UNDERSTANDING
Why are conditional statements useful in data analytics?
9. Predict the Output
revenue = 80000
if revenue >= 100000:
print("High")
elif revenue >= 50000:
print("Medium")
else:
print("Low")CHECK YOUR UNDERSTANDING
What will be printed?
10. Debugging Conditional Code
A common beginner mistake is forgetting the colon or using the assignment operator instead of a comparison operator.
score = 80
if score = 80:
print("Good")The condition should use ==, not =.
score = 80
if score == 80:
print("Good")CHECK YOUR UNDERSTANDING
Which operator should compare whether score is exactly 80?
11. Complete the Code
Complete: if temperature > 30 ___
Complete the comparison: if sales ___ 100000:
Complete the alternative branch keyword: ___:
12. Practice Challenge
Suppose a company gives a bonus when an employee's sales are at least ₹100,000.
sales = 120000
if sales >= 100000:
print("Bonus Eligible")
else:
print("Not Eligible")CHECK YOUR UNDERSTANDING
What will be printed?
13. Mini Challenge
Build a condition that classifies a customer's purchase:
- ₹50,000 or more → Premium
- ₹20,000 or more → Regular
- Below ₹20,000 → Basic
purchase = 35000
if purchase >= 50000:
category = "Premium"
elif purchase >= 20000:
category = "Regular"
else:
category = "Basic"
print(category)CHECK YOUR UNDERSTANDING
What category will a purchase of ₹35,000 receive?
14. Final Challenge
A sales analyst wants to classify monthly performance:
- Revenue ≥ ₹200,000 → Excellent
- Revenue ≥ ₹100,000 → Good
- Revenue ≥ ₹50,000 → Average
- Otherwise → Needs Improvement
revenue = 135000
if revenue >= 200000:
performance = "Excellent"
elif revenue >= 100000:
performance = "Good"
elif revenue >= 50000:
performance = "Average"
else:
performance = "Needs Improvement"
print(performance)CHECK YOUR UNDERSTANDING
What is the performance classification for revenue = ₹135,000?
Lesson 12 Recap
- ✓
ifchecks a condition. - ✓
elsehandles the alternative. - ✓
elifallows additional conditions. - ✓ Conditions return True or False.
- ✓
and,or, andnotcan combine conditions. - ✓ Conditional logic is widely used for data classification and business rules.
- ✓ Remember the difference between
=and==.
Next: Lesson 13 — for Loops