Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 9 · Decisions and loops

For loops

A for loop lets you do something many times while writing it only once. for n in range(3) repeats the indented lines under it once for each number 0, 1 and 2, and changing 3 to 100 gives a hundred repeats from the same two lines.

Posted · About a 1 minute watch, 4 minute read · Take the quiz

One instruction, repeated

How do you do something a hundred times without writing it a hundred times? You could type the same line a hundred times, but that is slow, and changing it later would be awful. Instead you use a loop.

A loop is like a coach saying "run three laps". The coach doesn't say "run a lap, run a lap, run a lap". It's one instruction, and the runner repeats it.

for n in range(3)

Here is a for loop in Python:

``python for n in range(3): print("Lap", n) print("Done!") ``

`` Lap 0 Lap 1 Lap 2 Done! ``

Read it piece by piece:

  • range(3) gives the numbers 0, 1 and 2. It starts at 0 and stops before the number you give, so you get exactly three numbers.
  • for n in range(3): means "for each of those numbers, call it n and do the lines below". Don't forget the colon at the end.
  • The indented line underneath, print("Lap", n), runs once for each number. Each time round, n holds the current number: first 0, then 1, then 2. print("Lap", n) prints its two values with a space between them, so you see Lap 0, Lap 1, Lap 2.

When the loop ends

After the last number, the loop is finished, and Python moves on to the next line that isn't indented: print("Done!"). That line is not part of the loop, so it runs only once.

Indentation is how Python knows which lines belong to the loop, just like the indented lines under an if last episode. Indent by four spaces.

Why does it start at 0?

Python starts counting at zero, so range(3) gives 0, 1, 2 and not 1, 2, 3. It feels odd at first, but you still get three numbers. Why programmers count from zero is a story for a later episode.

A hundred laps, same two lines

Here is the power of a loop. Change 3 to 100:

``python for n in range(100): print("Lap", n) print("Done!") ``

Now it prints 100 lines, Lap 0 up to Lap 99, and then Done!. The loop is still just two lines.

Try it yourself

Open any free online Python editor (search "online Python") and run:

``python for n in range(4): print("Step", n) print("Finished") ``

You should see:

`` Step 0 Step 1 Step 2 Step 3 Finished ``

Now change 4 to 10. Before you run it, predict the last Step line. Then indent print("Finished") by four spaces and predict what changes.

Good to know

  • Every snippet on this page was run with Python 3 and printed exactly what is shown.
  • The indented lines under the for are called the loop body. A body can have more than one line; every indented line runs each time round.
  • range can start somewhere else: range(1, 4) gives 1, 2, 3 (it still stops before the end number).
  • A for loop can go through more than numbers. It walks through the items of any sequence, one at a time. For example, a piece of text:

``python for letter in "hi": print(letter) ``

`` h i ``

  • n is just a name. You can call the loop variable anything, like lap or i; i is a very common choice.
  • Next episode: while loops, which repeat as long as something stays true, instead of a set number of times.

Answer to the comment question

What does this print?

``python total = 0 for n in range(4): total = total + n print(total) ``

It prints 6. range(4) gives 0, 1, 2 and 3 (it starts at zero and stops before four). total starts at 0, and each time round the loop the current n is added to it: 0 + 0 = 0, then 0 + 1 = 1, then 1 + 2 = 3, then 3 + 3 = 6. print(total) isn't indented, so it runs once, after the loop, and prints 6.

Test your knowledge

Quick quiz: For loops

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What numbers does range(3) give?
2How does Python know which lines are inside the loop?
3In for n in range(3):, what does n hold?
4How many lines does this print? for n in range(100): print("Lap", n)

Sources

Video transcript

How do you do something a hundred times without writing it a hundred times?

You use a loop. It's like a coach saying: run three laps. One instruction, repeated.

In Python, we type for n in range of three, colon. Range of three gives the numbers zero, one and two.

The indented line underneath runs once for each number, and n holds the current one. So it prints Lap zero, Lap one, Lap two.

Then the loop ends, and Python moves on to the line that isn't indented: Done!

Why zero first? Python starts counting at zero. More on that later.

Change three to a hundred, and you get a hundred laps from the same two lines.

What does this print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons