How Python makes decisions and controls program flow
Conditional statements allow your program to make decisions based on conditions. They control what code runs depending on whether something is true or false.
The simplest form of a condition:
age = 18
if age >= 18:
print("You are an adult.")
Use else when you want an alternative action:
temperature = 10
if temperature > 20:
print("It's warm outside.")
else:
print("It's cold outside.")
elif lets you check additional conditions:
score = 75
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: D")
== — equal to!= — not equal to>, < — greater/less than>=, <= — greater/less than or equalCombine conditions using:
and — both conditions must be trueor — at least one condition must be truenot — reverses a conditionage = 25
has_id = True
if age >= 18 and has_id:
print("Access granted.")
You can place conditions inside other conditions:
age = 16
parent_present = True
if age < 18:
if parent_present:
print("Allowed with supervision.")
else:
print("Not allowed.")
Python treats some values as True or False automatically:
0, "", [], {}, None, Falseif "":
print("This won't run.")
if "hello":
print("This will run.")
Now that you understand conditional logic, you’re ready to learn how Python repeats actions using loops in Lesson 7: Loops (for, while).
← Back to Lesson Index