Q:
Write a python program to sum all the numbers in a list
belongs to collection: Python Exercises, Practice Questions and Solutions
Python Exercises, Practice Questions and Solutions
- Write a program in Python to use the string join operation to create a string that contains a colon as a separator
- Write a program in Python to replace a, b with 1, 3 respectively in a string
- Write a program in Python to encode and decode the following string
- Write a program in Python to read content from one file and write in another file.
- Write a program in Python to create a dictionary from the given sequence of keys and value provided by the user
- Write a list comprehension that returns all the pairs in a dictionary whose associated values are greater than zero
- Write a program in Python to keep count of this given list starting from 100
- Write a program in Python to calculate the value of the following expression by using lambda function
- Write a program in Python to calculate the Fahrenheit of the following Celsius list by using Lambda function
- Write a program in Python to filter the given list if the given expression evaluates to true
- Write a Python program to count the occurrences of each word in a given sentence
- Write a program to calculate simple interest in Python
- Write a python program to calculate total marks percentage and grade of a student
- Write a python program to find the area of a rectangle in python
- Write a program to find the largest of three numbers in Python
- How do you transpose a matrix using list comprehension in Python?
- How do you make a simple calculator in Python?
- How do you find the prime factor of a number in Python?
- Write a program to read two numbers and print their quotient and remainder in Python
- Write a python program to sort words in alphabetical order
- Write a python program to sum all the numbers in a list
- How to apply filters to images using Python and OpenCV?
- Write a python program to input week number and print week day
- Write a python program to map two lists into a dictionary
- Python program to multiply two numbers
- Write a python program to remove last element from list
- How do you find the GCF in Python?
- How to multiply all elements in list Python?
- How do you count consonants in a string in python?
- How to read JSON from URL requests in python?
- Write a node.js program for making external http calls
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.
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.
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-
Output of the above code -
need an explanation for this answer? contact us directly to get an explanation for this answerSum of all elements in the list = 205