Course outline · 0% complete

0/27 lessons0%

Course overview →

Accumulating: totals and counts

lesson 5-3 · ~11 min · 17/27

The accumulator pattern

Here is the pattern that unlocks most beginner programs. To add up the numbers 1 through 5:

total = 0
for n in range(1, 6):
    total = total + n
print(total)

Three parts, all of them ideas you already own:

  1. Start an accumulator variable before the loop, total = 0, from lesson 3-1
  2. Update it inside the loop body each iteration, total = total + n, from lesson 3-2
  3. Use it after the loop with print(total)

Tracing total gives 0, 1, 3, 6, 10, 15, so the final print shows 15.

The same shape counts things instead of summing them. To count how many numbers from 1 to 20 are even, start count = 0 and add 1 only when a condition holds, using if inside the loop from lesson 4-2 and the remainder operator % from lesson 2-2. A number is even when n % 2 == 0.

That is worth noticing as a general move: the accumulator does not care what it accumulates. Change the update line and the same three-part skeleton totals prices, counts matches, or tracks the largest value seen.

total = 0 the loop print(total) start before, update inside, read after 1 3 6 10 15 n=1 n=2 n=3 n=4 n=5
The accumulator has three parts at three indentation levels: start once before the loop, update once per iteration, read once after. Moving the first line inside the loop would reset the total every pass.

A sum and a conditional count

Two accumulators, one adding every value and one adding 1 only when a condition holds.

total = 0
for n in range(1, 6):
    total = total + n
print(total)

count = 0
for n in range(1, 21):
    if n % 2 == 0:
        count = count + 1
print(count)

Output

15
10

The indentation carries the whole meaning of the second loop. The if line is indented once, putting it inside the for, and count = count + 1 is indented twice, putting it inside the if.

Read that nesting as a sentence: for each number from 1 to 20, if it is even, add one to the count. Ten of the twenty numbers are even, which matches the output.

Moving count = count + 1 back one level would count every number rather than the even ones, giving 20. The code would still run, which is what makes indentation mistakes worth double-checking.

Why the accumulator starts before the loop

Inside the loop, total = 0 would reset the total on every iteration, erasing the running sum.

Every pass would wipe it back to zero before adding the current number, so the final value would be just the last number rather than the sum of all of them.

The rule is: start the accumulator once, update it many times, and read it at the end. The three parts live at three different indentation levels for exactly this reason, and each one is doing a different job.

This also explains why the accumulator's starting value matters. Zero is right for a sum because adding zero changes nothing, and a running product would have to start at 1 for the same reason.

Summing 1 through 100

The same three-part pattern over a larger range.

total = 0
for n in range(1, 101):
    total = total + n
print(total)

Output

5050

Reading the pieces

  • range(1, 101) gives 1 through 100, following the stops-before rule from lesson 5-1.
  • The print is unindented so it runs once after the loop finishes. Indented, it would print a running total on each of the 100 iterations.
  • Nothing about the code changed except one number in the range. Going from 5 numbers to 100 needed no new ideas, which is the whole reason loops are worth learning.
  • Legend says the mathematician Carl Gauss computed 5050 in seconds as a schoolboy by pairing 1 with 100, 2 with 99, and so on into fifty pairs of 101. Your loop checks his work the slow and obvious way, and both answers agree.