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.
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.