7.6. 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 list.
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
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.
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
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:
One important thing to know about the range function in python3 is that if we want to use it outside of iteration, we
have to cast it as a list using
list(). Inside the textbook you’ll notice that
range works with or without
casting it as a list but it is best for you to try and get into the habit of casting it as a list. Here’s how you could use the range function in the previous problem.
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
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
Rearrange the code statements so that the program will add up the first n odd numbers where n is provided by the user.
Write code to create a list of integers from 0 through 52 and assign that list to the variable
numbers. You should use a special Python function – do not type out the whole list yourself. HINT: You can do this in one line of code!
Count the number of characters in string
str1. Do not use
len(). Save the number in variable
Create a list of numbers 0 through 40 and assign this list to the variable
numbers. Then, accumulate the total of the list’s values and assign that sum to the variable