Conditional statements are one of the most important building blocks of any programming language, including Python. They allow a program to make decisions and perform different actions based on different inputs or conditions.
🌟 What Are Conditional Statements?
- Conditional statements control the flow of a program.
- They let the program check if something is true, and if so, do something in response.
- This is like asking a question and taking action based on the answer.
Real-life example:
If it’s raining, take an umbrella.
Else, wear sunglasses.
✅ The if Statement
The if statement is used to test a condition. If the condition is true, the block of code under it runs. If not, it is skipped.
🧠 Syntax:
pythonCopyEditif condition:
# Code to run if the condition is true
🧪 Example 1:
pythonCopyEdittemperature = 30
if temperature > 25:
print("It's hot outside.") # This line runs because the condition is true
🧪 Example 2:
pythonCopyEditscore = 85
if score >= 90:
print("You got an A!") # This line does not run because the condition is false
🔁 The elif Statement
elif stands for else if. It lets you check another condition if the first if is false. You can have multiple elif blocks.
🧠 Syntax:
pythonCopyEditif condition1:
# Code runs if condition1 is true
elif condition2:
# Runs if condition1 is false and condition2 is true
🧪 Example:
pythonCopyEditscore = 75
if score >= 90:
print("You got an A!")
elif score >= 80:
print("You got a B!")
elif score >= 70:
print("You got a C!") # This line runs
🧍 The else Statement
The else block runs only if none of the if or elif conditions are true. Think of it as a “fallback”.
🧠 Syntax:
pythonCopyEditif condition1:
# Do something
elif condition2:
# Do something else
else:
# Do this if none of the above conditions were true
🧪 Example:
pythonCopyEditscore = 60
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("You need to study more.") # This line runs
🧩 Full Example with if, elif, and else
pythonCopyEditage = 16
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.") # This line runs
else:
print("You are a child.")
- The program checks each condition in order.
- Only the first true condition runs. The rest are skipped.
🔍 Python Indentation Rules
In Python, indentation is mandatory. It shows which lines of code belong to the same block.
✅ Correct:
pythonCopyEditif x > 10:
print("x is big")
❌ Incorrect (will cause an error):
pythonCopyEditif x > 10:
print("x is big") # Missing indentation!
Use 4 spaces or a Tab for indentation. Be consistent!
⚠️ Common Mistakes and Misconceptions
- ❌ Forgetting the colon
:at the end ofif,elif, orelselines. pythonCopyEditif x > 10 # Error! Missing colon - ❌ Incorrect indentation leads to
IndentationError. - ❌ Using
elifwithoutif–elifalways follows anif. - ❌ Using
elsewith a condition –elseshould never have a condition.
🧠 Real-Life Analogies
- Restaurant Menu Decision:
- If you’re vegetarian, order the veggie burger.
- Else if you’re vegan, get the salad.
- Else, order a steak.
- Morning Routine:
- If it’s Monday, wake up early.
- Else if it’s a weekend, sleep in.
- Else, follow a normal schedule.
- Clothing Based on Weather:
- If it’s cold, wear a jacket.
- Else if it’s warm, wear a T-shirt.
- Else, dress normally.
These help visualize how decisions flow based on conditions.
📝 Practice Problems
Try these to test your understanding!
1. Age Classifier
pythonCopyEdit# Write a program that prints:
# "Child" if age is less than 13
# "Teen" if age is between 13 and 17
# "Adult" if age is 18 or older
age = 15
# Your code here
2. Grade Checker
pythonCopyEdit# Given a variable score, print:
# "A" for 90+
# "B" for 80–89
# "C" for 70–79
# "D" for 60–69
# "F" for below 60
score = 73
# Your code here
3. Even or Odd
pythonCopyEdit# Write a program that checks if a number is even or odd.
number = 42
# Your code here
🎓 Final Thoughts
Understanding if, elif, and else is essential for writing code that makes decisions. Once you master this, you’ll be able to build smarter, more interactive programs. Keep practicing, and try to relate code logic to real-life situations—that’s the best way to make it stick!