# numpy.sum() in Python

numpy.sum() function in Python returns the sum of array elements along with the specified axis. So to get the sum of all element by rows or by columns numpy.sum() function is used.

## Basic Syntax

numpy.sum(arr, axis, dtype, out)

And the parameters are:

Parameter Description
arr This is an input array
axis [Optional] axis = 0 indicates sum along columns and if axis = 1 indicates sum along rows. If not specifies then assumes the array is flattened
dtype [Optional] It is the type of the returned array and the accumulator in which the array elements are summed.
out [Optional] Alternate output array in which to place the result.

## Return Value

It returns the sum of array elements along with the specified axis. And if the axis is not given then it will return scalar value.

## Examples

Following are the examples of numpy.sum() function in Python.

### Example 1: 1 dimensional array

# Python Program for numpy.sum() method
import numpy as np

# array 1 dimensional
items = [10, 30, 0.30, 5, 45]

print("\n Sum of items : ", np.sum(items))

print ("\nIs np.sum(items).dtype == np.uint : ",  np.sum(items).dtype == np.uint)
print ("\nIs np.sum(items).dtype == np.float : ",  np.sum(items).dtype == np.float)

print("Sum of items with dytype:uint8 : ", np.sum(items, dtype = np.uint8))
print("Sum of items with dtype:float32 : ", np.sum(items, dtype = np.float32))

The output should be:

Sum of items : 90.299999999999997
Is np.sum(items).dtype == np.uint : False
Is np.sum(items).dtype == np.float : True
Sum of items with dytype:uint8 : 90
Sum of items with dtype:float32 : 90.300003

### Example 2: 2 dimensional array

# Python Program for numpy.sum() method
import numpy as np

# array 2 dimensional
items = [[10, 30,  0.30,  5, 45],
[43, 67, 23   , 76, 89],
[ 5, 10, 15   , 20, 25],
[ 2,  3,  6   ,  7,  8]]

print("\n Sum of items : ", np.sum(items))

print ("\nIs np.sum(items).dtype == np.uint : ",  np.sum(items).dtype == np.uint)
print ("\nIs np.sum(items).dtype == np.float : ",  np.sum(items).dtype == np.float)

print("Sum of items with dytype:uint8 : ", np.sum(items, dtype = np.uint8))
print("Sum of items with dtype:float32 : ", np.sum(items, dtype = np.float32))

The output should be:

Sum of items : ‘, 489.30000000000001
Is np.sum(items).dtype == np.uint : False
Is np.sum(items).dtype == np.float : True
Sum of items with dytype:uint8 : 233
Sum of items with dtype:float32 : 489.29999

### Example 3: axis

# Python Program for numpy.sum() method
import numpy as np

# array 2 dimensional
items = [[10, 30,  0.30,  5, 45],
[43, 67, 23   , 76, 89],
[ 5, 10, 15   , 20, 25],
[ 2,  3,  6   ,  7,  8]]

print("\n Sum of items : ", np.sum(items))
print("Sum of items with axis = 0 : ", np.sum(items, axis = 0))
print("Sum of items with axis = 1 : ", np.sum(items, axis = 1))

The output should be:

Sum of items : 489.30000000000001
Sum of items with axis = 0 : [ 60. , 110. , 44.3, 108. , 167. ]
Sum of items with axis = 1 : [ 90.3, 298. , 75. , 26. ]