In Python, a generator is a special type of iterable, which can be used to create iterators in an efficient and concise manner. Unlike regular functions that return a single value and terminate, generators use the yield
statement to return a value and temporarily suspend the function’s execution state.
The generator can be resumed from the point it was paused, and this allows it to produce a sequence of values one at a time, on-the-fly, without holding the entire sequence in memory.
def generator_name(arg):
# statements
yield something
To create a generator, you define a function with the yield
statement instead of using return
.
Python Generator example
Here’s an example of a simple generator that yields numbers from 1 to a specified limit:
def number_generator(limit): num = 1 while num <= limit: yield num num += 1 # Using the generator to iterate through the sequence of numbers gen = number_generator(5) for num in gen: print(num)
Output:
You can also use generator expressions, which have a syntax similar to list comprehensions but produce a generator instead of a list:
Here’s an example of a generator expression that generates squares of numbers from 1 to 5:
# Generator expression to generate squares of numbers from 1 to 5
squares_gen = (x ** 2 for x in range(1, 6))
# Using the generator to iterate through the sequence of squares
for square in squares_gen:
print(square)
Here’s a simple example of a Python generator function that generates Fibonacci numbers:
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Using the generator to generate Fibonacci numbers up to a limit
limit = 50
fib_gen = fibonacci_generator()
fibonacci_sequence = []
for num in fib_gen:
if num <= limit:
fibonacci_sequence.append(num)
else:
break
print(fibonacci_sequence)
Generator Function Syntax:
A generator function is defined using the def
keyword and contains one or more yield
statements. When the function is called, it returns a generator object, which can be iterated over using a for
loop or by calling the next()
function. Here’s the syntax for a generator function:
def generator_function(arguments):
# Initialization, if needed
while condition:
# Some calculations or processing
yield value_to_return
# Execution will pause here and will resume from this point in the next iteration
Example of Generator Function:
def number_generator(limit):
num = 1
while num <= limit:
yield num
num += 1
Generator Expression Syntax:
A generator expression is defined using parentheses ()
and has a similar syntax to list comprehensions. However, instead of using square brackets []
, it uses parentheses ()
. A generator expression is used to create an anonymous generator without explicitly defining a generator function. Here’s the syntax for a generator expression:
generator_expression = (expression for item in iterable if condition)
expression
: The expression that computes the value to be yielded by the generator.item
: The variable representing each item in theiterable
.iterable
: The source of data from which the generator will produce values.condition
(optional): An optional condition to filter items in theiterable
. It can be omitted if not needed.
Example of Generator Expression:
squares_gen = (x ** 2 for x in range(1, 6))
Both generator functions and generator expressions are useful for efficiently handling large datasets or infinite sequences, as they generate elements on-the-fly, one at a time, without the need to store the entire sequence in memory.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.