Skip to the page
Year 8 · Spring 2 · Python I

if / elif / else

PracticalPresent ▶

What this lesson covers

Topic 13 Programming basics

KS3 Computing · A program stores information in variables, works on it with operators, and controls the order with sequence, selection and iteration. Examples are in Python, with Scratch alongside for Year 7.

Programming Computational ThinkingCambridge Lower Secondary Computing 0860

You will learn to

  • Use the comparison operators ==, !=, >, <, >= and <=.
  • Use if, elif and else, with a colon and indentation, to choose one path.
  • Explain why the order of the conditions matters.
  • Use PRIMM to predict the output of a program before you run it.

Success criteria

  • I can write a grade program: 90 and over is an A, and so on down.
  • I can predict the output of five short programs before I run them.
  • I can explain what goes wrong if the lowest grade is checked first.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Variable
A named place in memory that holds a value.
Assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Casting
Changing a value from one data type to another, like int("13").
Concatenation
Joining strings together: "7" + "7" = "77".
Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.
Boolean
A value that is only True or False.
Infinite loop
A loop whose condition never becomes false.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Selection: if, elif, else

score = int(input("Score out of 50: "))
if score >= 40:
    print("Gold badge!")
elif score >= 25:
    print("Bronze badge")
else:
    print("Try the Respawn quest")
  1. Check the if. True? Run its lines and skip the rest.
  2. False? Check the elif.
  3. Nothing true? Run the else.
!Watch out

Order matters. If >= 25 came first, a score of 45 would wrongly get Bronze.

★Remember

Indentation (4 spaces) shows which lines are inside the if. The colon : is required.

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.