Conditional Statements in Python
Introduction
Conditional statements allow executing different code blocks based on certain conditions. Python uses if, elif, and else for flow control.
The if Statement
Executes a code block only if a condition is True:
age = 18
if age >= 18:
print("You are an adult")
Indentation
Important: Python uses indentation (spaces) to delimit code blocks. Standard is 4 spaces.
if condition:
# This block executes if condition is True
statement1
statement2
# Here the if block ends
The if-else Statement
Executes one block if condition is True, another if False:
age = 16
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
The if-elif-else Statement
For checking multiple conditions:
grade = 8
if grade >= 9:
print("Excellent")
elif grade >= 7:
print("Good")
elif grade >= 5:
print("Satisfactory")
else:
print("Unsatisfactory")
Order matters!
Conditions are evaluated top to bottom. The first true condition is executed:
x = 15
# Wrong - first condition is always True for x > 10
if x > 5:
print("Greater than 5") # Always executes
elif x > 10:
print("Greater than 10") # Never reached
# Correct - order from specific to general
if x > 10:
print("Greater than 10") # Executes
elif x > 5:
print("Greater than 5")
Complex Conditions
Use logical operators for multiple conditions:
age = 25
has_license = True
# AND - both conditions must be True
if age >= 18 and has_license:
print("Can drive")
# OR - at least one condition True
grade = 4
if grade >= 5 or grade == 4:
print("Accepted")
# NOT - negates the condition
is_raining = False
if not is_raining:
print("We can go outside")
Nested If Statements
An if can be inside another if:
age = 20
has_license = True
if age >= 18:
if has_license:
print("Can drive")
else:
print("Needs to get license")
else:
print("Too young for license")
Conditional Expression (Ternary Operator)
A compact form for conditional assignments:
# Syntax: value_true if condition else value_false
age = 20
status = "adult" if age >= 18 else "minor"
print(status) # adult
# Equivalent to:
if age >= 18:
status = "adult"
else:
status = "minor"
Practical examples:
# Absolute value
x = -5
abs_x = x if x >= 0 else -x # 5
# Minimum of two numbers
a, b = 10, 5
minimum = a if a < b else b # 5
# Parity
n = 7
parity = "even" if n % 2 == 0 else "odd" # odd
The pass Statement
pass is a placeholder for empty blocks:
x = 10
if x > 5:
pass # TODO: implement later
else:
print("x is small")
“Truthy” and “Falsy” Values
In conditions, the following evaluate as False:
FalseNone0,0.0""(empty string)[],(),{}(empty collections)
All other values are True:
lst = [1, 2, 3]
# Check if list is not empty
if lst:
print("List has elements")
# Better than:
if len(lst) > 0:
print("List has elements")
Comparing with None
Use is for comparing with None:
result = None
# Correct
if result is None:
print("No result")
# Works but not recommended
if result == None:
print("No result")
Common Mistakes
1. Forgetting the colon
if x > 5 # SyntaxError: expected ':'
print("Big")
if x > 5: # Correct
print("Big")
2. Incorrect indentation
if x > 5:
print("Big") # IndentationError
if x > 5:
print("Big") # Correct
3. Using = instead of ==
if x = 5: # SyntaxError
print("Five")
if x == 5: # Correct
print("Five")
4. Non-mutually exclusive conditions
# Both can execute
if x > 5:
print("Big")
if x > 3:
print("This too") # Also executes!
# Only one executes
if x > 5:
print("Big")
elif x > 3:
print("Medium")
Key Points for Exam
- Indentation defines blocks (4 spaces standard)
- Conditions are evaluated top to bottom
- First true condition stops evaluation
- Ternary operator:
val_true if cond else val_false - “Falsy” values:
False,None,0,"",[],{} - Use
is None, not== None
Review Questions
- What does
print("Yes" if 0 else "No")display? - How many elif clauses can you have in an if?
- What happens if you forget
:after if? - What does it mean that an empty list is “falsy”?