fib() yielding the Fibonacci sequence forever: 0, 1, 1, 2, 3, … Callers slice what they need with itertools.islice — the generator itself never ends. Demonstrate that consuming 6 values gives [0, 1, 1, 2, 3, 5].list(islice(fib(), 6))
[0, 1, 1, 2, 3, 5]
The infinite stream was sliced lazily.
while True: yield a; a, b = b, a + b.
Generators pause at each yield — no infinite loop hazard.