Repeating with for loops

Introduction

A for loop runs the same lines once for every value in a sequence. range() makes a sequence of numbers for it.

Computers are valuable because they repeat work without getting tired or careless. With a loop, a program that handles three items can handle three million with no extra code.

How it works

Your first loop

for i in range(3):
    print(i)
Open in compiler

range(3) gives the numbers 0, 1 and 2, so the loop body runs three times. Step through it and watch i: each time the for line runs, i gets its next value. When there are no values left, the loop ends and Python moves past it.

The indented line (print(i)) is the body of the loop. Indentation, 4 spaces, is how Python knows which lines belong to the loop.

Using the loop variable

The loop variable can take part in any calculation. This prints the multiplication table of 3:

n = 3
for i in range(1, 6):
    print(n, "x", i, "=", n * i)
Open in compiler

Syntax

for i in range(5):         # 0, 1, 2, 3, 4
    print(i)
for i in range(1, 6):      # 1, 2, 3, 4, 5
    print(i)
for i in range(0, 10, 2):  # 0, 2, 4, 6, 8
    print(i)

Example

range(1, 5) starts at 1 and stops before 5, so it gives 1, 2, 3, 4. A loop like this can build up a total:

total = 0
for i in range(1, 5):
    total = total + i
print(total)
Open in compiler

Step through line 3 four times. The calculation box shows each update: 0 + 1, then 1 + 2, 3 + 3, and 6 + 4. The final total is 10.

Line by line

  1. total = 0 starts the total, before the loop begins.
  2. for i in range(1, 5): gives i the values 1, 2, 3 and 4.
  3. total = total + i adds the current i to the total.
  4. When there are no values left, the loop ends.
  5. print(total) is not indented, so it runs once, after the loop.

Expected result

10

In the real world

A loop adds up the prices in a shopping cart, sends a message to each member of a group, and draws each row of a table on a page. Most of the work computers do happens inside loops.

Common mistakes

  • Off by one: range(1, 5) stops at 4, not 5. To include 5, write range(1, 6).
  • Forgetting the colon : at the end of the for line gives a SyntaxError.
  • A line that is not indented is not part of the loop, so it runs only once, after the loop ends.

Best practices

  • Use range(1, n + 1) when you need to count from 1 to n.
  • Choose a clear name when the variable means something, such as row.
  • Keep the code before and after a loop outside its indentation.

Summary

A for loop runs its body once per value. range makes numbers and stops before its last value. A total is started before the loop and added to inside it.

Practice

Write a loop that prints the squares of 1 to 5 (1, 4, 9, 16, 25). Step through it and check that the loop variable and the output match on every step.

Quick check

1. Which numbers does range(2, 6) give?
2. How many times does the body of for i in range(4): run?
3. After the sum example finishes, what is i?

Key takeaways

  • range stops before its last number.
  • Indentation decides what is inside the loop.
  • A total starts at 0, outside the loop.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.