# infinite generator
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# generate first 5 fib numbers
fibs = fib()
f = [next(fibs) for _ in range(5)]
print('first 5:', f)
# generate next 5 fib numbers
f = [next(fibs) for _ in range(5)]
print('next 5 :', f)
# find the nth fib number, v1
nth_fib = fib()
n = 10
for i in range(n): f = next(nth_fib)
print(f'fib({n}): {f}')
# find the nth fib number, v2
nth_fib = fib()
n = 10
f = [next(nth_fib) for _ in range(n)][-1]
print(f'fib({n}): {f}')
To embed this project on your website, copy the following code and paste it into your website's HTML: