Imagine writing a story where the plot twists depend on the reader's choices. Python's conditional statements let you do something similar: you instruct the computer, “If this happens, do that; otherwise, do something else.” This ability allows your programs to dynamically adapt to different situations.
What are conditional statements?
Conditional statements control the flow of your program by specifying rules for what should happen under different circumstances. Python provides three main tools for this logic:
: Checks a condition.if
(short for "else if"): Adds more conditions to check.elif
: Provides a fallback for when no conditions are met.else
The if
statement: Asking "what if?"
if
The if
if
temperature = 30
if temperature > 25:
print("It's a warm day!")
Explanation:
- Python checks if the temperature is greater than 25.
- If true, it prints: "It's a warm day!"
- If false, it skips the block entirely.
The else
statement: A backup plan
else
The else
if
temperature = 15
if temperature > 25:
print("It's a warm day!")
else:
print("It's a cool day.")
Explanation:
- If the temperature is greater than 25, Python prints: "It's a warm day!"
- Otherwise, it prints: "It's a cool day."
The elif
statement: Adding more choices
elif
The elif
temperature = 20
if temperature > 25:
print("It's a warm day!")
elif temperature > 15:
print("It's a mild day.")
else:
print("It's a cool day.")
Explanation:
- If the temperature is greater than 25, it prints: "It's a warm day!"
- If not, it checks if the temperature is greater than 15. If true, it prints: "It's a mild day."
- If neither condition is true, it prints: "It's a cool day."
How if
, elif
, and else
work together
if
elif
else
- Python checks the
condition first.if
- If the
condition is true, the associated block runs, and Python skips the rest.if
- If the
condition is false, Python evaluates the nextif
condition (if present).elif
- If no conditions are true, the
block (if present) executes.else
Examples of conditional statements
Example 1: Even or odd numbers
number = 7
if number % 2 == 0:
print("It's an even number!")
else:
print("It's an odd number!")
Explanation:
- The program checks if the number is divisible by 2 (i.e., no remainder).
- If true, it’s even; otherwise, it’s odd.
Example 2: Grading system
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
else:
print("D")
Explanation:
- If the grade is 90 or above, it prints: "A".
- If between 80 and 89, it prints: "B".
- If between 70 and 79, it prints: "C".
- If below 70, it prints: "D".
Best practices for conditional statements
Keep conditions clear
Write conditions that are straightforward and easy to understand.
# Good
if age >= 18:
print("Adult")
# Confusing
if not (age < 18):
print("Adult")
Avoid redundant checks
Use elif
# Good
if score >= 90:
print("A")
elif score >= 80:
print("B")
# Inefficient
if score >= 90:
print("A")
if score >= 80 and score < 90:
print("B")
Use descriptive variable names
Choose meaningful names to make your code readable.
temperature = 30 # Clear
t = 30 # Vague
Conclusion
Conditional statements (if
elif
else