How to find the sum of all elements of a list or a Numpy array?

The numpy.sum() function calculates the total of all the values in a numpy array or list. By default, it calculates the sum of the entire array, but you can also specify an axis along which to compute the sum. This is useful if you want to calculate the sum of each column or row in a multi-dimensional array. The function returns a single value or an array of values, depending on the shape of the input array and the axis specified.

Example

We can also calculate the sum across different axis such as rows or columns by specifying the axis parameter. Specifying axis = 0 means the sum will be calculated along the column and axis = 1 specifies to find the sum across the rows.

Example