Python Program To Find Sum Of Array

23-Jun-2021

.

Admin

Python Program To Find Sum Of Array

Hi Dev,

In this blg, I would like to share with you how to find sum of array in python. I am going to learn you python program to find sum of array. The variable sum will be used to calculate the sum of the elements.

In this article, 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):

# initialize a variable

# to store the sum

# while iterating through

# the array later

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 = [15, 6, 8, 19]

# calculating length of array

n = len(arr)

ans = _sum(arr)

# display sum

print ('Sum of the array is ', ans)

Output:

('Sum of the array is ', 48)

Example 2

# Python 3 code to find sum

# of elements in given array

# driver function

arr = []

# input values to list

arr = [6, 8, 5, 11]

# 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 ', 30)

It will help you....

#Python