0% found this document useful (0 votes)
5 views2 pages

Difference

In Python, 'return' immediately terminates a function and returns a single value, while 'yield' creates a generator that can pause and resume execution, allowing for multiple values to be returned one at a time. Yield is more memory efficient as it generates values on demand, making it suitable for large datasets and infinite sequences. Examples illustrate the difference between a standard function using return and a generator function using yield.

Uploaded by

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

Difference

In Python, 'return' immediately terminates a function and returns a single value, while 'yield' creates a generator that can pause and resume execution, allowing for multiple values to be returned one at a time. Yield is more memory efficient as it generates values on demand, making it suitable for large datasets and infinite sequences. Examples illustrate the difference between a standard function using return and a generator function using yield.

Uploaded by

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

difference between yield and return in python

In Python, yield and return are both keywords used to return values from
functions, but they work in different ways and have distinct
purposes. Here's a breakdown of their differences:
Return:
 Terminates the function: When a return statement is encountered, the
function immediately stops executing, and the specified value is returned to
the caller.
 Single value: A function can only return a single value using return.
 Memory usage: If you need to return a large dataset, using return might
consume a lot of memory, as it creates the entire dataset in memory before
returning it.
Yield:
 Creates a generator:
A function containing a yield statement becomes a generator
function. When called, it doesn't immediately execute but instead returns a
generator object.
 Pauses and resumes:
The yield statement pauses the function's execution, saving its state, and
returns a value to the caller. The next time the generator's next() method
is called, the function resumes execution from where it left off.
 Multiple values:
A generator function can yield multiple values, one at a time, as the caller
iterates over the generator object.
 Memory efficient:
Generators are memory efficient, as they only generate values on
demand, rather than creating the entire dataset upfront.
When to use yield:
 Large datasets:
When you need to work with large datasets that don't fit comfortably in
memory, generators are a great choice.
 Lazy evaluation:
If you only need to process a few values at a time, using yield allows you
to generate values on demand, improving performance.
 Infinite sequences:
Generators can be used to represent infinite sequences, such as the
Fibonacci sequence.
Example:
Python
# Function using return
def square_numbers(n):
result = []
for i in range(n):
result.append(i**2)
return result

# Generator function using yield


def square_numbers_gen(n):
for i in range(n):
yield i**2

# Using return
squares = square_numbers(5)
print(squares) # [0, 1, 4, 9, 16]

# Using yield
squares_gen = square_numbers_gen(5)
for square in squares_gen:
print(square) # 0, 1, 4, 9, 16

You might also like