Central Tendency
Arithmetic Mean
Compute the arithmetic mean as the sum of all values divided by the count.
The replay 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
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.
naive.py
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))
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.