Using sum() to print the sum of even numbers in a list
问题 I am having trouble finding information on using sum to take from a list. I know how to use sum with range, for example: sum = 0 for i in range(50): sum=sum + i print (sum) But I can't get my code to work when I am using a list such as [1, 2, 6, 7, 8, 10] and taking the even numbers using sum . Can anyone point me in the right direction? 回答1: You can filter out odd-values: def is_even(x): # if the remainder (modulo) is 0 then it's evenly divisible by 2 => even return x % 2 == 0 def sum_of