# NumPy Sum

## In this tutorial you will learn:

• What is difference between summation and addition?
• Implementation of summation using different functions in Python
• Python Syntax

## Summation

Summation is the sum of all the elements of an array, if we are adding up two arrays it would be the index wise addition of elements which will result in another array having the size equal to the size of arrays being added up. Summation and addition are commonly used in mathematics and sciences to carry out basic tasks. Summation is mostly carried out on more than one elements which are contained in some data structures like list, tuple, array etc and it produces a single value.

## Implementation of Summation

Inorder to enhance the understanding, we will first see the result of adding up two 1D arrays having size 4, here you can observe that the result produced is an array having index wise addition.

`#importing the NumPy libraryimport numpy as np#declaring a first arrayarr_1= np.array([1,2,3,4])print('1st array being added up:',arr_1)#declaring a second arrayarr_2=np.array([5,6,7,8])print('2nd array being added up:',arr_2)#adding up array using add() functionres = np.add(arr_1,arr_2)#printing the resultprint('Printing the result of add() function:', res)`

In this example we will be summing up the content of one array, here you can observe that the result of applying sum() on an array is the sum of all its elements.
`#importing the NumPy libraryimport numpy as np#declaring an arrayarr_1= np.array([1,2,3,4])print('Array being summed up:', arr_1)#summing up the contents of an arrayres = np.sum(arr_1)#printing the resultprint('The result of sum() function:', res)`

NumPy provides another function ‘cumsum()’, the function takes in an array and at each index gives out a cumulative sum after adding up all the elements of the lower indexes of the array.
`#importing the NumPy libraryimport numpy as np#declaring an arrayarr= np.array([10,20,30,40])print('Array being cummulative summed:', arr)#summing up the contents of an arrayres = np.cumsum(arr)#printing the resultprint('The result of cumsum() function:', res)`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.