Counting with for and range
A for loop runs its block once for each item it's handed, range() hands it a row of numbers to count with, up, down or in steps, and break and continue end a loop early or skip one pass.
Novice · Python · Updated
After this lesson you can
- Repeat a block with a for loop over range() or a string, counting up, down or in steps
- End a loop early with break and skip one pass with continue
Key takeaways
- A for loop runs its block once for each item, and its variable holds the next item every time round.
- range(stop) counts from 0 up to stop, leaving stop out. range(start, stop) starts somewhere else, and range(start, stop, step) counts in steps, or down with a negative step.
- A for loop over text visits its characters one at a time.
- break ends a loop at once, and continue skips the rest of one pass and goes on to the next.
- Use for when you know how many times, or have the items to visit. Use while when you repeat until something happens.
Print a ticket for each of 30 students on a field trip. Check each of 12 months for a late payment. Number the pages of a report. When you know how many times something must happen, or you have the things to go through one by one, Python's for loop does the counting for you, and it's the loop you'll write most often.
The example is Splash Valley Water Park once more: a family's tickets, the countdown at the top of the Big Splash slide, and the tube rental by the lazy river.
A loop that counts for you
A family of four reaches the gate, and the booth prints one ticket per person. A while loop can do it, but you have to look after all three of its parts: the start, the condition and the change. A for loop looks after them for you:
for ticket in range(4):
print("Ticket", ticket)
Ticket 0
Ticket 1
Ticket 2
Ticket 3
Read it as "for each ticket in range 4". range(4) hands the loop four numbers, 0, 1, 2 and 3, and the loop runs its block once for each, with ticket holding the next number every time round. There's no counter to add to and no condition to forget, so this loop can't run forever.
Notice what range(4) gives: four numbers, starting at 0, and 4 itself is left out. Python counts from 0 in many places, and up to but not including the stop is how range always works.
Choosing where to start, stop and step
Tickets numbered from 0 look odd to a family. range takes up to three numbers:
range(stop): from 0 up to, not including,stop.range(start, stop): fromstartup to, not including,stop.range(start, stop, step): the same, going up bystepeach time.
people = 4
for person in range(1, people + 1):
print("Ticket for person", person)
Ticket for person 1
Ticket for person 2
Ticket for person 3
Ticket for person 4
The stop is people + 1, because the stop is left out: to reach 4, you stop at 5. The cabanas by the pool are numbered 2, 4, 6, 8 and 10, which is a step of 2: range(2, 11, 2).
A negative step counts down. The Big Splash slide counts each rider down before they go:
for seconds in range(5, 0, -1):
print(seconds)
print("Splash!")
5
4
3
2
1
Splash!
Counting down, the stop is still left out, so range(5, 0, -1) ends at 1.

Looping over text
A for loop can go through more than numbers. Given text, it visits each character in turn:
for letter in "SPLASH":
print(letter)
S
P
L
A
S
H
Text is one of many things a for loop can visit. In Module 2 you'll hand it whole lists, such as the ages of everyone in a family.
Leaving early and skipping: break and continue
A season pass costs $95, and a day ticket costs $30. On which visit does the pass start saving money? The loop below tries visits 1 to 10 and stops at the first answer:
for visits in range(1, 11):
if visits * 30 > 95:
print("The season pass pays for itself on visit", visits)
break
The season pass pays for itself on visit 4
break ends the loop at once, even though range had numbers left. Without it, the loop would print a line for visits 4, 5, 6 and on up to 10.
continue skips the rest of one pass and goes on to the next. The lazy river's tubes are numbered 10 to 15, but the park has no tube 13, since many guests won't take it:
for tube in range(10, 16):
if tube == 13:
continue
print("Tube", tube)
Tube 10
Tube 11
Tube 12
Tube 14
Tube 15
When tube is 13, continue jumps straight back to the top for 14, so the print() under it never runs for 13.
Both loops have their place. Use for when you know how many times to go round, or have the items to visit: four tickets, five seconds, six letters. Use while when you go round until something happens: the bucket tips, the guest types a believable height.
Do it: print the wave pool's schedule
The wave pool starts its waves at minute 0 of every hour, then again every few minutes, and never after minute 60.
every: how many minutes apart the waves start.
The practice box can't wait for someone to type, so the starter gives you a function line, def show_wave_times(every):, and the tests call it with several schedules. Functions come in Module 2 (What a Function Is: def and Calling); for now, write your lines indented under that line, and use every as a variable that already holds one schedule's minutes.
Your job: inside show_wave_times(every), use a for loop over range() to print one line per wave:
- The first wave is at minute 0. Print it as
Waves at minute 0. - Each wave after it is
everyminutes later. - Minute 60 counts when the schedule lands on it, and nothing after minute 60 is printed.
Worked through: with show_wave_times(15), the waves start at minutes 0, 15, 30, 45 and 60, so it prints these five lines:
Waves at minute 0
Waves at minute 15
Waves at minute 30
Waves at minute 45
Waves at minute 60
Practiced live on the lesson page: write the Python right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
A for loop with range() repeats work an exact number of times without a counter to manage, which is why it's the loop you'll reach for first, with break and continue for the cases that end early or skip one.