Program to find the sum of elements in Python

Program to find the sum of elements in Python

Sum () 

Given a list of numbers, find the total number of items. 

Examples: Input: arr [] = {1, 2, 3,4} 
output: 10  (1 + 2 + 3 + 4) = 10 

Input: arr [] 
To find the total number of items in a blank list, you can use the total () function as shown below. 

numpy.sum (a, axis = None, dtype = None, without = None, storage =, first =). Examples of numpy.sum () are provided.


numpy.sum () in Python


Python numpy sum () function is used to find the sum of objects in addition to a given axis.

Numpy editing is a great addition to the Python list. Another important advantage of Numpy arrays is that they are fast, easy to work with, and give users the opportunity to perform calculations across all arrays.

Now take a look at how to find the sum of elements of an array in Python Programming using numpy.

Example 1:
Python code:
import numpy as np
a = np.array([10,10,2,3,4,5,6,7,8])
print(a)
sum = 0count = 0while count<=8:
    sum = sum + a[count]
    count = count+1print('sum of elements')
print(sum)
Output:
[10 10  2  3  4  5  6  7  8]
sum of elements

55

Example 2:

Program to Plot elements of an array 

Python code:
import numpy as np
from matplotlib import pyplot as plt
a = [1,2,3,4,5,6,7,8,9]
x = [9,8,7,6,5,4,3,2,1]
y = np.linspace(1,9,9)
plt.plot(a,y,'r*')
plt.plot(x,y,'bo')
plt.xlabel('element number')
plt.ylabel('element value')
plt.legend(['a','x'])
plt.title('graph of array')
plt.show()
Output:
Plot elements of an array
Plot elements of an array








Recommended Post:

Program to find even and odd numbers from the list
Program to check whether a number is odd or even
Program to check whether the number is prime or not
Plotting of even, odd, prime numbers from a list
Subplotting of even, odd and prime numbers from a list