Write a Python program to sum all the numbers in a list
In this post, you will learn different ways to get the sum of all the numbers in a list using the Python programming language. Such a type of question is generally asked in an interview or competitive examination.
Python program to sum all the numbers in a list using for loop
Here is the Python program to sum all the numbers in a list using the for loop. First, we receive elements of the list from user input using a predefined function input() and use the for loop to iterate each element in this list. In each iteration, we are adding those elements to the total variable and getting the sum at last.
input_string = input("Enter a list element separated by space ")
listRange = input_string.split()
total = 0
# Iterate each element in a list
for num in listRange:
total += int (num)
# Get total value
print("Sum of all elements in the list = ",total)
Output of the above code-
Enter a list element separated by space 12 10 32 11 43
Sum of all elements in the list = 108
Enter a list element separated by space 200 122 422 123
Sum of all elements in the list = 867
Python program to sum all the numbers in a list using While loop
Here is the Python program to find the sum of all the elements in a list using a while loop. This is almost the same as the above. We just mention the elements of the list and replace the for loop with a while loop.
# 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 variable total
while(ele < len(list1)):
total = total + list1[ele]
ele += 1
# printing total value
print("Sum of all elements in given list: ", total)
Sample output of the above code-
Enter a list element separated by space 23 89 43 24 78
Sum of all elements in the list = 257
Enter a list element separated by space 45 33 78 29 66 20
Sum of all elements in the list = 271
Python program to sum all the numbers in a list using sum() method
Python provides an in-built method sum(), to add all the elements in a list. The syntax of the sum() method is-
sum(iterable, start)
Here, the iterable can be a list, tuples, or dictionaries. It should be numbers. The start is an optional parameter. It is added to the sum of the numbers in the iterable. The default value is zero. In the given example, we mention the elements of the list and sum them all quite easily using the sum() method-
listRange = [42, 45, 78, 20, 20]
total = sum(listRange)
# Get total value
print("Sum of all elements in the list = ",total)
Output of the above code-
Sum of all elements in the list = 205
Python program to sum all the numbers in a list using Recursive function
A recursion function is a function that is called by itself. In the given example, we call the recursion function to get the sum of all the elements in a list. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
# list of elements
ele_list = [89, 15, 39, 12, 45]
# creating recursive function
# to get sum
def getTotal(list, size):
if (size == 0):
return 0
else:
return list[size - 1] + getTotal(list, size - 1)
# Call function
total = getTotal(ele_list, len(ele_list))
print("Sum of all elements in given list: ", total)
Output of the above code-
Sum of all elements in given list: 200
Related Articles
Convert Python list to numpy array
Convert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml