Track: Python for Kids · Lesson 6 of 10 · Ages: 8+ · Time: 40 minutes
Imagine you had to write “I will not feed the robot jelly” a hundred times. Your hand would ache! A computer can do it in two lines, because computers are brilliant at doing the same thing again and again. In lesson 1 you met the for loop. Today you will find out how it really works and use it to draw some beautiful spirals.
Beep says Repeating is my favourite thing! Round and round I go. Beep boop!
What you need
- Python and IDLE.
- A new file called
loops.py.
Grown-ups Loops are one of the biggest ideas in programming. If a child gets stuck, ask them to say out loud what happens on the first time round the loop, then the second. The numbers produced by range start at 0, which feels odd at first. This is normal in programming and shows up again in the lists lesson.
Counting with a loop
for number in range(5):
print(number)
0 1 2 3 4
Surprised? It printed five numbers, but they start at 0 and stop at 4. Computers like to start counting at zero. range(5) makes five numbers: 0, 1, 2, 3 and 4.
The word number here is the loop variable. It is a variable that Python changes for you each time round the loop. First it holds 0, then 1, then 2, and so on. You can choose any name you like. The lines indented underneath are the loop body, and they run once for every number.
You can give range two numbers to choose where to start and where to stop. It starts at the first number and stops just before the second:
for number in range(1, 6):
print(number, "bananas")
1 bananas 2 bananas 3 bananas 4 bananas 5 bananas
You can even give it a third number, which is how big each jump is:
for number in range(2, 11, 2):
print(number)
2 4 6 8 10
Pixel says The stop number is never included. range(1, 6) goes 1, 2, 3, 4, 5. Think of it as a finish line that Python stops in front of, without crossing it. Meow!
Looping over words
A for loop can also walk through a group of things. Here we write the group between square brackets with commas. This is called a list, and you will learn lots more about lists in the next lesson.
for animal in ["cat", "robot", "dragon"]:
print("I love my pet", animal)
I love my pet cat I love my pet robot I love my pet dragon
The loop variable animal becomes "cat" the first time round, then "robot", then "dragon".
A rainbow hexagon
Let us give the turtle a list of colours. Each time round the loop, the pen picks up the next colour. A hexagon has six sides, and we have six colours, so each side gets its own!
import turtle
t = turtle.Turtle()
t.pensize(6)
colours = ["red", "orange", "gold", "green", "blue", "purple"]
for colour in colours:
t.pencolor(colour)
t.forward(100)
t.left(60)
turtle.done()

Each corner turns 60 degrees, because 360 divided by 6 is 60. The first side is red and the last is purple.
A growing spiral
A spiral is a shape that gets bigger as it goes round. How do we make lines that get longer? With a variable that grows, from lesson 3! Every time round the loop, we add a bit to the size variable.
import turtle
t = turtle.Turtle()
t.pencolor("royalblue")
t.pensize(3)
size = 5
for turn in range(40):
t.forward(size)
t.left(90)
size = size + 5
turtle.done()

The first line is 5 steps long, then 10, then 15, and so on, up to 200. After each line the turtle turns left by 90 degrees, so the spiral winds outwards in square corners.

A rainbow spiral
Now put the two ideas together. We will loop through the colours three times, and make the lines longer each time. A loop inside a loop is called a nested loop. The outer loop repeats three times, and the inner loop goes through the six colours.
import turtle
t = turtle.Turtle()
t.pensize(4)
colours = ["red", "orange", "gold", "green", "blue", "purple"]
size = 10
for round_number in range(3):
for colour in colours:
t.pencolor(colour)
t.forward(size)
t.left(60)
size = size + 8
turtle.done()

That is 3 times 6, which makes 18 lines. Each turn is 60 degrees, so the spiral has six-sided corners.
Did you know? Spirals are everywhere in nature: in snail shells, in the middle of sunflowers and in the shape of some galaxies. Computers can draw them using a simple loop, just like you did.
Challenges
Try it In the square spiral, change t.left(90) to t.left(95). Does the spiral still look square? Try 89 or 120 too.
Show the answer
With 95 the corners lean a little further each time, so the lines cross over each other and make a swirly pattern a bit like a rose, instead of a neat square spiral. With 120 you get a triangle spiral.
import turtle
t = turtle.Turtle()
t.pencolor("deeppink")
t.pensize(3)
size = 5
for turn in range(40):
t.forward(size)
t.left(95)
size = size + 5
turtle.done()

Try it Write a loop that prints the numbers 1 to 10, each on its own line, followed by the words “Beep boop!” after the loop.
Show a hint
range(1, 11) gives 1 up to 10. The line that prints “Beep boop!” should not be indented, so it only runs once, after the loop finishes.
Show the answer
for number in range(1, 11):
print(number)
print("Beep boop!")
1 2 3 4 5 6 7 8 9 10 Beep boop!
Try it Use a loop to print the 3 times table up to 3 times 5. The output should look like 3 x 1 = 3.
Show the answer
for number in range(1, 6):
print("3 x", number, "=", 3 * number)
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15
What you learned
- A
forloop repeats the lines inside it. The indented lines are the loop body. range(5)gives 0, 1, 2, 3, 4, andrange(1, 6)gives 1 to 5. The stop number is never included.- The loop variable changes every time round the loop.
- A loop can walk through a list of colours or words.
- A variable that grows inside a loop makes a spiral.
- A loop inside a loop is called a nested loop.
