← Back to course
Code Crew G6+ / Week 04 / Monday
1/6
Week 04 Β· Loops

Monday

Repeat with for
// for, while, break and a countdown clock
⏱ about 30 min

Monday: Repeat with for

Rocket has to run a systems check on all twelve stations in Mission Control. His program has twelve print lines, almost exactly the same, one after another.

"It works," he says, scrolling down. "But if we add a thirteenth station, I have to copy another line. And another for the next one."

Raven counts the lines with her pencil. "What do you notice?" she asks. "Only the station number changes. Everything else is the same each time."

Nova spins in a slow circle. "I found a pattern," she says. "When a line repeats with one small change, a loop can do the repeating for you."

Rocket deletes eleven lines and keeps just one.

The for loop

A for loop runs its indented block once for each value. The line ends with a colon, and the block is indented, just like if.

range(3) gives the numbers 0, 1 and 2. It starts at 0 and stops just before 3, so the loop runs three times.

# check_loop.py
for station in range(3):
    print("Checking station", station)
print("All checks done")
Checking station 0
Checking station 1
Checking station 2
All checks done

Three ways to write range

You writeNumbers you getWhy
range(5)0, 1, 2, 3, 4Starts at 0, stops before 5
range(1, 6)1, 2, 3, 4, 5Starts at 1, stops before 6
range(10, 0, -2)10, 8, 6, 4, 2Steps down by 2, stops before 0

The stop number is never included. To count from 1 to 12, write range(1, 13).

How many times does for i in range(7) run its block?
What is the last number range(1, 6) gives?
READ THE RANGE
  • Read the question.
  • Tap your answer.
Which numbers does range(10, 0, -3) give?
Which range counts stations 1 to 12?
At your computer
1. Type check_loop.py in a new file, save it and run it.
2. Change range(3) to range(1, 13). Run it and count the lines.
3. Change it to range(10, 0, -2). Predict the numbers, then run it.
4. Add a second indented line inside the loop: print(" status: OK"). Run it again.

Practice: the loop variable does the work

The loop variable takes each value from the range in turn. You can use it inside the block for math, not just for printing.

A step of 5 makes range jump by fives. This is handy for checkpoints, timers and tables.

# checkpoints.py
for km in range(0, 21, 5):
    print("Checkpoint at", km, "km")
for n in range(1, 5):
    print(n, "squared is", n * n)
Checkpoint at 0 km
Checkpoint at 5 km
Checkpoint at 10 km
Checkpoint at 15 km
Checkpoint at 20 km
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
READ THE RANGES
  • Read the question.
  • Tap your answer.
Why does the first loop include 20?
What does range(3, 10, 3) give?
How many times does for n in range(5, 10) run?
What is the last line of the squares loop if you change range(1, 5) to range(1, 6)?
How many checkpoint lines appear with range(0, 31, 10)?
Which range counts 2, 4, 6, 8, 10? (type it)
StatementTrue or false?
range(4) gives 1, 2, 3, 4.?
range(1, 4) gives 1, 2, 3.?
The loop variable can be used in math inside the block.?
range(10, 0, -5) gives 10 and 5.?
WHY THIS EXERCISErange(4) gives 0 to 3, and a negative step counts down, stopping before the stop number.
PUT THE OUTPUT OF RANGE(10, 0, -3) IN ORDER
  • ?7
  • ?10
  • ?1
  • ?4
WHY THIS EXERCISEA negative step counts down, and the loop stops before it reaches 0.

One loop line replaced many copied lines. Tomorrow you will use loops to add things up.