In this tutorial, you will learn a simple Python program to find the sum of an array. We can do this by 2 methods. First, we can calculate the sum of all the elements in the array without using functions. Secondly, we can create a function to add all the elements of an array and then print the sum by calling that function.
Recommended Read – 10 Best Books To Learn Python For Beginners And Experts.
Let’s see how we can do this by these 2 methods.
Python Program To Find Sum of Array Without Function
Age = [10, 12, 13] #Inputting Array values. theSum = 0 #Definig variable to store the sum of array. for i in Age: #For loop to iterate through the array theSum = theSum + i #Adding array elements. print(theSum) #Outputting the sum of array elements.
Related post: How To Use ArcGIS API for Python and Jupyter Notebooks.
Python Program To Find Sum of Array Using Function
Age = [10, 12, 13] def sumArray(): addition = 0 for i in Age: addition = addition + i print(addition) sumArray()
Download from our Python GitHub repository.