Program to find the sum of elements of array using Python

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 to find the sum of elements of array using Python

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

Program to find the sum of elements of array using Python 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.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.