← All Exercises

Python Generator Functions

intermediatepython~20min

Background

Generators produce values lazily, enabling processing of infinite sequences and large datasets without memory overhead.

Problem

Write a generator function that produces Fibonacci numbers indefinitely, and a function to take the first N numbers from it.

Input

A count N of Fibonacci numbers to generate.

Output

A list of the first N Fibonacci numbers.

Sample tests

Sample 1

Input: 8
Expected: [0, 1, 1, 2, 3, 5, 8, 13]

Your solution

Learning objective

Learn generator functions with yield, lazy evaluation, and memory-efficient sequence processing.