Lists and Records
Sum and Mean
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))
values ← [5, 10, 15, 20, 25, 30, 35, 40]
1values = [5, 10, 15, 20, 25, 30, 35, 40]2total = 0values this step[5, 10, 15, 20, 25, 30, 35, 40]valuestotal ← 0
1values = [5, 10, 15, 20, 25, 30, 35, 40]2total = 03count = 0values this step0totalcount ← 0
2total = 03count = 04for v in values:values this step0countv ← 5
3count = 04for v in values:5 total = total + vvalues this step5vtotal ← 5
4for v in values:5 total = total + v6 count = count + 1values this step0 → 5totalcount ← 1
5 total = total + v6 count = count + 17mean = total / countvalues this step0 → 1countv ← 10
3count = 04for v in values:5 total = total + vvalues this step5 → 10vtotal ← 15
4for v in values:5 total = total + v6 count = count + 1values this step5 → 15totalcount ← 2
5 total = total + v6 count = count + 17mean = total / countvalues this step1 → 2countv ← 15
3count = 04for v in values:5 total = total + vvalues this step10 → 15vtotal ← 30
4for v in values:5 total = total + v6 count = count + 1values this step15 → 30totalcount ← 3
5 total = total + v6 count = count + 17mean = total / countvalues this step2 → 3countv ← 20
3count = 04for v in values:5 total = total + vvalues this step15 → 20vtotal ← 50
4for v in values:5 total = total + v6 count = count + 1values this step30 → 50totalcount ← 4
5 total = total + v6 count = count + 17mean = total / countvalues this step3 → 4countv ← 25
3count = 04for v in values:5 total = total + vvalues this step20 → 25vtotal ← 75
4for v in values:5 total = total + v6 count = count + 1values this step50 → 75totalcount ← 5
5 total = total + v6 count = count + 17mean = total / countvalues this step4 → 5countv ← 30
3count = 04for v in values:5 total = total + vvalues this step25 → 30vtotal ← 105
4for v in values:5 total = total + v6 count = count + 1values this step75 → 105totalcount ← 6
5 total = total + v6 count = count + 17mean = total / countvalues this step5 → 6countv ← 35
3count = 04for v in values:5 total = total + vvalues this step30 → 35vtotal ← 140
4for v in values:5 total = total + v6 count = count + 1values this step105 → 140totalcount ← 7
5 total = total + v6 count = count + 17mean = total / countvalues this step6 → 7countv ← 40
3count = 04for v in values:5 total = total + vvalues this step35 → 40vtotal ← 180
4for v in values:5 total = total + v6 count = count + 1values this step140 → 180totalcount ← 8
5 total = total + v6 count = count + 17mean = total / countvalues this step7 → 8countfor v in values:
3count = 04for v in values:5 total = total + vmean ← 22.5
6 count = count + 17mean = total / count8print('RESULT:', round(mean, 10))values this step22.5meanstdout ← 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
totalandcountstay as integers throughout the loop; the/operator produces a float at the final division step.statistics.meanreturnsintwhen the mean divides evenly (e.g.mean([1, 2, 3])→2) andfloatotherwise; wrapping withfloat()normalises the result to always be a float regardless of the data.