Lesson 17 of 19
Iterators & Generators
Process values one at a time.
Introduction
An iterator produces the next value when asked. A generator function uses yield and pauses between values.
Lazy iteration avoids loading a large dataset into memory all at once.
Key insight
Process values one at a time.
Example
def count_up_to(limit):
for number in range(1, limit + 1):
yield number
print(list(count_up_to(3)))This example shows the core iterators & generators idea in a small, runnable program.
Real-world example
Data pipelines stream rows, log entries and API pages with generators.
Test yourself
2-question check
1
What is the main idea in Iterators & Generators?
2
Where can iterators & generators be useful?
Ready to check your understanding?
Answer every question, then submit your quiz.
Try it yourself
Python compiler
Runs in your browser. Nothing is sent to a server.
Output appears here
First run downloads the Python runtime, so it may take a moment.
Practice challenge
Write a generator that yields even numbers.