Python Array Sum
arr.sum() Practice

One call replaces a total loop

arr.sum() adds every element. Same idea as sum(list), but on a NumPy ndarray.

Picture this: add every cell

np.sum(a) folds the whole array into one number.

array amany numbers
→
np.sum(a)add all
→
one totalscalar

Diagram — array sum

np.array([...]) .sum() total

In easy words

You have numbers in an array. Instead of total = 0 and a for-loop, call arr.sum() and NumPy returns the total.

Where this shows up

Totals of sensor readings, pixel intensities, feature sums before normalization, batch loss aggregation.

Key facts

  • Method form — arr.sum() or function form np.sum(arr).
  • Axis optional — On 2D data, axis=0/1 sums columns/rows.
  • Empty risk — Know your shape before summing.
  • Type — Result is usually a NumPy scalar.

What to remember

  • Trace left→right — Running total grows cell by cell.
  • Practice target — [1,2,3,4,5] → 15.
  • Compare — Python sum(arr.tolist()) vs arr.sum().
  • Next — Mean uses the same array idea.
Interactive visualizer

Running total

Press Step to add the next cell

Example Program

1import numpy as np
2arr = np.array([1, 2, 3, 4, 5])
3print(arr.sum()) # 15