Given a list of numbers, write a Python program to find the sum of all the elements in the list.
Example:
Input: [12, 15, 3, 10] Output: 40 Input: [17, 5, 3, 5] Output: 30
Example #1:
Python3
# Python program to find sum of elements in listtotal = 0# creating a listlist1 = [11, 5, 17, 18, 23] # Iterate each element in list# and add them in variale totalfor ele in range(0, len(list1)): total = total + list1[ele]# printing total valueprint("Sum of all elements in given list: ", total) |
Output:
Sum of all elements in given list: 74
Example #2 : Using while() loop
Python3
# Python program to find sum of elements in listtotal = 0ele = 0# creating a listlist1 = [11, 5, 17, 18, 23] # Iterate each element in list# and add them in variale totalwhile(ele < len(list1)): total = total + list1[ele] ele += 1 # printing total valueprint("Sum of all elements in given list: ", total) |
Output:
Sum of all elements in given list: 74
Example #3: Recursive way
Python3
# Python program to find sum of all # elements in list using recursion# creating a listlist1 = [11, 5, 17, 18, 23]# creating sum_list functiondef sumOfList(list, size): if (size == 0): return 0 else: return list[size - 1] + sumOfList(list, size - 1) # Driver code total = sumOfList(list1, len(list1))print("Sum of all elements in given list: ", total) |
Output:
Sum of all elements in given list: 74
Example #4: Using sum() method
Python3
# Python program to find sum of elements in list# creating a listlist1 = [11, 5, 17, 18, 23]# using sum() functiontotal = sum(list1)# printing total valueprint("Sum of all elements in given list: ", total) |
Output:
Sum of all elements in given list: 74
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to find number of m contiguous elements of a List with a given sum
- Python | Maximum sum of elements of list in a list of lists
- Python | Ways to sum list of lists and return sum list
- Python program to find N largest elements from a list
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python program to find the frequency of the elements which are common in a list of strings
- Python | Find sum of frequency of given elements in the list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python program to find Cumulative sum of a list
- Python program to find sum of absolute difference between all pairs in a list
- Python program to find the sum of all even and odd digits of an integer list
- Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Python program to find the group sum till each K in a list
- Python | Replace elements in second list with index of same element in first list
- Python | Sorting list of lists with similar list elements
- Python | Indices of sorted list of list elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sasankgandla

