How to Find the Sum of an Array in Python?

Published On: 11/06/2022 | Category: Python

Hi Dev,

Hello all! In this article, we will talk about python find sum of array. you can understand a concept of how to find sum of array in python. This article will give you simple example of python program to find sum of array of numbers. it's simple example of python calculate sum of array.

So, We discussed about calculate the sum of the elements using python. We will show python program to find the sum of elements of the given array.

Here i will give you two example for python program to find sum of array.

So, let's see the below example:

Example : 1
# Python 3 code to find sum 
# of elements in given array 
def _sum(arr): 
      
    sum=0
      
    # iterate through the array
    # and add each element to the sum variable
    # one at a time
    for i in arr:
        sum = sum + i
          
    return(sum) 
  
# driver function 
arr=[] 
# input values to list 
arr = [20, 25, 5, 50] 
  
# calculating length of array 
n = len(arr) 
  
res = _sum(arr) 
  
# display sum 
print ('Sum of the array is ', res) 
Output:
Sum of the array is  100
Example : 2
# Python 3 code to find sum 
# of elements in given array
# driver function
arr = []
  
# input values to list
arr = [25, 20, 50, 100]
  
# sum() is an inbuilt function in python that adds 
# all the elements in list,set and tuples and returns
# the value 
ans = sum(arr)
  
# display sum
print ('Sum of the array is ',ans)
Output:
Sum of the array is  195

I Hope It will help you....