Lesson 46 min read

Conditionals

Teach your code to make decisions like a choose-your-own-adventure book

If, Elif, Else — The Decision Trio

Imagine you're a bouncer at a club. You check a condition ("Are you on the list?") and decide what happens next. That's exactly what if does in Python.

The structure is simple:

  • if — Check the first condition. If it's True, run this block.
  • elif — "Else if" — only checked when the previous condition was False. You can have as many as you want.
  • else — The catch-all. Runs when nothing above was True.

Indentation matters! Python uses 4 spaces (not curly braces) to define code blocks. Everything indented under an if belongs to that branch.

Python evaluates conditions top-to-bottom — only the first True branch runs

Basic Conditionals

temperature = 35
if temperature >= 30:
print("It's hot! Get some ice cream 🍦")
print("Don't forget sunscreen!")
elif temperature >= 20:
print("Nice weather for a walk")
elif temperature >= 10:
print("Better grab a jacket")
else:
print("Stay inside, it's freezing!")
# Only ONE branch runs — the first one that's True
print("\n--- Grade Calculator ---")
score = 87
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Score {score} → Grade {grade}")
Output
It's hot! Get some ice cream 🍦
Don't forget sunscreen!

--- Grade Calculator ---
Score 87 → Grade B

Truthy & Falsy Values

In Python, you don't always need an explicit True or False. Every value has a "truthiness" to it:

Falsy (treated as False):

  • False, None, 0, 0.0
  • Empty things: "", [], {}, (), set()

Truthy (treated as True):

  • Everything else! Non-zero numbers, non-empty strings, non-empty lists, etc.

This is super handy for checking if something is empty or exists.

Truthy/Falsy & Ternary Expressions

# Using truthiness to check for empty values
username = ""
if username:
print(f"Welcome, {username}!")
else:
print("Please enter a username")
# Checking a list
inventory = ["sword", "shield"]
if inventory:
print(f"You have {len(inventory)} items")
else:
print("Your inventory is empty")
# Ternary expression (one-liner if/else)
age = 15
status = "adult" if age >= 18 else "minor"
print(f"Age {age}: {status}")
# Ternary in an f-string
coins = 0
print(f"You have {coins} coin{'s' if coins != 1 else ''}")
Output
Please enter a username
You have 2 items
Age 15: minor
You have 0 coins

Nested Conditions & Combining Logic

You can put if statements inside other if statements — but be careful not to go too deep! If you're nesting more than 2 or 3 levels, it's usually a sign you should refactor with and/or or use a function.

Nested Conditions & Combined Logic

# Amusement park ride checker
age = 12
height = 140 # cm
has_parent = True
# Nested approach (works but gets messy)
if age >= 10:
if height >= 130:
print("You can ride! 🎢")
else:
print("Sorry, too short for this ride")
else:
print("Sorry, too young for this ride")
# Cleaner with 'and' / 'or'
can_ride_alone = age >= 10 and height >= 130
can_ride_with_parent = age >= 6 and has_parent
if can_ride_alone:
print("Hop on!")
elif can_ride_with_parent:
print("You can ride with a parent")
else:
print("Maybe next year!")
Output
You can ride! 🎢
Hop on!
Note: Pro tip: Avoid deep nesting by flipping conditions. Instead of if valid: (big block of code), try if not valid: print("error"); return. This is called a guard clause and it keeps your code flat and readable.

Quick check

Which of these values is truthy in Python?
Strings & FormattingLoops