- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python Generators Introduction
A Generator in Python is a special type of function that yields values one at a time, instead of returning them all at once.
Generators are memory-efficient and used for working with large datasets, streams, or infinite sequences.
Why Generators?
| Feature | Why it matters |
|---|---|
| Lazy evaluation | Values are produced on demand, not all at once |
| Memory efficient | Doesn’t store entire data in memory |
| Improves performance | Faster for large datasets / loops |
| Infinite sequences | Possible to generate endless values |
Regular Function vs Generator
Normal Function
def numbers():
return [1,2,3]
Generator Function
def numbers():
yield 1
yield 2
yield 3
The keyword yield makes it a generator.
How to Use a Generator
def numbers():
yield 1
yield 2
yield 3
gen = numbers()
print(next(gen)) # 1
print(next(gen)) # 2
print(next(gen)) # 3
Generator with Loop
def countdown(n):
while n > 0:
yield n
n -= 1
for num in countdown(5):
print(num)
Difference Between return and yield
| Keyword | Behavior |
|---|---|
| return | Exits function and sends a single value |
| yield | Pauses function, saves state, returns value, resumes on next call |
Generator Expression
List comprehension
squares = [x*x for x in range(5)]
Generator expression
squares = (x*x for x in range(5))
print(squares) # <generator object>
print(next(squares)) # 0, then 1, 4, 9...
Practical Examples
Generate Even Numbers
def even_numbers(limit):
for n in range(limit+1):
if n % 2 == 0:
yield n
for i in even_numbers(10):
print(i)
Read Large Files Line by Line (Memory Friendly)
def read_file(filepath):
with open(filepath) as f:
for line in f:
yield line
for line in read_file("large_data.txt"):
print(line)
Infinite Generator (Be careful!)
def infinite_counter():
num = 1
while True:
yield num
num += 1
gen = infinite_counter()
print(next(gen))
print(next(gen))
Fibonacci Series Generator
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
for num in fibonacci(10):
print(num)
Generator to Filter Data
def filter_positive(numbers):
for num in numbers:
if num > 0:
yield num
nums = [-4, 3, 5, -1, 9]
for n in filter_positive(nums):
print(n)