nick34992
2/12/2017 - 11:20 PM

Python: Generator and Comprehension for Fibonacci Sequence

Python: Generator and Comprehension for Fibonacci Sequence

#!/usr/bin/env python3.5

def fibonacci():
    a, b = 0, 1
    while True:
        yield b
        a, b = b, a + b

fib = fibonacci()
print([fib.__next__() for i in range(20)])

'''
 a,  b =  b,  a +  b

 5,  8 =  8,  5 +  8
 8, 13 = 13,  8 + 13
13, 21 = 21, 13 + 21
'''