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

for, range and random

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.

ProgrammingCambridge Lower Secondary Computing 0860

You will learn to

  • Use for and range() to repeat code a set number of times.
  • Explain why range(5) gives 0, 1, 2, 3 and 4, and stops before 5.
  • Use import random and random.randint() to make a dice program.
  • Create a times table program in four lines or fewer.

Success criteria

  • I can predict the numbers that range(5) and range(1, 11) give.
  • I can make a dice that gives a random whole number from 1 to 6.
  • I can print a times table with a for loop.
  • I can say when to use for and when to use while.

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.

Loops: for

for i in range(5):
    print("Lap", i)

Prints Lap 0 to Lap 4. range(5) gives 0, 1, 2, 3, 4: it starts at 0 and stops before 5.

e.g.Example

Scratch: 'repeat 5'.

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.