Python program to find sum of elements in list

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: 33

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find sum of elements in list
total = 0
  
# creating a list
list1 = [11, 5, 17, 18, 23
  
# Iterate each element in list
# and add them in variale total
for ele in range(0, len(list1)):
    total = total + list1[ele]
  
# printing total value
print("Sum of all elements in given list: ", total)

chevron_right


Output:

Sum of all elements in given list:  74

 
Example #2 : Using while() loop



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find sum of elements in list
total = 0
ele = 0
  
# creating a list
list1 = [11, 5, 17, 18, 23
  
# Iterate each element in list
# and add them in variale total
while(ele < len(list1)):
    total = total + list1[ele]
    ele += 1
      
# printing total value
print("Sum of all elements in given list: ", total)

chevron_right


Output:

Sum of all elements in given list:  74

 
Example #3: Recursive way

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find sum of all 
# elements in list using recursion
  
# creating a list
list1 = [11, 5, 17, 18, 23]
  
# creating sum_list function
def 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)

chevron_right


Output:

Sum of all elements in given list:  74

 

Example #4: Using sum() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find sum of elements in list
  
# creating a list
list1 = [11, 5, 17, 18, 23]
  
# using sum() function
total = sum(list1)
  
# printing total value
print("Sum of all elements in given list: ", total)

chevron_right


Output:

Sum of all elements in given list:  74


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.