In: Computer Science
Program:
# Function sum_evens that takes a list of numbers and returns
the sum of evens in the list
def sum_evens(lst):
# declare a variable to hold the sum
sum = 0
# run a loop to get each number in the
list
for i in lst:
# if i is even add it to
sum
if (i % 2 == 0):
sum = sum + i
# return the sum of evens
return sum
# declare a list of numbers
list_of_nums = [1, 5, 4, 8, 5, 3, 2]
# call the sum_evens function passing the list as parameter
x = sum_evens(list_of_nums)
# display the sum of evens
print("Sum of evens in the list: " + str(x)) #prints 14
Output:
Program Screenshot: