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.