Infinite generators
Because a generator makes values only when asked, it can go on forever. A
while True: loop with a yield inside is fine, as long as the consumer stops
asking at some point.
Never call list() on an infinite generator. It will try to collect
everything and never finish.
itertools has ready-made endless iterators:
count(start, step):start, start + step, ...cycle(items): repeat the items forever.repeat(x):xforever, orrepeat(x, n)forntimes.
To take just some, use itertools.islice(it, stop) or islice(it, start, stop). It works like slicing a list, but on any iterator, lazily. Normal
slicing like gen[:5] is a TypeError, because generators have no length or
index.
Splitting "make the values" from "decide how many" keeps each part simple, and this is how you model streams in System design style questions.
Run it, then use islice to print the 10th to 15th prime.