Programming · Grade 10 · Chapter 7
Python: Conditionals
Teaching programs to make decisions — using if, elif, and else to control which code runs when.
Learning objectives
What We'll Cover
- Understand the
if, elif, else structure
- Write programs with single and multi-branch conditions
- Use nested conditionals appropriately
- Combine conditions with
and, or, not
- Identify logical errors in conditional code
- Apply conditionals to realistic programs: login, grade checker, quiz
The problem
A Quiz That Always Says "Correct!"
A simple quiz program prints "Correct!" after every answer — even wrong ones. The code has no condition checking the answer. Every path leads to the same result.
Discuss: What keyword would you use in real life to say "only do this IF something is true"? What everyday decisions follow this same pattern?
The if statement
Basic if Syntax
The most basic decision: run this code block only if a condition is True.
score = int(input("Enter score: "))
if score >= 60:
print("You passed!")
Critical rule: The colon : after the condition is mandatory. The indented block only runs when the condition is True. If False, nothing happens.
if / else
Adding an Else Branch
else provides a fallback — the code that runs when the condition is False.
score = int(input("Enter score: "))
if score >= 60:
print("You passed!")
else:
print("You need to resit.")
Key concept: Exactly one branch will always execute. The else block has no condition — it simply catches everything not caught by if.
Quick check · 1
If score = 59, what does this program print? if score >= 60: print("Pass") else: print("Fail")
APass
BFail
CBoth Pass and Fail
DNothing — the else never runs
Click to reveal answer
if / elif / else
Multiple Conditions with elif
elif ("else if") adds additional conditions to check, in sequence. Python tests each condition in order and runs the first one that is True.
score = int(input("Score: "))
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
Trace it
Walk Through the Grade Checker
For each score, trace through the conditions in order. Which branch runs, and why?
Class activity: Teacher calls out a score. Students identify which branch runs and why all previous conditions were skipped. Scores to test: 95, 83, 72, 61, 45
Quick check · 2
What does elif allow you to do that if alone cannot?
ASkip the else block
BCheck an additional condition only if all previous conditions were False
CMake the program run faster
DUse two conditions at once
Click to reveal answer
Compound conditions
Combining Conditions with and / or
age = int(input("Age: "))
has_id = input("Have ID? (yes/no): ")
# Both must be true
if age >= 18 and has_id == "yes":
print("Access granted")
else:
print("Access denied")
# Either works
if score > 95 or extra_credit > 10:
print("Distinction!")
Scenario analysis
Real-World Conditional Logic
Scenario: A food delivery app charges a delivery fee of 3.00 unless the order is over 30.00 OR the user has a premium membership.
Write the condition: How would you express this rule in Python? What variables do you need? What is the condition that makes fee = 0?
Nested conditionals
Conditionals Inside Conditionals
A conditional can contain another conditional inside its block. Use sparingly — deeply nested code becomes hard to read.
if age >= 13:
if age >= 18:
print("Adult access")
else:
print("Teen access")
else:
print("Child access")
Often better: Rewrite nested conditions as elif chains where possible — they are easier to read and test.
Live coding · 1
Login System
Build a simple login checker together.
correct_username = "student"
correct_password = "python2026"
username = input("Username: ")
password = input("Password: ")
if username == correct_username and password == correct_password:
print("Login successful! Welcome.")
elif username == correct_username:
print("Wrong password.")
else:
print("Unknown user.")
Quick check · 3
What is the output if age = 15 and has_id = "yes"? if age >= 18 and has_id == "yes": print("Granted") else: print("Denied")
AGranted
BDenied
CError
DBoth Granted and Denied
Click to reveal answer
Explanation: age >= 18 is False (15 < 18). Since we used and, both conditions must be True — so the overall condition is False, and the else branch runs.
Common mistake
= vs == — The Classic Bug
❌ Assignment (Bug!)
if score = 100:
print("Perfect!")
SyntaxError — cannot assign inside a condition.
✅ Comparison (Correct)
if score == 100:
print("Perfect!")
== checks for equality and returns True or False.
Memory trick: = is a box label. == is a question mark: "Are these the same?"
Common mistake 2
Overlapping Conditions — Order Matters
# BUG: This always prints "C or below"
if score >= 60:
print("C or above")
elif score >= 80: # Never reached!
print("B or above")
Fix: Always check the most specific (highest) conditions first. An 85 passes score >= 60 first — so it never reaches score >= 80.
Live coding · 2
BMI Calculator with Categories
Build a program that calculates BMI and categorises the result.
weight = float(input("Weight (kg): "))
height = float(input("Height (m): "))
bmi = weight / (height ** 2)
print(f"BMI: {bmi:.1f}")
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Healthy weight")
elif bmi < 30:
print("Overweight")
else:
print("Obese category")
Quick check · 4
What is wrong with this condition? if score >= 50 or score >= 90: print("High")
ANothing — it is correct
BThe or should be and
CThe score >= 90 check is redundant — anyone with 90+ already satisfies score >= 50
DSyntaxError — cannot use or in an if
Click to reveal answer
Challenge project
Text Adventure: First Room
Create the first room of a text adventure game using conditionals.
print("You stand before two doors: left or right.")
choice = input("Which door? ").lower()
if choice == "left":
print("You find a treasure chest!")
elif choice == "right":
print("A dragon! You flee.")
else:
print("Invalid choice. Try again.")
Extension: Add a second room. Add an inventory system using a variable.
Quick check · 5
Which correctly checks if a number is between 10 and 20 (inclusive)?
Aif 10 < n < 20:
Bif n >= 10 and n <= 20:
Cif n >= 10 or n <= 20:
Dif 10 =< n =< 20:
Click to reveal answer
Note: Python actually does support chaining: 10 <= n <= 20 also works and is very readable. Both B and chained comparisons are valid Python.
Where it appears
Conditional Logic is Everywhere
Every digital system you interact with uses conditional logic constantly.
Login Systems
if username matches AND password matches → grant access
E-Commerce
if order > 50 → free shipping; elif member → 10% off; else → full price
Content Filters
if age < 13 → restrict; elif age < 18 → limit; else → full access
AI Decisions
if confidence > 0.9 → act; elif > 0.7 → ask human; else → decline
Quick check · 6
Predict the output: x = 7 — if x > 5 and x < 10: print("A") elif x > 3: print("B") else: print("C")
Click to reveal answer
Trace: x=7 → 7>5 True AND 7<10 True → both True → prints "A". The elif and else are never evaluated.
Exit reflection
Conditionals in Algorithms
Almost every algorithm in computer science relies on conditional logic.
Think & discuss: You are writing an AI model to recommend whether a patient should be referred to a specialist. What conditions would you include? What would make this dangerous if implemented poorly?
Before you go
Today We Learned...
if runs a block only when a condition is True
elif checks additional conditions in sequence — only if all previous were False
else catches everything not caught above — no condition needed
- Conditions can be combined with
and, or, not
- Order matters: check the most specific conditions first
= assigns, == compares — confusing them is one of the most common beginner bugs
Next chapter: Loops — repeating actions automatically without typing the same code again and again.