Running Totals

A running total is a value that accumulates as a loop runs.

Output:

12

This pattern appears everywhere: sum losses, count tokens, accumulate correct predictions, compute averages, and build simple statistics.

Start with an Initial Value

The total must exist before the loop:

total = 0

Then each iteration updates it:

total = total + value

Read that line carefully:

  1. compute the right side using the current total
  2. assign the new value back to total

Trace the Loop

For [3, 4, 5]:

Iterationvalueold totalnew total
1303
2437
35712

Show the running total

Printing inside the loop makes the accumulation visible.

Runs locally with Python in your browser.

Ready to run.

Averages

An average is a total divided by a count:

len(values) returns the number of items in the list. Here that count is 3.

Output:

4.0

We will later write this as a function. For now, the loop pattern matters.

Initialize the Total Before the Loop

Do not reset the total inside the loop:

That restarts the total every iteration. Initialize before the loop.

Exercise: Compute a running total

What final total does this program print?

Compute it first, then check your number.

HintCarry the total forward

Begin at zero, then add 2, then 5, then 1 without resetting.

SolutionThe final total is eight

The running totals are 2, 7, and 8. After the final iteration, print(total) prints 8.

Accumulators Remember Work Across Iterations

Running totals make repeated computation visible. If a loop feels abstract, print the total after each update.