Home >>Python Programs >Find the sum of all elements in an array in python
In this example, we will see a Python program through which we can print the sum of all the elements present in a given array.
We can find the sum of all the elements in an array by looping through the array and add the value of the element in each iteration to some variable sum. At the end of the iteration, the variable will have the value of the sum of all elements.
#Initialize array
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
sum = 0;
#Loop through the array to calculate sum of elements
for i in range(0, len(arr)):
sum = sum + arr[i];
print("Sum of all the elements of an array: " + str(sum));