Sum a 6-element list in a single accumulator loop, then divide by the count to get the mean. The trace shows total growing with each addition before mean is computed in one final step.

By hand

Initialize total to 0, then loop over values adding each element. After the loop divide by len(values) and round to two decimal places.

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

    1values = [2, 5, 1, 8, 3, 5]2total = 0
    values this step[2, 5, 1, 8, 3, 5]values
  2. total ← 0

    1values = [2, 5, 1, 8, 3, 5]2total = 03for v in values:
    values this step0total
  3. v ← 2

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

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

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

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

    2total = 03for v in values:4    total += v
    values this step5 1v
  8. total ← 8

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

    2total = 03for v in values:4    total += v
    values this step1 8v
  10. total ← 16

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

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

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

    2total = 03for v in values:4    total += v
    values this step3 5v
  14. total ← 24

    3for v in values:4    total += v5mean = round(total / len(values), 2)
    values this step19 24total
  15. for v in values:

    2total = 03for v in values:4    total += v
  16. mean ← 4.0

    4    total += v5mean = round(total / len(values), 2)6print('RESULT:', (total, mean))
    values this step4.0mean
  17. stdout ← RESULT: (24, 4.0)

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

With NumPy

a.sum() and a.mean() are reduction methods that collapse the entire array to a scalar. The snapshot shows the input array's shape and dtype, then the scalar results.

library.py
import numpy as np

values = [2, 5, 1, 8, 3, 5]
a = np.array(values)
total = int(a.sum())
mean = round(float(a.mean()), 2)
print('shape:', a.shape)
print('dtype:', a.dtype)
print('sum:', total)
print('mean:', mean)
print('RESULT:', (total, mean))
shape: (6,)
dtype: int64
sum: 24
mean: 4.0
RESULT: (24, 4.0)

Implementation notes

  • A reduction collapses an array along one or more axes to a smaller shape. Both sum() and mean() called without arguments reduce the entire array to a single scalar.
  • a.sum() and a.mean() return NumPy scalars. Using int() and float() converts them to plain Python types for RESULT, avoiding repr differences across NumPy versions (e.g. np.int64(24) vs 24).
  • For the equivalent pure-Python idiom see list-sum-mean in the python-data-basics book, and arithmetic-mean in the statistics chapter.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.