Skip to Content
Course content

185: Practice Exercise: Building a Custom Fibonacci Generator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you have a magic vending machine. Unlike a normal one, it doesn't hold a giant warehouse of pre-made snacks inside. Instead, it has a small notebook and a tiny kitchen. Every time you press the button, the machine looks at its notebook to see what it gave you last, cooks one fresh snack based on that information, hands it to you, and then pauses. It doesn't cook the next snack until you press the button again. It doesn't matter if you want ten snacks or ten million; the machine only ever needs enough space for that one notebook and one snack at a time.

That's exactly how a Python generator works. Instead of calculating a massive list of numbers and shoving them into your computer's RAM (which is the "warehouse" approach), a generator calculates the next value on the fly and then "freezes" its state until you ask for more. In this lesson, we're applying this to the Fibonacci sequence.

Mapping the Magic Machine to Code

Let's look at how that vending machine logic translates directly into a Fibonacci generator:

  • The Button: This is the next() function or a for loop. It's the signal that tells the generator, "Okay, wake up and give me the next value."
  • The Notebook: These are the local variables inside your function (like a and b). They keep track of where the sequence left off so the function doesn't have to start from zero every time.
  • The Delivery: This is the yield keyword. Unlike return, which kills the function and sends back a value, yield sends the value back but keeps the function alive in a paused state.
def fibonacci_generator():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

Why we avoid the "List Trap"

I've seen a lot of developers start by writing a function that appends Fibonacci numbers to a list and returns that list. That works fine if you want the first 20 numbers. But what if you need the 2,000,000th number? If you use a list, your program will likely hang or crash as it tries to allocate a massive chunk of memory to hold every single preceding number. I've been there—it's a frustrating way to spend an afternoon.

By using yield, the memory footprint remains constant. Whether you're on the 5th number or the 5 billionth, the generator only cares about two integers at any given moment. It's lean, it's fast, and it's the "professional" way to handle infinite or massive sequences.

Taming the Infinite Loop

You'll notice I used while True in the example above. Normally, an infinite loop is a bug. Here, it's a feature. Since the yield keyword pauses the execution, the loop doesn't actually "run away" and freeze your computer. It only advances one step every time you iterate over it. To prevent it from running forever in your actual app, you just use a break condition inside your loop or use a tool like itertools.islice to tell Python exactly when to stop asking the vending machine for more snacks.




📋 Practical Task

Building a Limited-Range Fibonacci Generator

Your task is to create a generator function called fibonacci_until(limit). Unlike the infinite generator we discussed, this one should stop producing numbers once the value exceeds the limit provided as an argument.

Requirements:

  • The function must use the yield keyword.
  • It should take one parameter: limit (an integer).
  • It should yield Fibonacci numbers starting from 0 and 1.
  • As soon as the next number in the sequence is greater than the limit, the generator should stop.

Example Usage:

for num in fibonacci_until(10):
    print(num) 
# Expected output: 0, 1, 1, 2, 3, 5, 8
Rating
0 0

There are no comments for now.

to be the first to leave a comment.