Python code to find the sum of elements of the array. In this example, we will see how to write the Python program to find the sum of all the items of an array.
How to print the sum of all the items of the array?
To print the sum of all the items of the array we use the sum() function. Sum() function is used to find the total of all the items present in the array.
Where iterable can be list, tuples or objects of numeric datatype
And, n is a number of items which are usually considered 0.
What is the sum of all the items in the array?
Input: Array elements = [5, 10, 15, 20, 25]
Output: The sum of 5 elements is 75
Algorithm to print the sum of all the items in the array:
- Initialize an array.
- Assign a variable that will call the sum function and passing the array name as an argument.
- Print the variable.
Python program to print the sum of elements in the array.
from array import * arr = array('i', ) n = int(input("enter number of elements")) for i in range(n): arr.append(int(input("enter the array elements"))) print("entered array is:") for i in range(len(arr)): print(arr[i]) res = sum(arr) print("sum of",n,"numbers is",res)
Output: Printing the sum of all items in the array