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.

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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