Python Program to Find Sum of Array
In this tutorial, we will learn how to find the sum of all the elements in an array. Before moving further it is necessary that you are familiar with loops in Python.
Python arrays are a data structure like lists, which store homogenous data. Arrays stores objects of the same datatype. Arrays are mutable, meaning that the data in an array can be changed and iterative, meaning each element in an array can be accessed one by one.
In python, you need to import the array module to create an array. The array(data_type, value) function is used to specify an array with its data type and value list given as arguments. Look at the example given below for creating an array in Python.
import array as ar
a = ar.array('i', [1, 3, 5])
Here, we have declared an array of integer types with the value list [1, 3, 5]. The letter i is a type code. This determines the type of the array during creation.
Commonly used type codes are
Code |
Python Type |
i |
integer |
u |
unicode |
f |
float |
l |
integer |
The program should take input and give output as follows:
Input- arr[] = {15,12,13,10}
Output- 50
Approach to Find Sum of Array
For executing this task we can follow two approaches:
- Using a loop to calculate sum
- Using built-in sum() function
Approach 1: Using looping statement
In this approach, we will use a loop that will run from the starting to the ending index of the array and add all the elements individually.
Algorithm
Step 1- Import array module
Step 2- Define a function to calculate the sum of elements in an array
Step 3- Declare a variable to store the sum
Step 4- Calculate the length of the array using len() function
Step 5 - Run a loop for all the elements in the array
Step 6- Add each element to the variable for sum one by one
Step 7- Return sum
Step 8- Declare the array
Step 9- Call function
Step 10- Print the result
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
import array as ar
def SumofArray(arr):
sum=0
n = len(arr)
for i in range(n):
sum = sum + arr[i]
return sum
#input values to list
a = ar.array('i',[10, 21, 12, 13])
# display sum
print ('Sum of the array is ', SumofArray(a) )
Sum of the array is 56
Approach 2: Using sum() function
In this approach, we will use a built-in function called sum() which calculates the sum of all the elements in an array and returns the result.
Algorithm
Step 1- Import array module
Step 2- Declare array using the module
Step 3- Call sum() function
Step 4- Print the result
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach.
import array as arr
#input values
a = arr.array('i',[10, 21, 12, 13])
# display sum
print ('Sum of the array is ', sum(a) )
Sum of the array is 56
Conclusion
In this tutorial, we have learned, two approaches by which we can calculate the sum of all the elements in the array. One, by using a loop to add the elements. Second, by using a predefined function sum() in the Python Library.