Last updated
Python If...Else
Definition: An if statement runs a block of code only when a condition is True. With elif and else you can choose between several outcomes.
Example 1 — a simple if
age = 20
if age >= 18:
print("You are an adult")
Note the colon : and the indentation — both are required.
Example 2 — if...else
else runs when the condition is False:
age = 15
if age >= 18:
print("Adult")
else:
print("Minor")
Example 3 — elif for multiple paths
Python checks each condition in order and runs the first True one:
score = 75
if score >= 90:
print("Grade A")
elif score >= 70:
print("Grade B")
elif score >= 50:
print("Grade C")
else:
print("Fail")
Example 4 — combining conditions
age = 25
if age > 18 and age < 65:
print("Working age")
💡 Try this: change score in the editor and run it — watch a different branch fire each time.
Try it Yourself
Output
Ad · responsive