Accumulate the total and count of a list of numbers in a single loop, then divide to get the mean. This is the building block for every aggregate function in later books.

By hand

Walk the list once: add each value to total and bump count by one. After the loop, divide to produce mean.

naive.py
Replay: real traced execution (multi-file project)
values = [5, 10, 15, 20, 25, 30, 35, 40]
total = 0
count = 0
for v in values:
    total = total + v
    count = count + 1
mean = total / count
print('RESULT:', round(mean, 10))
  1. values ← [5, 10, 15, 20, 25, 30, 35, 40]

    1values = [5, 10, 15, 20, 25, 30, 35, 40]2total = 0
    values this step[5, 10, 15, 20, 25, 30, 35, 40]values
  2. total ← 0

    1values = [5, 10, 15, 20, 25, 30, 35, 40]2total = 03count = 0
    values this step0total
  3. count ← 0

    2total = 03count = 04for v in values:
    values this step0count
  4. v ← 5

    3count = 04for v in values:5    total = total + v
    values this step5v
  5. total ← 5

    4for v in values:5    total = total + v6    count = count + 1
    values this step0 5total
  6. count ← 1

    5    total = total + v6    count = count + 17mean = total / count
    values this step0 1count
  7. v ← 10

    3count = 04for v in values:5    total = total + v
    values this step5 10v
  8. total ← 15

    4for v in values:5    total = total + v6    count = count + 1
    values this step5 15total
  9. count ← 2

    5    total = total + v6    count = count + 17mean = total / count
    values this step1 2count
  10. v ← 15

    3count = 04for v in values:5    total = total + v
    values this step10 15v
  11. total ← 30

    4for v in values:5    total = total + v6    count = count + 1
    values this step15 30total
  12. count ← 3

    5    total = total + v6    count = count + 17mean = total / count
    values this step2 3count
  13. v ← 20

    3count = 04for v in values:5    total = total + v
    values this step15 20v
  14. total ← 50

    4for v in values:5    total = total + v6    count = count + 1
    values this step30 50total
  15. count ← 4

    5    total = total + v6    count = count + 17mean = total / count
    values this step3 4count
  16. v ← 25

    3count = 04for v in values:5    total = total + v
    values this step20 25v
  17. total ← 75

    4for v in values:5    total = total + v6    count = count + 1
    values this step50 75total
  18. count ← 5

    5    total = total + v6    count = count + 17mean = total / count
    values this step4 5count
  19. v ← 30

    3count = 04for v in values:5    total = total + v
    values this step25 30v
  20. total ← 105

    4for v in values:5    total = total + v6    count = count + 1
    values this step75 105total
  21. count ← 6

    5    total = total + v6    count = count + 17mean = total / count
    values this step5 6count
  22. v ← 35

    3count = 04for v in values:5    total = total + v
    values this step30 35v
  23. total ← 140

    4for v in values:5    total = total + v6    count = count + 1
    values this step105 140total
  24. count ← 7

    5    total = total + v6    count = count + 17mean = total / count
    values this step6 7count
  25. v ← 40

    3count = 04for v in values:5    total = total + v
    values this step35 40v
  26. total ← 180

    4for v in values:5    total = total + v6    count = count + 1
    values this step140 180total
  27. count ← 8

    5    total = total + v6    count = count + 17mean = total / count
    values this step7 8count
  28. for v in values:

    3count = 04for v in values:5    total = total + v
  29. mean ← 22.5

    6    count = count + 17mean = total / count8print('RESULT:', round(mean, 10))
    values this step22.5mean
  30. stdout ← RESULT: 22.5

    7mean = total / count8print('RESULT:', round(mean, 10))
    values this stepRESULT: 22.5stdout

The Pythonic way

statistics.mean handles the accumulation internally and raises StatisticsError on an empty list instead of a silent divide-by-zero. The float() call normalises the return type for integer inputs.

library.py
import statistics
values = [5, 10, 15, 20, 25, 30, 35, 40]
mean = statistics.mean(values)
print('RESULT:', round(float(mean), 10))
RESULT: 22.5

Implementation notes

  • total and count stay as integers throughout the loop; the / operator produces a float at the final division step.
  • statistics.mean returns int when the mean divides evenly (e.g. mean([1, 2, 3])2) and float otherwise; wrapping with float() normalises the result to always be a float regardless of the data.