← All Exercises

Generator Functions with Yield

intermediatepython~15min

Background

Generators enable processing of large or infinite sequences with constant memory usage.

Problem

Implement a generator that yields Fibonacci numbers lazily without storing the entire sequence in memory.

Input

A count of Fibonacci numbers

Output

Generator yielding Fibonacci numbers

Sample tests

Sample 1

Input: fib(5)
Expected: 0, 1, 1, 2, 3

Your solution

Learning objective

Understand lazy evaluation and generator protocols