Reduce a list of values to a single sum and mean with a manual accumulator loop. The pandas version calls .sum() and .mean() directly on a Series, showing how aggregations collapse the entire index-and-values structure to a scalar.

By hand

Accumulate total by adding each value in a loop. Compute mean as total divided by the count, rounded to two decimal places.

naive.py
Replay: real traced execution (multi-file project)
values = [4, 7, 2, 9, 3]
total = 0
for v in values:
    total += v
mean = round(total / len(values), 2)
print('RESULT:', (total, mean))
  1. values ← [4, 7, 2, 9, 3]

    1values = [4, 7, 2, 9, 3]2total = 0
    values this step[4, 7, 2, 9, 3]values
  2. total ← 0

    1values = [4, 7, 2, 9, 3]2total = 03for v in values:
    values this step0total
  3. v ← 4

    2total = 03for v in values:4    total += v
    values this step4v
  4. total ← 4

    3for v in values:4    total += v5mean = round(total / len(values), 2)
    values this step0 4total
  5. v ← 7

    2total = 03for v in values:4    total += v
    values this step4 7v
  6. total ← 11

    3for v in values:4    total += v5mean = round(total / len(values), 2)
    values this step4 11total
  7. v ← 2

    2total = 03for v in values:4    total += v
    values this step7 2v
  8. total ← 13

    3for v in values:4    total += v5mean = round(total / len(values), 2)
    values this step11 13total
  9. v ← 9

    2total = 03for v in values:4    total += v
    values this step2 9v
  10. total ← 22

    3for v in values:4    total += v5mean = round(total / len(values), 2)
    values this step13 22total
  11. v ← 3

    2total = 03for v in values:4    total += v
    values this step9 3v
  12. total ← 25

    3for v in values:4    total += v5mean = round(total / len(values), 2)
    values this step22 25total
  13. for v in values:

    2total = 03for v in values:4    total += v
  14. mean ← 5.0

    4    total += v5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))
    values this step5.0mean
  15. stdout ← RESULT: (25, 5.0)

    5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))
    values this stepRESULT: (25, 5.0)stdout

With pandas

s.sum() and s.mean() reduce the Series to scalars. int() and float() convert the numpy scalars to plain Python types for a stable RESULT.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

values = [4, 7, 2, 9, 3]
s = pd.Series(values)
total = int(s.sum())
mean = round(float(s.mean()), 2)
print('index:', s.index.tolist())
print('values:', s.tolist())
print('dtype:', s.dtype)
print('sum:', total)
print('mean:', mean)
print('RESULT:', (total, mean))
index: [0, 1, 2, 3, 4]
values: [4, 7, 2, 9, 3]
dtype: int64
sum: 25
mean: 5.0
RESULT: (25, 5.0)

Implementation notes

  • s.sum() and s.mean() return numpy scalars (np.int64, np.float64). Wrapping with int() / float() converts them to Python natives, avoiding repr differences across numpy versions (see the NumPy sum-and-mean lesson).
  • Aggregation methods ignore the index and operate only on the values array. Any index — default RangeIndex or string labels — produces the same sum and mean.
  • Other common aggregations on a Series: .min(), .max(), .std(), .median(), .count(). All reduce to a scalar and share the same numpy-scalar return type pattern.
  • Cross-reference: sum-and-mean (python-numpy ch05) for the NumPy array version; list-sum-mean (python-data-basics) for the plain Python version.