Yield vs. Return in Python: A Beginner's Guide 2025


If you're learning Python, you've probably come across the terms yield
and return
while working with functions. Understanding their differences is crucial, especially when dealing with generators and returning values from functions.
In this step-by-step guide, we'll break down yield
and return
with clear explanations and practical examples to make it easy for beginners to grasp.
Understanding return
in Python
The return
statement is used to send a value back from a function and exit the function immediately.
Example of return
# Function using return
def add_numbers(a, b):
return a + b # Returns the sum of a and b
result = add_numbers(5, 3)
print(result) # Output: 8
Key Points:
The function executes and immediately stops when it reaches
return
.It sends the specified value back to the caller.
Once
return
is executed, the function cannot continue.
Understanding yield
in Python
The yield
statement is used in Python when defining a generator function. It allows the function to return values one at a time without losing its state.
Example of yield
# Function using yield
def count_up_to(n):
count = 1
while count <= n:
yield count # Returns one value at a time
count += 1
# Using the generator function
generator = count_up_to(5)
for num in generator:
print(num)
Output:
1
2
3
4
5
Key Points:
yield
turns a function into a generator.The function does not terminate when
yield
is used; instead, it pauses and remembers its state.Each time the generator is iterated, it resumes from where it left off.
It is memory efficient as it does not store all values at once.
yield
vs. return
: Key Differences
Feature | return | yield |
Stops function execution? | Yes | No (pauses and resumes) |
Returns multiple values? | No (returns once) | Yes (returns one value at a time) |
Memory efficiency | Uses more memory | Uses less memory (good for large data) |
Used in | Normal functions | Generator functions |
When to Use return
vs. yield
?
Use
return
when you need a function to compute and return a final result.Use
yield
when you need to generate a sequence of values without storing all of them in memory.
Example: Comparing return
and yield
Using return
(Consumes more memory)
def squares(n):
result = []
for i in range(n):
result.append(i ** 2)
return result
print(squares(5)) # Output: [0, 1, 4, 9, 16]
Using yield
(Memory efficient)
def squares_generator(n):
for i in range(n):
yield i ** 2
for num in squares_generator(5):
print(num) # Output: 0, 1, 4, 9, 16
๐ Bonus for Developers & Designers: Need quick online tools for productivity? Check out UtilsHub โ a free platform offering background removers, social media downloaders, QR code generators, calculators, and more!
Conclusion
Understanding yield
and return
is essential for writing efficient Python programs. If you need to return a single value and stop, use return
. If you need to generate multiple values on the fly, use yield
. Mastering generators can help improve performance and memory usage, especially when handling large datasets.
Try implementing yield
in your own programs, and soon, you'll see its power in action!
Subscribe to my newsletter
Read articles from ajit gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

ajit gupta
ajit gupta
Learning!