Exercise 5
Given a list L of integers, write a Python program that returns the sum of the elements of L.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# create a function that returns the sum of the list elements def listSum(L): # initialization of the sum of the list elements s = 0 for x in L: s = s + x return s # Example L = [3, 2, 5, 3] # Display the sum of the list elements: print("The sum of the list elements is: ", listSum(L)) # The output is: The sum of the list elements is: 13 |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Solution Exercise 5: python algorithm that displays the sum of the elements of a list of numbers”