Book a free demo

🐍 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.

🖨 Practice sheet

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.

See the teacher's plan
  1. Warm-up4 min
  2. True or False6 min
  3. if and else8 min
  4. elif8 min
  5. and, or, not7 min
  6. Grade machine7 min
  7. Electricity bill8 min
  8. Rock, paper, scissors7 min
  9. Quiz4 min
  10. 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.

Example: run it, then change it

Output

True
False
True
False

Memory boxes

height125int

Change 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.

Warm-up

1.1 x = 15. Print whether x is greater than 10, then whether x equals 15.

Task 1.1

Output

(nothing is printed)

Memory boxes

x15int
Model answer (for the teacher)
x = 15
print(x > 10)
print(x == 15)
Core

1.2 a = 7 and b = 7. Print whether they are equal, then whether they are different.

Task 1.2

Output

(nothing is printed)

Memory boxes

a7intb7int
Model answer (for the teacher)
a = 7
b = 7
print(a == b)
print(a != b)
Stretch 🔥

1.3 n = 24. Print whether n is even, then whether n is a multiple of 3.

Task 1.3

Output

(nothing is printed)

Memory boxes

n24int
Model 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.

Example: run it, then change it

Output

Sorry, not yet.
Next person, please.

Memory boxes

height115int

The 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.

Warm-up

2.1 temp = 30. Print Hot if temp is above 25, otherwise Not hot.

Task 2.1

Output

(nothing is printed)

Memory boxes

temp30int
Model answer (for the teacher)
temp = 30
if temp > 25:
    print("Hot")
else:
    print("Not hot")
Core

2.2 n = 7. Print even or odd.

Task 2.2

Output

(nothing is printed)

Memory boxes

n7int
Model answer (for the teacher)
n = 7
if n % 2 == 0:
    print("even")
else:
    print("odd")
Stretch 🔥

2.3 pin = 4821. Print Access granted if pin is 4821, otherwise Wrong PIN, then always print Goodbye.

Task 2.3

Output

(nothing is printed)

Memory boxes

pin4821int
Model 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.

Example: run it, then change it

Output

Warm

Memory boxes

temp31int

31 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.

Warm-up

3.1 speed = 45. Print Slow below 30, Normal below 60, otherwise Fast.

Task 3.1

Output

(nothing is printed)

Memory boxes

speed45int
Model answer (for the teacher)
speed = 45
if speed < 30:
    print("Slow")
elif speed < 60:
    print("Normal")
else:
    print("Fast")
Core

3.2 n = -4. Print positive, negative or zero.

Task 3.2

Output

(nothing is printed)

Memory boxes

n-4int
Model answer (for the teacher)
n = -4
if n > 0:
    print("positive")
elif n < 0:
    print("negative")
else:
    print("zero")
Stretch 🔥

3.3 Ticket price by age: under 5 free (Price: 0), under 13 Price: 100, 60 or over Price: 120, everyone else Price: 200.

Task 3.3

Output

(nothing is printed)

Memory boxes

age30int
Model 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.

Example: run it, then change it

Output

You can ride alone

Memory boxes

age10intheight125int

Change 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.

Warm-up

4.1 day = "Sunday". Print Holiday! on Saturday or Sunday, otherwise School day.

Task 4.1

Output

(nothing is printed)

Memory boxes

day'Sunday'str
Model answer (for the teacher)
day = "Sunday"
if day == "Saturday" or day == "Sunday":
    print("Holiday!")
else:
    print("School day")
Core

4.2 Print Ride if height is at least 120 and age at least 8, otherwise No ride.

Task 4.2

Output

(nothing is printed)

Memory boxes

height130intage9int
Model answer (for the teacher)
height = 130
age = 9
if height >= 120 and age >= 8:
    print("Ride")
else:
    print("No ride")
Stretch 🔥

4.3 Leap year, full rule: divisible by 4 and not by 100, or divisible by 400. Print leap or not leap.

Task 4.3

Output

(nothing is printed)

Memory boxes

y2024int
Model 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.

  1. Write if marks >= 90: and print Grade: A.
  2. Add elif for B (75 or more).
  3. Add elif for C (50 or more).
  4. Add else for D.
  5. Test 90, 75, 50 and 49 yourself.
Your code

Output

(nothing is printed)

Memory boxes

marks82int
Hint

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.

  1. If units <= 100: bill = units × 5.
  2. elif units <= 200: bill = 500 + (units − 100) × 7.
  3. else: bill = 500 + 700 + (units − 200) × 10.
  4. Print Bill: and the bill.
Your code

Output

(nothing is printed)

Memory boxes

units150int
Hint

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.

  1. If p1 == p2 print Draw.
  2. elif p1 beats p2 (three cases joined with or) print Player 1 wins.
  3. else print Player 2 wins.
  4. Test all nine pairs if you can.
Your code

Output

(nothing is printed)

Memory boxes

p1'rock'strp2'scissors'str
Hint

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.

1What does this show?
print(5 > 3)
Show the answer

True. 5 is greater than 3.

2What does this show?
x = 7
if x % 2 == 0:
    print("even")
else:
    print("odd")
Show the answer

odd. 7 % 2 is 1.

3What does this show?
t = 20
if t > 30:
    print("hot")
elif t > 15:
    print("warm")
else:
    print("cold")
Show the answer

warm. Only the first True branch runs.

4What does this show?
print(True and False)
Show the answer

False. and needs both.

5What does this show?
print(not (3 == 3))
Show the answer

False. not flips True.

6Which line correctly asks whether score is 100?
Show the answer

if score == 100:. == and a colon.

7What does this show?
age = 15
if age >= 13 or age == 9:
    print("yes")
else:
    print("no")
Show the answer

yes. or needs one side.

8What does this show?
n = 12
r = ""
if n > 10:
    r = r + "A"
if n > 5:
    r = r + "B"
print(r)
Show the answer

AB. Two separate ifs, not an elif: both are checked, so both letters are added.

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 forDone well when
QuestionsThree questions, each checked
ScoreGoes up only for right answers
Final messageDifferent for different scores
FriendlyClear questions and replies

Starter code to take home

Press Copy and paste it into the editor at home, or work on it right here.

Project starter

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

score0int

Homework

Practise at home

Each one checks itself. Or print the practice sheet.

Warm-up

1. year = 2024. Print leap if it is divisible by 4, else not leap (simple rule).

Homework 1

Output

(nothing is printed)

Memory boxes

year2024int
Core

2. a, b and c are three numbers. Print the largest.

Homework 2

Output

(nothing is printed)

Memory boxes

a4intb9intc2int
Stretch 🔥

3. 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).

Homework 3

Output

(nothing is printed)

Memory boxes

a3intb4intc5int

For 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.

TimePartThe teacherThe learner
0–4 minWarm-upTheme-park gate.Say the rule.
4–10 minTrue or FalsePredict each line.Even/multiple of 3.
10–18 minif and elsePoint at colon and indentation.PIN stretch.
18–26 minelifWhy order matters.Ticket stretch.
26–33 minand, or, notYES/NO cards.Leap-year stretch.
33–40 minGrade machineCheck the 90 boundary.Pass all checks.
40–48 minElectricity billWork 250 units on paper first.Pass all checks.
48–55 minRock, paper, scissorsDraw a who-beats-whom triangle.Pass all checks.
55–59 minQuizRun the quiz.Answer.
59–60 minSet the projectOpen 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

For a child’s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

We’ll use your details only to respond to this request.

Chat on WhatsApp
Esc

Try