# numpy.sum() in Python With Examples [Latest]

Sep 13, 2021

Python numpy sum() function is used to get the sum of array elements over a given axis.

## Python numpy sum() function syntax

Python NumPy sum() method syntax is:

• The array elements are used to calculate the sum.
• If the axis is not provided, the sum of all the elements is returned. If the axis is a tuple of ints, the sum of all the elements in the given axes is returned.
• We can specify dtype to specify the returned output data type.
• The out variable is used to specify the array to place the result. It’s an optional parameter.
• The keepdims is a boolean parameter. If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
• The initial parameter specifies the starting value for the sum.

## Python numpy sum() Examples

Let’s look at some of the examples of numpy sum() function.

### 1. Sum of All the Elements in the Array

If we pass only the array in the sum() function, it’s flattened and the sum of all the elements is returned.

Output: `Sum of all the elements is 21`

### 2. Sum of Array Elements Along the Axis

If we specify the axis value, the sum of elements along that axis is returned. If the array shape is (X, Y) then the sum along 0-axis will be of shape (1, Y). The sum along 1-axis will be of shape (1, X).

Output:

### 3. Specifying Output Data Type of Sum

Output: `Sum of elements at 1-axis is [3. 7.]`

### 4. Initial Value for the Sum

Output: `Sum of elements at 1-axis is [13 17]`

Reference: API Doc