How to find sum of array with python

Configurare noua (How To)

Situatie

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 = [12, 3, 4, 15
 
# 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  34

Solutie

Tip solutie

Permanent

Voteaza

(12 din 20 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?