Q:

Write a python program to sum all the numbers in a list

0

Write a python program to sum all the numbers in a list

In this exercise, you will learn different ways to get the sum of list using the Python programming language. Such a type of question is generally asked in an interview or competitive examination.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1: 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)

Sample 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

Method 2: 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

Method 3: 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to apply filters to images using Python and Op... >>
<< Write a python program to sort words in alphabetic...