Solution Review: Generator for Fibonacci Numbers
Have a look at how to create a generator for Fibonacci numbers.
We'll cover the following...
Implementation
Press + to interact
def fibonacci():c = 0n = 1while True:yield cc, n = n, c + nfor i in fibonacci():print(i)if i > 100:break
Explanation
We implement our fibonacci
generator by using a yield
statement in the function.
We start by defining two local variables, c
and n
. They are initialized to the first two elements of the Fibonacci series, i.e., 0 and 1, respectively. c
stores the ...
Access this course and 1400+ top-rated courses and projects.