← All lessons

Lesson 5 of 18 · about 14 minutes

Making Decisions 🔀

Programs get interesting when they react to data. if lets you run code only when something is true.

temperature = 23
if temperature > 28:
    print("Scorching")
elif temperature > 18:
    print("Lovely")
else:
    print("Bring a coat")

Indentation matters

The indented block (4 spaces) belongs to the if. Python uses indentation instead of curly braces, so keep it consistent.

Comparisons and logic

==   equal        !=   not equal
<  <=  >  >=
and   or   not
"py" in "python"   # membership test
age = 20
has_id = True
if age >= 18 and has_id:
    print("Welcome in")
💡 Truthiness: empty strings, 0, None and empty lists count as False in an if.

Your challenge

Set n = 15. Print FizzBuzz if n is divisible by both 3 and 5, Fizz if only by 3, Buzz if only by 5, otherwise print n. Then do the same for n = 9.