This is Python code to print Fibonacci series using generator function. Code 1:
def test_fib(n):
a = 0
b = 1
for i in range(n):
yield a
a = b
b = a+b
#end of function
for t in test_fib(10):
print(t)
Output of code1:- 0 1 2 4 8 16 32 64 128 256 Code 2:-
def test_fib(n):
a, b = 0, 1
for i in range(n):
yield a
a, b = b, a+b
#end of function
for i in test_fib(10):
print(i)
Output of code2:- 0 1 1 2 3 5 8 13 21 34