🐍 Python for beginners · session 4 of 57 · 60 minutes
Making decisions: if, elif and else
Programs can choose. With if, elif and else a program checks something and does different things depending on the answer. Practise each kind of decision before building a grade machine, a bill calculator and a game judge.
A complete lesson · 60 minutes
Teach it, see it, practise it, all on this page.
Open this page with your learner and work down it, or press Teach this lesson to go through it one step at a time on a big screen. Every section has something to do.
- Warm-up4 min
- True or False6 min
- if and else8 min
- elif8 min
- and, or, not7 min
- Grade machine7 min
- Electricity bill8 min
- Rock, paper, scissors7 min
- Quiz4 min
- Set the project1 min
By the end you can
- Compare values with == != < > <= >=
- Write if and else with correct indentation
- Choose between many cases with elif
- Combine conditions with and, or and not
Before you start
- Session 3: input() and converting to numbers
Have ready
- This page
- Cards that say YES and NO (optional)
Beginner · ages 10 and up
Warm-up · think first
A ride says: "You must be at least 120 cm tall." Write the rule as a yes/no question the gate machine asks, and what it does for each answer.
Show the answer
Is height at least 120? Yes: open the gate. No: "Sorry, not yet". Every decision is a yes/no question with an action for each answer.
Teacher tip: Act it out with different heights; ask what happens at exactly 120.
Part 1 of 4 · learn, see, practise
Questions with True or False answers
A comparison gives True or False. == asks "equal?", != "not equal?", and < > <= >= compare sizes.
One = stores a value; two == ask a question.
Output
True False True False
Memory boxes
125intChange height to 120 and predict the first two lines.
Practise it
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
1.1 x = 15. Print whether x is greater than 10, then whether x equals 15.
Output
(nothing is printed)
Memory boxes
15intModel answer (for the teacher)
x = 15
print(x > 10)
print(x == 15)1.2 a = 7 and b = 7. Print whether they are equal, then whether they are different.
Output
(nothing is printed)
Memory boxes
7intb7intModel answer (for the teacher)
a = 7
b = 7
print(a == b)
print(a != b)1.3 n = 24. Print whether n is even, then whether n is a multiple of 3.
Output
(nothing is printed)
Memory boxes
24intModel answer (for the teacher)
n = 24
print(n % 2 == 0)
print(n % 3 == 0)Part 2 of 4 · learn, see, practise
if and else
if condition: runs the indented lines only when the condition is True; else: runs when it is False.
The colon and the indentation (4 spaces) show which lines belong to the if.
Output
Sorry, not yet. Next person, please.
Memory boxes
115intThe last line is not indented, so it always runs.
Practise it
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
2.1 temp = 30. Print Hot if temp is above 25, otherwise Not hot.
Output
(nothing is printed)
Memory boxes
30intModel answer (for the teacher)
temp = 30
if temp > 25:
print("Hot")
else:
print("Not hot")2.2 n = 7. Print even or odd.
Output
(nothing is printed)
Memory boxes
7intModel answer (for the teacher)
n = 7
if n % 2 == 0:
print("even")
else:
print("odd")2.3 pin = 4821. Print Access granted if pin is 4821, otherwise Wrong PIN, then always print Goodbye.
Output
(nothing is printed)
Memory boxes
4821intModel answer (for the teacher)
pin = 4821
if pin == 4821:
print("Access granted")
else:
print("Wrong PIN")
print("Goodbye")Part 3 of 4 · learn, see, practise
Many choices with elif
elif adds more cases between if and else. Python checks from the top and runs only the first True one.
Put the strictest condition first.
Output
Warm
Memory boxes
31int31 is not >= 35, but it is >= 25, so Warm, and the rest is skipped.
Practise it
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
3.1 speed = 45. Print Slow below 30, Normal below 60, otherwise Fast.
Output
(nothing is printed)
Memory boxes
45intModel answer (for the teacher)
speed = 45
if speed < 30:
print("Slow")
elif speed < 60:
print("Normal")
else:
print("Fast")3.2 n = -4. Print positive, negative or zero.
Output
(nothing is printed)
Memory boxes
-4intModel answer (for the teacher)
n = -4
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")3.3 Ticket price by age: under 5 free (Price: 0), under 13 Price: 100, 60 or over Price: 120, everyone else Price: 200.
Output
(nothing is printed)
Memory boxes
30intModel answer (for the teacher)
age = 30
if age < 5:
print("Price: 0")
elif age < 13:
print("Price: 100")
elif age >= 60:
print("Price: 120")
else:
print("Price: 200")Part 4 of 4 · learn, see, practise
and, or, not
and is True only when both sides are True; or when at least one is; not flips True and False.
Output
You can ride alone
Memory boxes
10intheight125intChange age to 6, then height to 110.
Practise it
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
4.1 day = "Sunday". Print Holiday! on Saturday or Sunday, otherwise School day.
Output
(nothing is printed)
Memory boxes
'Sunday'strModel answer (for the teacher)
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Holiday!")
else:
print("School day")4.2 Print Ride if height is at least 120 and age at least 8, otherwise No ride.
Output
(nothing is printed)
Memory boxes
130intage9intModel answer (for the teacher)
height = 130
age = 9
if height >= 120 and age >= 8:
print("Ride")
else:
print("No ride")4.3 Leap year, full rule: divisible by 4 and not by 100, or divisible by 400. Print leap or not leap.
Output
(nothing is printed)
Memory boxes
2024intModel answer (for the teacher)
y = 2024
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
print("leap")
else:
print("not leap")Watch out
Common mistakes
One = instead of two
if score = 10: is an error; asking needs ==.
Forgetting the colon
Every if, elif and else line ends with :.
Wrong indentation
Lines inside an if are indented by the same amount.
Conditions in the wrong order
With if marks >= 50 first, a 95 never reaches >= 90.
Class activity 1 of 3
The grade machine
Print Grade: A for 90 or more, B for 75 to 89, C for 50 to 74 and D below 50. It will be tested with many marks.
- Write if marks >= 90: and print Grade: A.
- Add elif for B (75 or more).
- Add elif for C (50 or more).
- Add else for D.
- Test 90, 75, 50 and 49 yourself.
Output
(nothing is printed)
Memory boxes
82intHint
Biggest first: >= 90, then >= 75, then >= 50.
Model answer (for the teacher)
marks = 82
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: D")Class activity 2 of 3
Electricity bill
The first 100 units cost 5 each, the next 100 cost 7 each and every unit above 200 costs 10. Print Bill: and the amount. Test with 80, 150, 200 and 250 units.
- If units <= 100: bill = units × 5.
- elif units <= 200: bill = 500 + (units − 100) × 7.
- else: bill = 500 + 700 + (units − 200) × 10.
- Print Bill: and the bill.
Output
(nothing is printed)
Memory boxes
150intHint
For 250 units: 100 × 5 + 100 × 7 + 50 × 10.
Model answer (for the teacher)
units = 150
if units <= 100:
bill = units * 5
elif units <= 200:
bill = 500 + (units - 100) * 7
else:
bill = 500 + 700 + (units - 200) * 10
print("Bill:", bill)Class activity 3 of 3
Rock, paper, scissors judge
p1 and p2 hold rock, paper or scissors. Print Draw, Player 1 wins or Player 2 wins. Rock beats scissors, scissors beat paper, paper beats rock.
- If p1 == p2 print Draw.
- elif p1 beats p2 (three cases joined with or) print Player 1 wins.
- else print Player 2 wins.
- Test all nine pairs if you can.
Output
(nothing is printed)
Memory boxes
'rock'strp2'scissors'strHint
Put each winning pair in brackets: (p1 == "rock" and p2 == "scissors").
Model answer (for the teacher)
p1 = "rock"
p2 = "scissors"
if p1 == p2:
print("Draw")
elif (p1 == "rock" and p2 == "scissors") or (p1 == "scissors" and p2 == "paper") or (p1 == "paper" and p2 == "rock"):
print("Player 1 wins")
else:
print("Player 2 wins")Quiz · 8 questions
Check what you learned
Pick an answer to see if it is right and why. Your best score is saved on this device.
After class · project
My quiz game
Make a three-question quiz that keeps score and ends with a message chosen by if/elif/else.
What to make: A three-question quiz with score and a final message.
Stretch: Accept any capitals with answer.lower() == "new delhi".
How it is marked
| Look for | Done well when |
|---|---|
| Questions | Three questions, each checked |
| Score | Goes up only for right answers |
| Final message | Different for different scores |
| Friendly | Clear questions and replies |
Starter code to take home
Press Copy and paste it into the editor at home, or work on it right here.
Output
Q1: What is the capital of India? Line 4: EOFError: EOF when reading a line (there is no more input; add lines to the Input box)
Memory boxes
0intHomework
Practise at home
Each one checks itself. Or print the practice sheet.
1. year = 2024. Print leap if it is divisible by 4, else not leap (simple rule).
Output
(nothing is printed)
Memory boxes
2024int2. a, b and c are three numbers. Print the largest.
Output
(nothing is printed)
Memory boxes
4intb9intc2int3. Three sides of a triangle. Print equilateral, isosceles or scalene (or not a triangle if one side is at least the sum of the other two).
Output
(nothing is printed)
Memory boxes
3intb4intc5intFor parents and teachers
The 60-minute lesson plan
Use it as it is, or stretch any part over two sessions. Each row links to the part of the page you need.
| Time | Part | The teacher | The learner |
|---|---|---|---|
| 0–4 min | Warm-up | Theme-park gate. | Say the rule. |
| 4–10 min | True or False | Predict each line. | Even/multiple of 3. |
| 10–18 min | if and else | Point at colon and indentation. | PIN stretch. |
| 18–26 min | elif | Why order matters. | Ticket stretch. |
| 26–33 min | and, or, not | YES/NO cards. | Leap-year stretch. |
| 33–40 min | Grade machine | Check the 90 boundary. | Pass all checks. |
| 40–48 min | Electricity bill | Work 250 units on paper first. | Pass all checks. |
| 48–55 min | Rock, paper, scissors | Draw a who-beats-whom triangle. | Pass all checks. |
| 55–59 min | Quiz | Run the quiz. | Answer. |
| 59–60 min | Set the project | Open the quiz-game starter. | Pick a topic. |
Questions to ask
- What is the difference between = and ==?
- If two conditions in a chain are True, which runs?
- When would you use or instead of and?
If it feels hard
Draw each if/elif/else as a flowchart before coding.
If it feels easy
Sort three numbers into order using only if statements.
FAQ
Making decisions: if, elif and else: questions
What is the difference between if, elif and else in Python?
if checks first, elif checks more conditions when earlier ones were False, and else runs when none were True.
Why does Python use indentation?
It shows which lines belong to an if, elif or else.
What is == in Python?
It compares two values and gives True or False; one = stores a value.
Start a conversation
Want a teacher to take this class?
Tell us the learner's age and level. A teacher from our network teaches with these lessons; we confirm timing and price first.
- Tell us the learner’s age or grade and goals
- We’ll suggest a suitable class or learning path
- We’ll confirm format, timing and fees with a parent or guardian