The exam's favourite loop shapes: running totals, counting matches, tracking max without built-ins, and loops inside loops that print tables and patterns.
01
Learning objectives
Apply the accumulator pattern: initialise before the loop, update inside, use after.
Count items matching a condition with a counter variable.
Track the maximum/minimum of a sequence without max()/min().
Write nested loops and predict the total number of inner passes.
Print patterns (triangle of stars, multiplication grid) with nested loops and end="".
02
Lesson notes
A handful of shapes cover nearly every loop question ever set. The first is the accumulator: a variable that gathers a result across the whole loop. The recipe has three beats, and every beat matters — initialise before the loop (total = 0), update inside the body (total = total + n, usually written total += n), and use the result after the loop. Initialise-inside-the-loop is the classic bug: the total resets to zero every pass and the final answer is just the last item.
The same skeleton with different updates produces the whole family. count += 1 inside an if makes a counter that answers 'how many students passed?'. Replacing the arithmetic with a comparison makes a tracker: if n > biggest: biggest = n walks a list keeping the largest value seen so far — the from-scratch max() the board loves to demand (initialise with the FIRST item, not with 0, or negative lists will defeat you). Swapping > for < turns it into a minimum. One skeleton, three tools.
Averages and summaries compose the pieces: accumulate a total, count the items, divide — and remember the guard for an empty sequence (dividing by zero is a chapter 9 concern, but checking if count > 0 first is already good style). The f-string from module 3 then formats the report: f"Average: {total / count:.2f}". Almost every 'data analysis' challenge in the Labs is exactly this loop wearing slightly nicer clothes.
Nested loops — a loop inside a loop — appear whenever data has two dimensions or output has rows and columns. The outer loop advances row by row; the inner loop sweeps across each row completely before the outer's next pass. Their trip counts MULTIPLY: 3 outer × 4 inner passes = 12 inner executions, a fact papers test directly ('how many times does print run?'). Trace nested loops with a two-column table (outer value, inner value) — one row per inner pass.
Pattern printing is nested loops' favourite exam appearance, and end="" from module 3 is the trick that makes it work. A right triangle of stars: the outer loop counts rows 1..5; the inner loop prints row stars with end="" so they sit on one line; a bare print() after the inner loop closes the row. The multiplication grid follows the same frame with numbers. Master these two and every 'print the pattern' question becomes assembly of known parts.
03
Worked code examples
Accumulator + counter + average
Two accumulators share one loop: total gathers every mark, passed counts the ≥ 33 ones. Both initialise before the loop and report after — the pattern is identical to while loops, but for walks the list for free.
marks = [72, 91, 55, 84, 28]
total = 0
passed = 0
for m in marks:
total += m
if m >= 33:
passed += 1
print("Total:", total)
print("Average:", round(total / len(marks), 2))
print("Passed:", passed, "of", len(marks))
Output
Total: 330
Average: 66.0
Passed: 4 of 5
Max and min without max() / min()
Trackers initialise with the first element and update only when a better value appears. Starting biggest at 0 would wrongly report 0 instead of 37 for an all-negative list — the exam's favourite trap.
readings = [21, -4, 37, 8]
biggest = readings[0] # first item, NOT 0
smallest = readings[0]
for r in readings[1:]:
if r > biggest:
biggest = r
if r < smallest:
smallest = r
print(biggest, smallest)
Output
37 -4
Nested loops: triangle of stars and the times grid
The inner loop's range DEPENDS on the outer's row value — that dependence is what bends a square of stars into a triangle. In the grid the inner loop completes fully for every outer pass: 3 × 3 = 9 cells, tab-separated with \t.
for row in range(1, 6):
for star in range(row):
print("*", end=" ")
print()
for r in range(1, 4):
for c in range(1, 4):
print(r * c, end="\t")
print()
Output
*
* *
* * *
* * * *
* * * * *
1 2 3
2 4 6
3 6 9
04
Key terminology
accumulator
A variable initialised before a loop and updated inside it, collecting a running result.
counter
An accumulator that increments by one for each matching item, answering 'how many?'.
tracker
A variable holding the best value seen so far (max or min), updated by comparison.
nested loop
A loop inside another; the inner completes all its passes for every single outer pass.
end=""
print's setting that keeps output on one line — the tool that lets a loop draw a row.
05
Common misconceptions
Common mistake
total = 0 can go inside the loop, just before the update.
Correct understanding
That resets the total every pass and the 'final' value is only the last item. Accumulators initialise once, BEFORE the loop.
Common mistake
biggest = 0 is a safe starting point for finding a maximum.
Correct understanding
For all-negative data the tracker would end at 0, which was never in the data. Initialise with the first element.
Common mistake
A nested loop with 3 outer and 4 inner passes runs the inner body 7 times.
Correct understanding
Trip counts multiply: 3 × 4 = 12 inner executions — one full inner sweep per outer pass.
06
Check your understanding
1. total = 0
for n in [4, 9, 2]:
total += n
print(total)
What is printed?
2. How many times does the inner print run?
for r in range(3):
for c in range(4):
print(r, c)