Skip to main content

Foundations of Python Programming: Functions First

Section 7.5 The Accumulator Pattern

One common programming “pattern” is to traverse a sequence, accumulating a value as we go, such as the sum-so-far or the maximum-so-far. That way, at the end of the traversal we have accumulated a single value, such as the sum total of all the items or the largest item.
The anatomy of the accumulation pattern includes:
  • initializing an “accumulator” variable to an initial value (such as 0 if accumulating a sum)
  • iterating (e.g., traversing the items in a sequence)
  • updating the accumulator variable on each iteration (i.e., when processing each item in the sequence)
For example, consider the following code, which computes the sum of the numbers in a string.
In the program above, notice that the variable accum starts out with a value of 0. Next, the iteration is performed 10 times. Inside the for loop, the update occurs. w has the value of current item (1 the first time, then 2, then 3, etc.). accum is reassigned a new value which is the old value plus the current value of w.
This pattern of iterating the updating of a variable is commonly referred to as the accumulator pattern. We refer to the variable as the accumulator. This pattern will come up over and over again. Remember that the key to making it work successfully is to be sure to initialize the variable before you start the iteration. Once inside the iteration, it is required that you update the accumulator.
Here is the same program in codelens. Step through the function and watch the “running total” accumulate the result.

Note 7.5.1.

What would happen if we indented the print accum statement? Not sure? Make a prediction, then try it and find out.
We can utilize the range function in this situation as well. Previously, you’ve seen it used when we wanted to draw in turtle. There we used it to iterate a certain number of times. We can do more than that though. The range function takes at least one input - which should be an integer - and returns a list as long as your input. While you can provide two inputs, we will focus on using range with just one input. With one input, range will start at zero and go up to - but not include - the input. Here are the examples:
Because the range function is exclusive of the ending number, we have to use 11 as the function input.
We can use the accumulation pattern is count the number of something or to sum up a total. The above examples only covered how to get the sum for a list, but we can also count how many items are in the list if we wanted to.
In this example we don’t make use of w even though the iterator variable (loop variable) is a necessary part of constructing a for loop. Instead of adding the value of w to count we add a 1 to it, because we’re incrementing the value of count when we iterate each time through the loop. Though in this scenario we could have used the len function, there are other cases later on where len won’t be useful but we will still need to count.
Check your understanding

Checkpoint 7.5.2.

    Consider the following code:
    nums = "123456789"
    for w in nums:
       accum = 0
       accum = accum + w
    What happens if you put the initialization of accum inside the for loop as the first instruction in the loop?
  • It will print out 9 instead of 45
  • The variable accum will be reset to 0 each time through the loop. Then it will add the current item. Only the last item will count.
  • It will cause a run-time error
  • Assignment statements are perfectly legal inside loops and will not cause an error.
  • It will print out 0 instead of 45
  • Good thought: the variable accum will be reset to 0 each time through the loop. But then it adds the current item.

Checkpoint 7.5.3.

Rearrange the code statements so that the program will add up the first n odd numbers where n is provided by the user.

Checkpoint 7.5.4.

Count the number of characters in string str1. Do not use len(). Save the number in variable numbs.

Checkpoint 7.5.5.

Create a string of numbers 0 through 40 and assign this string to the variable numbers. Then, accumulate the total of the string’s values and assign that sum to the variable sum1. Note that, although the value 10 will be added to the string, it will be summed as 1 + 0, as the loop merely looks at one digit at a time.
You have attempted of activities on this page.