Generators
Understand generators and their use for creating iterators.
Generators is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Generators
Generators are a special type of iterator in Python that allow you to iterate over data lazily. Instead of returning all the data at once, they yield one item at a time, making them memory-efficient.
In this lesson, you’ll learn how to create and use generators in Python.

2
What Is a Generator?
A generator is a function that uses the yield keyword to return values one at a time. Each call to the generator’s next() method resumes execution from where it last yielded.
# Basic generator example
def my_generator():
yield 1
yield 2
yield 3
# Using the generator
gen = my_generator()
print(next(gen)) # Outputs: 1
print(next(gen)) # Outputs: 2
print(next(gen)) # Outputs: 33
How Generators Work
Generators save the state of the function, so they can resume execution exactly where they left off. This makes them memory-efficient and ideal for large datasets.
# Example: Generating a range of numbers
def range_generator(start, end):
while start < end:
yield start
start += 1
for number in range_generator(1, 5):
print(number) # Outputs: 1, 2, 3, 44
Generator Expressions
Generator expressions are similar to list comprehensions but use parentheses instead of square brackets. They create generators instead of lists.
# Generator expression
gen_exp = (x * x for x in range(5))
for value in gen_exp:
print(value) # Outputs: 0, 1, 4, 9, 165
Generators vs. Lists
Generators are more memory-efficient than lists because they don’t store all the data in memory. Instead, they generate data on the fly.
# Comparing lists and generators
# List comprehension
list_comp = [x * x for x in range(1000000)]
# Generator expression
gen_exp = (x * x for x in range(1000000))
# List comprehension uses more memory
print(type(list_comp)) # Outputs: <class 'list'>
print(type(gen_exp)) # Outputs: <class 'generator'>6
Use Cases for Generators
Generators are ideal for:
- Processing large datasets that don’t fit into memory.
- Generating infinite sequences.
- Lazy evaluation of data.
7
Infinite Generators
Generators can produce infinite sequences, such as an endless sequence of numbers. Make sure to stop the loop to prevent infinite execution.
# Infinite generator example
def infinite_sequence():
num = 0
while True:
yield num
num += 1
# Using the infinite generator
for i in infinite_sequence():
if i > 5:
break
print(i) # Outputs: 0, 1, 2, 3, 4, 58
Generator Functions with Multiple Yields
Generator functions can have multiple yield statements, allowing you to create complex sequences:
# Multiple yields in a generator
def multi_yield():
yield "First"
yield "Second"
yield "Third"
for value in multi_yield():
print(value) # Outputs: First, Second, Third9
10
Common Mistakes with Generators
Here are some mistakes to avoid:
- Using
returninstead ofyieldin a generator function. - Not consuming the generator with a loop or
next(). - Using generators for small datasets where lists are more appropriate.
11
What Did We Learn?
In this lesson, you learned:
- What generators are and how they work in Python.
- How to use
yieldto create generator functions. - The differences between generators and lists.
- Use cases for generators, such as infinite sequences and large datasets.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Generators” lesson free?
Yes — the full text of “Generators” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Generators”?
Understand generators and their use for creating iterators. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Generators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.