Loops
Repeat work without repeating code.
Ava has to print a label for every one of forty plants. Writing forty print lines would be silly, and boring. Computers are brilliant at repeating things, as long as we tell them how. Loops are the tool, and they are probably the most powerful idea in this whole course.

- The problem: the same task, forty times
- The idea: tell Python to repeat
- The tool: loops
The for loop
A for loop repeats code for every item in a collection. Here, the variable plant takes each name in the list, one by one, and the indented block runs each time. Three names, so three greetings. Notice the colon, and the indentation, just like with if.
plants = ["monstera", "fern", "cactus"]
for plant in plants:
print("Packing a", plant)
print("All packed!")Packing a monstera Packing a fern Packing a cactus All packed!
Counting with range()
To repeat something a number of times, use range. Range of five gives zero, one, two, three, four. It stops before the end number. You can also give a start, and a step. Here Ava counts the first five boxes, then counts down with a negative step, like a rocket countdown.
for i in range(5):
print("Box", i)
for n in range(1, 10, 3):
print(n)
for n in range(3, 0, -1):
print(n)
print("Lift off!")Box 0 Box 1 Box 2 Box 3 Box 4 1 4 7 3 2 1 Lift off!
The while loop
A while loop repeats as long as a condition stays true. Ava has a budget of fifty dollars, and keeps buying pots at twelve dollars, until the money runs out. Be careful. If the condition never becomes false, the loop never ends. Always make sure something inside the loop changes.
budget = 50
while budget >= 12:
budget -= 12
print("Bought a pot, left:", budget)
print("Not enough money for another pot")Bought a pot, left: 38 Bought a pot, left: 26 Bought a pot, left: 14 Bought a pot, left: 2 Not enough money for another pot
break and continue
Two keywords control a loop. Break stops the loop immediately. Continue skips the rest of this round, and jumps to the next. Here, Ava scans her stock list. She skips the empty entries, and stops as soon as she finds a cactus.
stock = ["fern", "", "ivy", "cactus", "rose"]
for item in stock:
if item == "":
continue
print("Checking", item)
if item == "cactus":
print("Found it!")
breakChecking fern Checking ivy Checking cactus Found it!
Accumulating a total
Loops often build up a result. Start with a total of zero, then add to it on every round. This pattern, called an accumulator, is everywhere in programming. Here it totals a day of sales, and we also count how many sales were big ones.
sales = [24.99, 9.5, 14.0, 39.9, 5.25]
total = 0
big = 0
for s in sales:
total += s
if s > 20:
big += 1
print("Total:", round(total, 2))
print("Big sales:", big)Total: 93.64 Big sales: 2
Loops inside loops
A loop can live inside another loop. For each row, the inner loop runs completely. Here, Ava builds a small multiplication table for pack sizes. Nested loops are great for grids, tables, and comparing every pair of things.
for row in range(1, 4):
line = ""
for col in range(1, 5):
line += str(row * col) + " "
print(line)1 2 3 4 2 4 6 8 3 6 9 12
Water every plant
Your turn. Write a loop that goes through the list of plants, and prints, Watering, followed by the plant name. Then, after the loop, print how many plants you watered. Ready? Here is one solution. A for loop, a counter that grows each round, and a final print.
For each plant in the list print "Watering <plant>", then print the total count.
Show hint
A counter that grows inside the loop
Show solution
plants = ["fern", "ivy", "cactus", "rose"]
count = 0
for p in plants:
print("Watering", p)
count += 1
print("Watered", count, "plants")Watering fern Watering ivy Watering cactus Watering rose Watered 4 plants
Summary
That finishes the lesson on loops. For repeats over a collection, use for. For a fixed count, use range. For repeats until something changes, use while. And remember that break stops a loop and continue skips a round. Next, Ava gets organised, with lists, and tuples.
Key takeaways- for loops walk over any collection
- range(start, stop, step) counts
- while repeats until a condition fails