...

/

Solution Review: Creating Fibonacci Sequence Using Generator

Solution Review: Creating Fibonacci Sequence Using Generator

This lesson explains the solution for the "Fibonacci generator" problem.

We'll cover the following...

Solution

Press + to interact
def fib_gen(n):
x, y = 0, 1
for _ in range(n):
yield x
x, y = y, x + y
n = 10
gen = fib_gen(n)
print(list(gen))

Explanation

The first thing we need to do is declare our initial variables (x, y) at line 2 and assign them the values 0 and 1 which are the starting values for a Fibonacci sequence.

Next, since we have been provided with the ...