![Python-courses-python-exercises-python-for-while](https://1.bp.blogspot.com/-T3BongcoJFo/X4CftQxVL8I/AAAAAAAAB5U/Lf59WgNyhGo2FdogKqxv0PIy_Ef7XDPdgCLcBGAsYHQ/s0/my-python-courses-python-exercises-with-solutions.png)
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
my-courses.net