Python-courses-python-exercises-python-for-while

Exercise 5

Given a list of integers L, write a program in Python that returns the sum of the list L elements.

Solution

# creation of a function which returns the sum of the elements of a list
def listSum (L):
# initialization of the sum of the list elements
s = 0
for x in L:
s = s + x
return s

# Example
L = [2, 1, 5, 3]
# Display the sum of the list elements:
print ("The sum of the list element is:", listSum (L))
# The output is: The sum of the list L element is: 11

Younes Derfoufi
my-courses.net

Leave a Reply