An if statement runs some code only when a condition is true. If the condition is false, that code is skipped.
age = 10
if age >= 8:
print("You can ride the roller coaster! 🎢")
📌 Two things to remember
1. End the if line with a colon:
2. Indent the code inside with 4 spaces. Indentation is how Python knows what belongs to the if!
⚖️ Comparison operators
Conditions usually compare two values. They give back True or False:
a == b # equal to (two equals signs!)
a != b # not equal to
a > b # greater than
a < b # less than
a >= b # greater than or equal
a <= b # less than or equal
One = vs two ==
=stores a value (x = 5). ==compares two values (x == 5). Mixing them up is the #1 beginner bug!
🔀 else — the backup plan
Add else to run different code when the condition is false:
temperature = 15
if temperature > 20:
print("Wear a t-shirt 👕")
else:
print("Bring a jacket 🧥")
🪜 elif — checking many cases
Use elif (short for "else if") to check several conditions in order. Python runs the first one that's true and skips the rest.
score = 85
if score >= 90:
print("Grade: A 🌟")
elif score >= 80:
print("Grade: B 😊")
elif score >= 70:
print("Grade: C 🙂")
else:
print("Keep practicing! 💪")
🔗 Combining conditions: and, or, not
age = 12
has_ticket = True
if age >= 10 and has_ticket:
print("Enjoy the movie! 🍿")
if age < 5 or age > 65:
print("You get a discount!")
and — both must be true
or — at least one must be true
not — flips true to false
✅ What you learned
if runs code when a condition is true.
else handles the false case; elif checks more cases.
Use a colon and indentation.
Compare with == != > < >= <=; combine with and, or, not.
🎮 Time to Practice!
Give your programs a brain. Mind your colons and indentation!
🔢
Task 1: Big or Small?
Easy
The variable num is 50. Write an if that prints Big number! when num is greater than 10.
num = 50
if num > 10:
print("Big number!")
⚖️
Task 2: Even or Odd?
Medium
The variable n is 8. Print Even if it divides evenly by 2, otherwise print Odd. (Hint: use n % 2 == 0.)
n = 8
if n % 2 == 0:
print("Even")
else:
print("Odd")
🎓
Task 3: Grade Machine
Tricky
A test score is 85. Print the letter grade using if/elif/else: 90+ → A, 80–89 → B, 70–79 → C, otherwise → F. For 85 it should print B.
Ask the user their height in cm. If it's 120 or more, let them on the ride; otherwise, tell them to grow a bit more. Try different heights!
🏋️ Extra Practice — Drill Time!
Decisions are the brain of a program. The more if statements you write, the more natural they feel — so here is a whole workout.
⚖️
Task 5: Which Is Bigger?
Easy
With a = 14 and b = 9, print a is bigger, b is bigger or They are equal — whichever is true. Then change the numbers and run again to test the other branches!
a is bigger
a = 14
b = 9
if a > b:
print("a is bigger")
elif b > a:
print("b is bigger")
else:
print("They are equal")
➕
Task 6: Positive, Negative or Zero
Easy
With n = -7, print positive, negative or zero. Use one if, one elif and one else.
negative
n = -7
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")
🎢
Task 7: Two Rules at Once (and)
Medium
A ride needs riders to be at least 8 years old and at least 120 cm tall. With age = 10 and height = 115, print You may ride! or Sorry, not yet. using and.
Sorry, not yet.
age = 10
height = 115
if age >= 8 and height >= 120:
print("You may ride!")
else:
print("Sorry, not yet.")
🎟️
Task 8: Either Rule Works (or)
Medium
Entry is free if you are under 5 or over 65. With age = 70, print Free entry! or Please pay $10 using or.
Free entry!
age = 70
if age < 5 or age > 65:
print("Free entry!")
else:
print("Please pay $10")
🚫
Task 9: Flip It With not
Medium
With is_raining = False, use if not is_raining: to print Go outside! ☀️, otherwise print Stay in and code. 🌧️.
Go outside! ☀️
is_raining = False
if not is_raining:
print("Go outside! ☀️")
else:
print("Stay in and code. 🌧️")
🔍
Task 10: Is It In There?
Medium
The word in also asks questions. With password = "dragon123", print Has a number if "1" is in it, and print Long enough if it has 8 or more characters. Two separate if statements — both should print here.
Has a number
Long enough
password = "dragon123"
if "1" in password:
print("Has a number")
if len(password) >= 8:
print("Long enough")
🎫
Task 11: Ticket Price Machine
Tricky
Ticket prices: under 5 is free, 5–12 costs $8, 13–64 costs $15, 65 and over costs $10. With age = 13, print just the price line, like Price: $15. Test it by changing age to 3, 9 and 70 too!
Price: $15
age = 13
if age < 5:
price = 0
elif age <= 12:
price = 8
elif age <= 64:
price = 15
else:
price = 10
print(f"Price: ${price}")
🪆
Task 12: An if Inside an if
Tricky
With logged_in = True and is_admin = False: if the user is logged in, then check whether they are an admin — print Welcome, boss! for an admin, Welcome, user! for a normal one. If they are not logged in at all, print Please log in.
Welcome, user!
logged_in = True
is_admin = False
if logged_in:
if is_admin:
print("Welcome, boss!")
else:
print("Welcome, user!")
else:
print("Please log in.")
👑
Task 13: Boss Level: Rock Paper Scissors Judge
Boss
With player = "rock" and computer = "scissors", decide the winner and print exactly one of Player wins!, Computer wins! or It's a tie!. Remember: rock beats scissors, scissors beats paper, paper beats rock. Try all the combinations!
Player wins!
player = "rock"
computer = "scissors"
if player == computer:
print("It's a tie!")
elif player == "rock" and computer == "scissors":
print("Player wins!")
elif player == "scissors" and computer == "paper":
print("Player wins!")
elif player == "paper" and computer == "rock":
print("Player wins!")
else:
print("Computer wins!")
🧙
Task 14: Free Play — Choose Your Adventure
Creative
Write a short story where variables decide what happens: has_key, coins, brave… Use at least three if statements so the ending changes when you change the variables. Then change them and run it again! 🗝️