Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 10+ · Coding · Python
Loops in Python
1for i in range(5):2 print("wave")3print("bow")
A for loop repeats the pushed-in line. The last line is not pushed in, so it runs once.
The big ideaA for loop repeats the indented lines under it, as many times as range says.

How it works

Typing the same line again and again is slow, and a typo can hide in one of the copies. A loop lets you write the job once.

A for loop repeats the lines pushed in under it. Range says how many turns there are.

Range of five counts zero, one, two, three and four. That is five numbers, so the loop turns five times.

Only lines pushed in belong to the loop. A line that is not pushed in runs just once.

Reach the forlineRun thepushed-in linesTurns left? Goround againThen run thenext line down
What Python does with a for loop.
Pushed in: inside the loop• runs on every turn• once for each number range countsNot pushed in: outside• runs just once• after the loop is finished
Pushed in, or not.

Example 1: Five waves

  1. Ms Ola writes for i in range(5): and puts print("wave") pushed in under it.
  2. Range of five counts 0, 1, 2, 3 and 4.
  3. That is five numbers, so the loop turns five times.
  4. Bo waves 5 times, from one line.

Example 2: Change one number

  1. Mina changes range(5) to range(8) and changes nothing else.
  2. Range of eight makes the loop turn eight times.
  3. Bo waves 8 times. For twenty waves, the number would be 20.

Example 3: A line that is not pushed in

  1. The show starts with print("Show starts"), which is not pushed in, so it runs once.
  2. The for line says range of four, and wave and spin are pushed in, so each runs 4 times.
  3. The Bow line is not pushed in, so it runs once, at the end.
  4. Counting every print: 1 + 4 + 4 + 1 = 10 messages.
1print("Show starts")2for i in range(4):3 print("wave")4 print("spin")5print("Bow")
The show program, line by line.

Example 4: A mix-up to fix

  1. Mina wants a bow just once, but print("bow") is pushed in under range(3).
  2. A pushed-in line is inside the loop, so Bo bows 3 times.
  3. The mix-up to fix: move the bow line back to the edge, so it runs once, after the loop.

Words to know

loop
A way to repeat lines without typing them again.
for
The word that starts a loop.
range
Says how many turns the loop takes.
indent
Pushed in a little, to show a line is inside the loop.
turn
One time round the loop.

Watch out

Try it

  1. How many times does Bo wave? for i in range(3): then print("wave") pushed in
  2. Mina changes range(5) to range(8). How many waves now?
  3. A loop of range(4) has wave and spin pushed in, and print("Bow") after it. How many times does Bow run?
  4. Does range of five start counting at zero or one?
  5. True or false: a line pushed in under for runs only once.
Answers: 1. 3 times · 2. 8 waves · 3. Once · 4. Zero · 5. False
Try at homeAsk a grown-up to be the coder and call out a loop, like repeat six times: clap, then spin. You are the computer, so you do exactly that, and then you swap. Spot loops around the house too, like stairs, the chorus of a song or brushing teeth.
For grown-upsBe the computer together: say a loop such as repeat six times, clap and then spin, and your child does exactly that. Then swap, and spot loops at home, like the chorus of a song or stairs.
Open these notes on screenScan the code, or go to tutorpip.com.