Python 3 Script to Find the Sum of Array or List Using For Loop Full Tutorial For Beginners

You are currently viewing Python 3 Script to Find the Sum of Array or List Using For Loop Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be finding out the sum of array or list in python. All the full source code of application is shown below.

 

 

Get Started

 

 

In order to get started you need to create an app.py file and copy paste the following code

 

app.py

 

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

# calculating length of array 
n = len(arr) 

ans = _sum(arr) 

# display sum 
print ('Sum of the array is ', ans) 

# This code is contributed by Himanshu Ranjan

 

 

Now if you execute the script by typing the below command as shown below

 

python app.py

 

Leave a Reply