Deviopath › Python

Fibonacci Generator

Medium · Generators

A function that contains yield is a generator: calling it returns an object that produces values one at a time, pausing after each yield until the next value is requested. That makes infinite sequences possible, because nothing is computed until someone asks.

Write a generator fibonacci() that yields the Fibonacci numbers forever: 0, 1, 1, 2, 3, 5, 8, ... (each number is the sum of the two before it).

The driver first_fibonacci is already written: it takes the first n values with itertools.islice.

Next: Countdown Iterator