And ye shall compass the city, all ye men of war, and go round about the city once. Thus shalt thou do six days. And seven priests shall bear before the ark seven trumpets of rams’ horns: and the seventh day ye shall compass the city seven times, and the priests shall blow with the trumpets. — Joshua 6:3–4 (KJV)
Jericho was the first city Israel faced in the promised land, and the instructions were strange: no siege, no attack. Walk around it once a day for six days, in silence. On the seventh day, walk around it seven times. Then shout.
Those instructions have a shape every programmer knows: do this, a known number of times.
The for loop
for day in range(1, 7):
print("Day", day, "- one lap")
Day 1 - one lap
Day 2 - one lap
Day 3 - one lap
Day 4 - one lap
Day 5 - one lap
Day 6 - one lap
Read it as: for each day in the numbers from 1 up to (but not including) 7, run the indented line.
dayis a variable the loop sets for you: 1 the first time, 2 the second, and so on.- The indented line is the body of the loop. It runs once per number.
- When the numbers run out, Python moves on to whatever comes after the loop.
range stops just before the end
range(1, 7) gives 1, 2, 3, 4, 5, 6 — six numbers, not seven. The end number is where it stops, and it’s not
included. This trips everyone up at first; it’s on purpose, because it makes the count easy: range(1, 7) has
7 - 1 = 6 numbers.
| You write | You get |
|---|---|
range(6) |
0, 1, 2, 3, 4, 5 (starts at 0) |
range(1, 7) |
1, 2, 3, 4, 5, 6 |
range(0, 10, 2) |
0, 2, 4, 6, 8 (counts by 2) |
The seventh day: a second loop
for day in range(1, 7):
print("Day", day, "- one lap")
for lap in range(1, 8):
print("Day 7 - lap", lap)
print("Shout!")
The second loop runs seven times (range(1, 8) is 1 to 7). The last line isn’t indented, so it isn’t part of either
loop: it runs once, after both have finished. Six laps plus seven laps: thirteen.
So the people shouted when the priests blew with the trumpets… and the wall fell down flat. — Joshua 6:20 (KJV)
The same idea in JavaScript
for (let day = 1; day <= 6; day++) {
console.log("Day", day, "- one lap");
}
JavaScript spells the counting out: start at 1, keep going while day <= 6, add 1 each time (day++).
It’s everywhere
| Loop | What it does |
|---|---|
for person in subscribers: |
send everyone the email |
for photo in album: |
make a thumbnail of every photo |
for rep in range(10): |
count ten reps |
A for loop can go through any list, not just numbers — that’s the next big idea, in the lesson on lists.
Common mistakes
- Off by one.
range(1, 6)stops at 5. If you want 1 to 6, the end is 7. - Forgetting the colon after
for … in …— Python needs it. - Indentation. Only the indented lines repeat. A line that should repeat but isn’t indented runs once, after the loop.
- Changing the loop variable inside the loop doesn’t change how many times it runs;
rangealready decided.
Try it
-
Print the numbers 1 to 10, one per line.
-
Print only the even numbers from 2 to 20 using the third argument of
range. -
Add up the laps with a loop instead of by hand: start with
total = 0, add 1 for every lap of both loops, and printtotal. It should be 13.One way to do it
total = 0 for day in range(1, 7): total = total + 1 for lap in range(1, 8): total = total + 1 print(total) # 13
Reflect
Thirteen laps that must have felt pointless while they walked them. The walls didn’t crack a little each day; nothing seemed to happen until the very last lap. Faithfulness often looks like repeating the same small obedience, counting on God for the outcome.