Table of Contents
Introduction
In the section, we will understand the python code to print the sum of elements of an array. Python has inbuilt function sum() to calculate the sum of elements in lists, tuples and sets the return the values.
Program
arr = [] result = 0 # Array input value arr = [2 , 5, 7] # Using sum() to add array elements result = sum(arr) print("Sum of the elements is",result)
Output
Explanation
In the above python code we have created an array variable arr[] and a result variable which is initialised to zero. The result variable will store the resulting sum of elements (2, 5 ,7) of array arr[] and sum is displayed by using print() function.
0 Comments