0
6.2kviews
Write a python program to find the sum of all the elements of the list.
1 Answer
0
822views

Explanation

  • Create a variable n which stores the no of elements user wants to add to the list.
  • Iterate for loop n times and take the elements entered from the user and append them to a list l.
  • Initialize the variable total as 0.
  • Iterate over the list and add the values of the elements to the total variable.

Code:-

l= []
ele=0
total=0
n = int(input("Enter number of elements : "))
for i in range(0, n):
    print("Enter elements:")
    ele = int(input()) 
    l.append(ele)
print(l)
for ele in range(0, n): 
    total = total + l[ele] 
print("Sum of all elements in the list: ", total)

Output:-

enter image description here

Please log in to add an answer.