Python Program to Find Sum of Array (original) (raw)

Last Updated : 4 Nov, 2025

Given an array of integers, find the sum of its elements. For Examples:

**Input: [1, 2, 3]
**Output: 6
**Explanation: 1 + 2 + 3 = 6

Let's explore different methods to find the sum of an array one by one:

Using sum() Function

Python provides a built-in sum() function to calculate the sum of elements in a list, tuple or set.

Python `

arr = [12, 3, 4, 15] ans = sum(arr) print('Sum:', ans)

`

Using reduce() Method

The reduce() function from functools applies a function cumulatively to the elements of an iterable, effectively summing all elements.

Python `

from functools import reduce arr = [12, 3, 4, 15] ans = reduce(lambda a, b: a + b, arr) print('Sum:', ans)

`

**Explanation: reduce() applies a function cumulatively to items of the iterable, combining them into a single result (here it repeatedly adds pairs).

Using Iteration

Iterating through the array and adding each element to the sum variable and finally displaying the sum.

Python `

arr = [12, 3, 4, 15] t = 0 for x in arr: t += x

print('Sum:', t)

`

Using enumerate() Function

enumerate() allows looping through an array with an index and element. This method adds each element to a running sum.

Python `

arr = [12, 3, 4, 15] t = 0 for i, val in enumerate(arr): t += val

print(t)

`