In this article, we will learn about the solution and approach to solve the given problem statement.
Given an list as an input, we need to compute the sum of the given list.
Here we have two approaches to consider i.e. using built-in function & using the brute-force approach.
# main arr = [1,2,3,4,5] ans = sum(arr) print ('Sum of the array is ',ans)
15
All the variables & functions are declared in the global scope and are shown below.
total = 0 # creating a list list1 =[1,2,3,4,5] for ele in range(0, len(list1)): total = total + list1[ele] # main print("Sum of all elements in given list: ", total)
15
All the variables & functions are declared in the global scope and are shown below.
In this article, we learned about the approach to find the sum of the list.