Python / Loops

Repeating with for

Repeat code a set number of times with for and range(), and control the start, stop and step. Baccalaureate

Printing a word ten times with ten print lines is tiring, and impossible for a thousand. A for loop repeats the indented lines under it as many times as you say:

Python
for count in range(5):
    print("warm")
Console
warm
warm
warm
warm
warm

The pattern is for variable in range(number): with a colon, and the repeated lines are indented 4 spaces, just like if. A line after the loop with no indent runs once, after the loop ends.

Python
for i in range(3):
    print("Hello")
print("Done")
Console
Hello
Hello
Hello
Done

The loop variable

range(5) produces the numbers 0 to 4 and hands them to the loop variable one at a time. You can print it or use it in a calculation:

Python
for i in range(5):
    print(i)
Console
0
1
2
3
4
Counting starts at 0 and stops before the number you give: range(5) does not include 5, but it still repeats exactly 5 times.

Start, stop and step

Give range two numbers to set the start and stop, or three numbers to also set the step (how much to add each time):

CodeValues of iTimes
range(3)0, 1, 23
range(1, 3)1, 22
range(1, 6)1, 2, 3, 4, 55
range(0, 10, 2)0, 2, 4, 6, 85
range(10, 0, -3)10, 7, 4, 14
Python
for i in range(1, 4):
    print("Round " + str(i))
for i in range(10, 0, -3):
    print(i)
Console
Round 1
Round 2
Round 3
10
7
4
1

A loop can contain an if, which is checked on every turn. Here an extra message is printed on the turn where i is 2:

Python
for i in range(4):
    print("vocal warm-up")
    if i == 2:
        print("tongue twister practice")
Console
vocal warm-up
vocal warm-up
vocal warm-up
tongue twister practice
vocal warm-up

Practice

1Fill in the blankCurriculum practice

Complete the first line so warm is printed 10 times, using the variable count.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
for ___:
    print("warm")
Console
Press Run to see the result.
2Write the programCurriculum practice

Print Yay 7 times with a for loop and the variable i.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
3Write the programCurriculum practice

Print She sells seashells twice using range(1, 3).

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Edit the programCurriculum practice

The program prints sandwich 5 times. Inside the loop, add: if i is 1, print water bottle.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
for i in range(5):
    print("sandwich")
Console
Press Run to see the result.
5Write the programExtra practice

Print the even numbers from 2 to 10, one per line, using range with three numbers.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Write the programExtra practice

Rocket countdown: print the numbers from 5 down to 1, then print Launch! once after the loop.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • for i in range(n): repeats the indented lines n times, with i from 0 to n−1.
  • range(a, b) goes from a to b−1; range(a, b, step) adds step each time.
  • An unindented line after the loop runs once, after the loop ends.