Compute the arithmetic mean as the sum of all values divided by the count. The trace builds the running total step by step, then shows a second pass where each deviation v - mean accumulates into dev_sum, ending at 0 — demonstrating that the mean is the balance point of the distribution.

By hand

Accumulate total and count elements in a first loop, then divide. A second loop sums up every deviation v - mean to confirm they cancel out exactly.

naive.py
Replay: real traced execution (multi-file project)
values = [4, 8, 6, 13, 10, 6, 8, 7]
total = 0
n = 0
for v in values:
    total = total + v
    n = n + 1
mean = total / n
dev_sum = 0
for v in values:
    dev_sum = dev_sum + (v - mean)
print('RESULT:', round(mean, 10))
  1. values ← [4, 8, 6, 13, 10, 6, 8, 7]

    1values = [4, 8, 6, 13, 10, 6, 8, 7]2total = 0
    values this step[4, 8, 6, 13, 10, 6, 8, 7]values
  2. total ← 0

    1values = [4, 8, 6, 13, 10, 6, 8, 7]2total = 03n = 0
    values this step0total
  3. n ← 0

    2total = 03n = 04for v in values:
    values this step0n
  4. v ← 4, total ← 4, n ← 1

    pass 1 of 8
    3n = 04for v in values:5    total = total + v6    n = n + 17mean = total / n
    values this step4v0 4total0 1n
    All 8 passes — pass 1 is the card above
    passvtotaln
    140 40 1
    24 84 121 2
    38 612 182 3
    46 1318 313 4
    513 1031 414 5
    610 641 475 6
    76 847 556 7
    88 755 627 8
  5. for v in values:

    3n = 04for v in values:5    total = total + v
  6. mean ← 7.75

    6    n = n + 17mean = total / n8dev_sum = 0
    values this step7.75mean
  7. dev_sum ← 0

    7mean = total / n8dev_sum = 09for v in values:
    values this step0dev_sum
  8. v ← 4, dev_sum ← -3.75

    pass 1 of 8
    8dev_sum = 09for v in values:10    dev_sum = dev_sum + (v - mean)11print('RESULT:', round(mean, 10))
    values this step7 4v0 -3.75dev_sum
    All 8 passes — pass 1 is the card above
    passvdev_sum
    17 40 -3.75
    24 8-3.75 -3.5
    38 6-3.5 -5.25
    46 13-5.25 0.0
    513 100.0 2.25
    610 62.25 0.5
    76 80.5 0.75
    88 70.75 0.0
  9. for v in values:

    8dev_sum = 09for v in values:10    dev_sum = dev_sum + (v - mean)
  10. stdout ← RESULT: 7.75

    10    dev_sum = dev_sum + (v - mean)11print('RESULT:', round(mean, 10))
    values this stepRESULT: 7.75stdout

With the library

statistics.mean and numpy.mean both reduce the list to a single value. statistics.mean operates on pure Python sequences; np.mean works on any array-like and is vectorised for large datasets.

library.py
import statistics
import numpy as np
from dalib.display import set_display
set_display()

values = [4, 8, 6, 13, 10, 6, 8, 7]
mean_stdlib = float(statistics.mean(values))
mean_numpy = float(np.mean(values))
print('statistics.mean:', mean_stdlib)
print('np.mean:        ', mean_numpy)
print('RESULT:', round(mean_stdlib, 10))
statistics.mean: 7.75
np.mean:         7.75
RESULT: 7.75

Implementation notes

  • The deviations summing to zero is the defining property of the mean as a balance point: sum(v - mean for v in values) == 0 (up to float rounding). No other value has this property.
  • This lesson shares mechanics with python-data-basics/list-sum-mean; the framing there is algorithmic (how to compute it), while here it is statistical (what the result means).
  • statistics.mean returns int when the mean divides evenly for an all-integer input; float() normalises before printing.
  • For large arrays, prefer np.mean — it is vectorised and avoids Python loop overhead. Its internal summation order also tends to produce smaller rounding error than a scalar running total, though the exact behaviour depends on array shape and NumPy version.