Given an array of integers, find sum of its elements.
Examples :
Input : arr[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6
Input : arr[] = {15, 12, 13, 10}
Output : 50
Method 1:
# Python 3 code to find sum # of elements in given array def _sum(arr,n): # return sum using sum # inbuilt sum() function return(sum(arr)) # driver function arr=[] # input values to list arr = [12, 3, 4, 15] # calculating length of array n = len(arr) ans = _sum(arr,n) # display sum print ('Sum of the array is ', ans) # This code is contributed by Himanshu Ranjan |
chevron_right
filter_none
Output:
Sum of the array is 34
Method 2:
# Python 3 code to find sum # of elements in given array # driver function arr = [] # input values to list arr = [12, 3, 4, 15] # sum() is an inbuilt function in python that adds # all the elements in list,set and tuples and returns # the value ans = sum(arr) # display sum print ('Sum of the array is ',ans) # This code is contributed by Dhananjay Patil |
chevron_right
filter_none
Output:
Sum of the array is 34
Please refer complete article on Program to find sum of elements in a given array for more details!
Recommended Posts:
- Python Program to print digit pattern
- Python | Extract key-value of dictionary in variables
- Python program to convert time from 12 hour to 24 hour format
- Google Chrome Dino Bot using Image Recognition | Python
- Python | Use of __slots__
- Python - Summation of float string list
- Python | Animated Banner showing 'GeeksForGeeks'
- Python | Segregate list elements by Suffix
- Python Program for Selection Sort
- Python Program for Bubble Sort
- Python Program for Insertion Sort
- Python Program for Heap Sort
- Python Program for Counting Sort
- Python Program for Radix Sort
- Python Program for n-th Fibonacci number
- Python Program for Counting Sort
- Python Program for ShellSort
- Python Program for Longest Common Subsequence
- Python Program for Binary Search (Recursive and Iterative)
- Python Program for 0-1 Knapsack Problem
Improved By : avsadityavardhan

