# Calculate the sum of elements in an array using Python ## Sum of Elements in an array using Python

Here, in this page we will discuss the program to find the sum of elements in an array using Python programming language. We are given with an array and need to print the sum of its element. In this page we will discuss different ways to find the sum.

## Methods Discussed in this page are :

• Method 1 : Using Iteration
• Method 2 : Using recursion
• Method 3 : Using inbuilt Function

## Method 1 :

• Declare a variable say Sum =0 to hold the value of sum
• Run a loop for range( len(arr))
• Set variable Sum = Sum + arr[i]
• After complete iteration print value Sum

### Method 1 : Python Code

Run
```arr = [10, 89, 9, 56, 4, 80, 8]
Sum = 0

for i in range(len(arr)):
Sum = Sum + arr[i]
print (Sum)```

`256`

## Method 2 :

• Create a recursive function say getSum(arr, n)
• Base Condition : if(n==0) then return 0
• Otherwise, return arr[n-1] + getSum(arr, n-1)

### Method 2 : Python Code

Run
```def getSum(arr, n):
if n == 0:
return 0
return arr[n-1] + getSum(arr, n-1)
arr = [10, 20, 30, 40]
print(getSum(arr, len(arr)))```

`100`

## Method 3:

• Using in-built function sum(arr), we can get the sum of the elements of the array passed to it.

### Method 2 : Python Code

Run
```arr = [10, 20, 30, 40]
print(sum(arr))```

`100`

### 4 comments on “Calculate the sum of elements in an array using Python”

• SUMAN

a1 = list(map(int,input().split(“,”)))
sum = 0
for i in a1 :
sum = sum + i;
print(sum); 0
• Ranjeet

n = int(input(“Enter the size of the array. \n”))
arr = []
for i in range(n):
element = int(input())
arr.append(element)
arr.sort()
print(f”The array are: {arr} \n”)
print(f”The sum of the total array are: {sum(arr)}”) 0
• Arun

from array import *
a=array(“i”,[])
b=int(input(“enter size :”))
sum=0
for x in range(b):
c=int(input())
a.append(c)
sum=sum+c
print(sum) 0
• Abhishek

n=int(input(“Enter how many number you want to add?”))
arr=[]

for i in range(n):
x=int(input(“Enter numbers”))
arr.append(x)

print(arr)
sum=0
for j in range(len(arr)):
sum=sum+arr[j]
print(sum) 0