0% found this document useful (0 votes)
8 views

Generators and Generator Expressions

Uploaded by

fatimaabass0328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Generators and Generator Expressions

Uploaded by

fatimaabass0328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NANOEDGE INTERNATIONAL – PYTHON PROGRAMMING COURSE (Generators and Generator

expressions)

GENERATORS AND GENERATOR EXPRESSIONS


In Python, generators are a powerful tool for crea ng iterators. They allow you to generate a sequence of
values lazily, one at a me, rather than genera ng them all at once and storing them in memory.

This lazy evalua on can be very efficient, especially for large datasets or infinite sequences.

Generators

A generator is a special type of iterator that is defined using a func on that contains one or more yield
statements. When a generator func on is called, it returns an iterator object but does not start execu on
immediately.

Instead, the code inside the func on is paused at each yield statement, and the value specified a er the
yield is returned to the caller. The state of the generator func on is preserved between calls, allowing it to
resume execu on where it le off.

def countdown(n):
while n > 0:
yield n
n -= 1

# Using the generator


for num in countdown(5):
print(num)

In this example, the countdown func on is a generator that yields numbers from n down to 1. Each me
the yield statement is encountered, the func on pauses and returns the current value of n to the caller.

Generator Expressions

Generator expressions provide a more concise way to create simple generators without the need for a
separate func on defini on. They have a syntax similar to list comprehensions but use parentheses
instead of square brackets.

1
NANOEDGE INTERNATIONAL – PYTHON PROGRAMMING COURSE (Generators and Generator
expressions)

Generator expressions are par cularly useful when you need to generate a sequence of values on the fly
without storing them in memory.

# Generate squares of numbers from 1 to 5


squares = (x**2 for x in range(1, 6))

# Using the generator expression


for square in squares:
print(square)

In this example, the generator expression (x**2 for x in range(1, 6)) generates the squares of numbers
from 1 to 5. Each value is produced lazily when needed, allowing for efficient memory usage.

Using iter() and next() with Generators

iter() func on

The iter() func on is used to create an iterator from an iterable object. When called with an iterable, such
as a list or a generator, it returns an iterator object that can be used to traverse the elements of the
iterable.

next() func on

The next() func on is used to retrieve the next element from an iterator. When called, it returns the next
element in the iterator's sequence. If there are no more elements in the sequence, it raises a StopItera on
excep on.

Here's an example of using iter() and next() with a generator:

# Define a generator function


def countdown(n):
while n > 0:
yield n

2
NANOEDGE INTERNATIONAL – PYTHON PROGRAMMING COURSE (Generators and Generator
expressions)

n -= 1

# Create an iterator from the generator using iter()


iterator = iter(countdown(3))

# Retrieve elements from the iterator using next()


print(next(iterator))
print(next(iterator))
print(next(iterator))

Addi onally, if we try to call next() beyond the end of the iterator, it will raise a StopItera on excep on.

print(next(iterator))

You might also like