...

/

Solution Review: Generator for Fibonacci Numbers

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 = 0
n = 1
while True:
yield c
c, n = n, c + n
for 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.