Phase: 2. Control Flow & Data | Estimated time: 2 hours | Milestone Project: No
- Write
ifstatements to execute code conditionally - Use
elifandelseto handle multiple branches - Properly indent code blocks
- Nest conditionals inside each other
- Use truthiness directly in conditions
- Check for
None, empty strings, and other common patterns
Conditional statements are how programs make decisions. Without them, code runs the same way every time. With if/elif/else, your program can react to user input, validate data, and handle different scenarios — the essence of intelligent behavior.
An if statement runs a block of code only when its condition is truthy:
age = 18
if age >= 18:
print("You are an adult.")
print("You can vote.")
# Output: You are an adult.
# Output: You can vote.The condition is any expression that Python evaluates for truthiness. The indented block (4 spaces by convention) runs only if the condition is True.
age = 16
if age >= 18:
print("You are an adult.") # This does NOT run
print("This runs either way.")
# Output: This runs either way.Add else to run code when the condition is falsy:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
# Output: You are a minor.Use elif (short for "else if") for multiple conditions:
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Grade: {grade}")
# Output: Grade: BOnly the first matching branch runs. Once a condition is True, Python skips the remaining elif and else blocks.
Python uses indentation (4 spaces recommended) to define code blocks. Consistent indentation is mandatory:
x = 10
if x > 5:
print("x is greater than 5") # 4 spaces
if x > 8:
print("x is also > 8") # 8 spaces (nested)
print("Outside the if block") # 0 spacesYou don't need if x == True:. Just use if x::
name = input("Enter your name: ") # User types "" (empty)
if name:
print(f"Hello, {name}!")
else:
print("No name entered.")
# Output: No name entered.The idiomatic way to check for None:
result = None
if result is None:
print("No result yet.")
else:
print(f"Result: {result}")
# Output: No result yet.Use is None rather than == None. The is operator checks identity, which is the correct way to compare to None.
text = ""
if not text:
print("String is empty.")
# Output: String is empty.You can put if statements inside other if statements:
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive.")
else:
print("Get a license first.")
else:
print("Too young to drive.")
# Output: You can drive.-
Forgetting the colon — Every
if,elif, andelseline must end with a colon (:). -
Inconsistent indentation — Mixing tabs and spaces, or using the wrong number of spaces, causes
IndentationError. -
Using assignment
=instead of comparison==— Python 3.12 allows=in conditions, but it's almost certainly a bug if you meant==. -
Putting too many conditions in
elif— Remember that once a condition matches, laterelifbranches are skipped, even if they would also match. -
Empty
ifblock — Python won't let you leave a block empty. Usepassas a placeholder:
if condition:
pass # I'll fill this in laterLet's build a simple number guessing game using conditionals:
import random
secret = random.randint(1, 10)
guess = int(input("Guess a number 1-10: ")) # Assume user types 7
if guess == secret:
print("Perfect! You guessed it!")
elif guess > secret:
print("Too high! Try again.")
else:
print("Too low! Try again.")
print(f"The secret number was {secret}.")Now a temperature checker with nested conditions:
temp = float(input("Enter temperature in °C: ")) # Assume 30
if temp > 30:
print("It's hot outside!")
elif temp > 20:
print("It's warm and pleasant.")
elif temp > 10:
print("It's a bit cool.")
elif temp > 0:
print("It's cold!")
else:
print("It's freezing! Stay inside.")ifruns a block when its condition is truthy;elseprovides the fallbackelifadds additional conditions — only the first matching branch executes- Indentation (4 spaces) defines blocks; all lines in a block must be indented consistently
- Use
if x:for truthiness checks instead ofif x == True: - Use
if x is None:to check forNone(identity, not equality) - Use
if not x:to check for empty strings, empty collections, or zero values - Nested conditionals let you check multiple layers of conditions
- Every
if,elif,elseline must end with a colon
- Python docs: More Control Flow Tools — if statements
- PEP 8 — Indentation guidelines
- Real Python: Conditional Statements in Python
Module 013: Comparison Chaining & Logical Operators Deep Dive — Master chained comparisons like 1 < x < 10, understand is vs ==, and learn De Morgan's laws.