🐍 Lesson 6: Making Decisions

Teach your code to choose with if / elif / else

← Back to Python Basics

🤔 The if statement

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.