Totalling an array with a for loop

Start at zero, visit every item, and add it to a running total. Watch the total box update after each step.

Python Beginner Loops · Accumulators ~5 min
The idea

Keep a running total

To add up a list, you need a variable that remembers the sum so far — an accumulator. Initialise it to 0, then for each element, add that element into it.

After the loop finishes, total holds the sum of every value in the array.

TipThe pattern is always: init → loop → update. The variable box shows that update happening live.
IdeaAccumulator loop
array_sum.pyPython
total = 0
for i in range(len(arr)):
    total = total + arr[i]

Array: [4, 7, 2, 9] · Expected total: 22

Interactive

Run the simulation

Labeled boxes track i and total. The highlighted cell is the value being added.

Step 0 / 0
demo.pyPython
Array + variablesdemo
Ready
Shortcuts: Space play/pause · → step · ← back · R reset
Remember

The accumulator pattern

Same idea powers averages, products, and many score trackers.

Init

Start at a known value

For a sum, start at 0. For a product you would start at 1.

Update

Change the box each step

total = total + arr[i] reads the old total, adds, and writes back.

Result

Ready after the loop

Once every index has been visited, total is the answer.