from itertools import takewhile ifilter def fib while True yield print

1
2
3
4
5
6
7
8
9
from itertools import takewhile, ifilter
def fib():
a, b = 0, 1
while True:
a, b = b, a + b
yield a
print sum(ifilter(lambda x: not x % 2, takewhile(lambda x: x <= 4000000, fib())))