Central Tendency
Arithmetic Mean
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))
values ← [4, 8, 6, 13, 10, 6, 8, 7]
1values = [4, 8, 6, 13, 10, 6, 8, 7]2total = 0values this step[4, 8, 6, 13, 10, 6, 8, 7]valuestotal ← 0
1values = [4, 8, 6, 13, 10, 6, 8, 7]2total = 03n = 0values this step0totaln ← 0
2total = 03n = 04for v in values:values this step0nv ← 4, total ← 4, n ← 1
pass 1 of 83n = 04for v in values:5 total = total + v6 n = n + 17mean = total / nvalues this step4v0 → 4total0 → 1nAll 8 passes — pass 1 is the card above pass vtotaln1 4 0 → 4 0 → 1 2 4 → 8 4 → 12 1 → 2 3 8 → 6 12 → 18 2 → 3 4 6 → 13 18 → 31 3 → 4 5 13 → 10 31 → 41 4 → 5 6 10 → 6 41 → 47 5 → 6 7 6 → 8 47 → 55 6 → 7 8 8 → 7 55 → 62 7 → 8 for v in values:
3n = 04for v in values:5 total = total + vmean ← 7.75
6 n = n + 17mean = total / n8dev_sum = 0values this step7.75meandev_sum ← 0
7mean = total / n8dev_sum = 09for v in values:values this step0dev_sumv ← 4, dev_sum ← -3.75
pass 1 of 88dev_sum = 09for v in values:10 dev_sum = dev_sum + (v - mean)11print('RESULT:', round(mean, 10))values this step7 → 4v0 → -3.75dev_sumAll 8 passes — pass 1 is the card above pass vdev_sum1 7 → 4 0 → -3.75 2 4 → 8 -3.75 → -3.5 3 8 → 6 -3.5 → -5.25 4 6 → 13 -5.25 → 0.0 5 13 → 10 0.0 → 2.25 6 10 → 6 2.25 → 0.5 7 6 → 8 0.5 → 0.75 8 8 → 7 0.75 → 0.0 for v in values:
8dev_sum = 09for v in values:10 dev_sum = dev_sum + (v - mean)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.meanreturnsintwhen 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.