Python-courses-python-exercises-python-students-online-courses

Exercise 40

Write a Python algorithm that extracts from a list of numbers the sublist of numbers that end with an even number. Example: if L = [21, 14, 346, 728, 13, 19], the algorithm returns the list: [14, 346, 728]

Solution

def listEven (L):
# initialization of the list of numbers that end with an even digit
lEvent = []
for n in L:
# determine the last digit of the number n
last = n% 10
# test if the last digit is even
if last% 2 == 0:
lEvent.append (n)
return lEvent

# Example
L = [21, 14, 346, 728, 13, 19]
print (listEven (L)) # display [14, 346, 728]

Leave a Reply