4. Making decisions with if/else
A condition is a question that Python answers with either True or False. You write conditions using comparison operators:
== equal to (5 == 5 → True)
!= not equal to (5 != 3 → True)
> greater than (10 > 7 → True)
< less than (3 < 1 → False)
>= greater or equal (5 >= 5 → True)
<= less or equal (4 <= 3 → False)
Notice the double equals == for comparison. A single = stores a value (assignment); a double == asks a question ("are these the same?"). Mixing them up is the most common beginner mistake.
Examples with Ethiopian context:
balance = 200 # balance in birr
price = 150
print(balance > price) # True — enough money
print(balance == price) # False — amounts differ
An if statement runs a block of code only when a condition is True. The structure is:
if condition:
# code to run when condition is True
The colon : at the end of the if line is required — it signals that a block of code follows. That block must be indented (4 spaces is standard). Python uses indentation to know which lines belong to the if:
age = 20
if age >= 18:
print("You can open a TeleBirr account.")
print("Thank you for visiting.")
Here, the first print is inside the if (indented) — it only runs when age is 18 or more. The second print is outside the if (not indented) — it always runs.
If age = 15, only "Thank you for visiting." is printed. The indented line is skipped.
An if statement alone only handles the True case. To handle both outcomes — True and False — you add an else:
if condition:
# runs when condition is True
else:
# runs when condition is False
Example — a simple TeleBirr balance check:
balance = 80
price = 150
if balance >= price:
print("Payment approved. Thank you!")
else:
print("Insufficient balance. Please top up.")
Output (because 80 < 150):
Insufficient balance. Please top up.
The else block runs when the if condition is False. Only one of the two blocks ever runs — Python chooses based on the condition.
Change balance to 200 and rerun the program — now the first message appears instead.
Sometimes you need more than two outcomes. elif (short for "else if") lets you check additional conditions in sequence:
if condition1:
# runs if condition1 is True
elif condition2:
# runs if condition1 is False AND condition2 is True
elif condition3:
# runs if condition1 and condition2 are False AND condition3 is True
else:
# runs if ALL conditions above are False
Python checks conditions from top to bottom and stops at the first True one. Only that block runs.
Example — a school grade system used in Addis Ababa:
score = 74
if score >= 90:
print("Grade: A — Excellent!")
elif score >= 80:
print("Grade: B — Very Good")
elif score >= 70:
print("Grade: C — Good")
elif score >= 50:
print("Grade: D — Pass")
else:
print("Grade: F — Fail")
Output:
Grade: C — Good
With score = 74, the first two conditions (>= 90 and >= 80) are False, but >= 70 is True, so "Grade: C" is printed. The remaining elif and else are ignored.
Conditions in Python evaluate to Boolean values — True or False (capital T and F, they are keywords). You can store Boolean values in variables:
is_registered = True
has_balance = False
You can also combine conditions with logical operators:
and — both conditions must be True
or — at least one condition must be True
not — flips True to False and vice versa
Examples:
age = 25
has_id = True
if age >= 18 and has_id:
print("Abebe can open an account.")
discount = True
vip = False
if discount or vip:
print("Special price applies.")
shop_open = False
if not shop_open:
print("The shop is closed. Come back tomorrow.")
Logical operators let you express real-world rules in one clean if statement instead of using confusing nested ifs.
Scenario
Almaz runs a small injera shop in Piassa. She writes a program to decide if she should bake more injera today. She has this code: stock = 30 orders = 45 if stock >= orders: print("Enough injera. No baking needed today.") else: print("Stock is low. Bake more injera!") What does the program print?
Check your understanding
1/7 · 80 XPWhat does the == operator do in Python?