Exercise 27 

Write a program in Python as a function that calculates the sum of elements in a list of numbers. And another that multiplies all the elements of a list of numbers.

Solution

# Function that calculate the product of list elements
def mult(l):
m = 1
# run through the list elements
for x in l:
m = m*x
return m
print(mult([2,5,3]))

# Function that calculate the sum of list elements
def sum(l):
s = 0
# run through the list elements
for x in l:
s = s + x
return s
print(sum([2,5,3]))
Younes Derfoufi

Leave a Reply