6 min read
Control Flow - Conditional Statements
Learn to make decisions in your code with if, elif, and else
What You'll Learn
- Making decisions with if
- Multiple choices with elif
- Default option with else
- Nested conditions
What are Conditional Statements?
They let your code make choices, like:
- "If it's raining, take an umbrella"
- "If age is over 18, allow entry"
- "If password is correct, log in"
The if Statement
Basic format:
code.py
if condition:
# do thisExample:
code.py
age = 20
if age >= 18:
print("You can vote!")Important: Notice the indentation (spaces)! Python needs it.
if-else Statement
Do one thing or another:
code.py
age = 16
if age >= 18:
print("You can vote!")
else:
print("Too young to vote")Output: Too young to vote
if-elif-else Statement
Multiple choices:
code.py
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")Output: Grade: B
Practice Examples
Example 1: Age check
code.py
age = 25
if age < 13:
print("You are a child")
elif age < 18:
print("You are a teenager")
elif age < 65:
print("You are an adult")
else:
print("You are a senior")Example 2: Password check
code.py
password = "secret123"
user_input = "secret123"
if user_input == password:
print("Access granted!")
else:
print("Wrong password!")Example 3: Temperature
code.py
temp = 75
if temp > 80:
print("It's hot!")
elif temp > 60:
print("It's nice!")
else:
print("It's cold!")Multiple Conditions
Use and and or:
Example: Club entry
code.py
age = 25
has_id = True
if age >= 21 and has_id:
print("Welcome to the club!")
else:
print("Sorry, can't enter")Example: Weekend check
code.py
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
else:
print("It's a weekday")Nested if Statements
if inside if:
code.py
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive!")
else:
print("You need a license")
else:
print("You're too young")Comparing Numbers
code.py
num1 = 10
num2 = 5
if num1 > num2:
print("num1 is bigger")
elif num1 < num2:
print("num2 is bigger")
else:
print("They are equal")Checking Text
code.py
name = "John"
if name == "John":
print("Hello John!")
else:
print("You are not John")Case sensitive:
code.py
name = "john"
if name == "John":
print("Hello!") # Won't print (lowercase j)Better way:
code.py
name = "john"
if name.lower() == "john":
print("Hello!") # Will print!Real-World Examples
Example 1: Discount calculator
code.py
price = 100
is_member = True
if is_member:
discount = price * 0.2 # 20% off
final_price = price - discount
print("Member price:", final_price)
else:
print("Regular price:", price)Example 2: Login system
code.py
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Login successful!")
else:
print("Invalid credentials")Example 3: Grade calculator
code.py
score = 75
if score >= 90:
grade = "A"
message = "Excellent!"
elif score >= 80:
grade = "B"
message = "Great job!"
elif score >= 70:
grade = "C"
message = "Good!"
elif score >= 60:
grade = "D"
message = "Passed"
else:
grade = "F"
message = "Failed"
print("Grade:", grade)
print(message)Common Mistakes
Mistake 1: Forgetting colon (:)
code.py
# Wrong
if age > 18
print("Adult")
# Correct
if age > 18:
print("Adult")Mistake 2: Wrong indentation
code.py
# Wrong (no spaces)
if age > 18:
print("Adult")
# Correct (4 spaces)
if age > 18:
print("Adult")Mistake 3: Using = instead of ==
code.py
# Wrong (assigns value)
if age = 18:
# Correct (compares)
if age == 18:Mistake 4: No else after elif
code.py
# This is OK
if score >= 90:
print("A")
elif score >= 80:
print("B")
# else is optional!Short if Statements
One-line version:
code.py
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status) # AdultSame as:
code.py
if age >= 18:
status = "Adult"
else:
status = "Minor"Quick Reference
Basic if:
code.py
if condition:
# do thisif-else:
code.py
if condition:
# do this
else:
# do thatif-elif-else:
code.py
if condition1:
# do this
elif condition2:
# do that
else:
# do defaultSummary
ifmakes decisionselifadds more choiceselseis the default option- Always use colon (:)
- Always indent (4 spaces)
- Use
and/orfor multiple conditions
What's Next?
Now let's learn how to repeat code with for loops!