write a Python program to sum all the items in a list.
Answers
Answer:
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 list
total = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
for ele in range(0, len(list1)):
total = total + list1[ele]
# printing total value
print("Sum of all elements in given list: ", total)
Output:
Sum of all elements in given list: 74
♠ Hope this may help you ♠
Answer:
Following is the simplest Python program will sum all the items in a list.
arr = list(map(int,input().split()))
total = sum(arr)
print("The sum of all the elements of the given list is:", total)
Explanation:
Let's understand the code above:
- First, we take the input of the elements of the list from the user.
- I have used the list function so that all the input elements will be arranged in the list.
- Next, we find the sum of all the elements. In Python, we have a pre-defined sum() function available that can find the sum by passing the name of the list.
- The sum of the elements is stored in the total variable.
- In the last line, we print the sum.
#SPJ3