
👉Python Yield Keyword Tutorial: Generator & Yield vs Return Example
Python Yield Keyword Tutorial: The yield keyword in Python functions similarly to return, but instead of returning a value directly, it provides a generator object to the caller. This allows data to be generated and iterated efficiently without consuming excessive memory.
👉Python Yield Keyword Tutorial Table of Contents
- What is Python Yield?
- Python Yield Syntax
- Python Yield Example
- Generators in Python
- Difference between yield and return
- Using List, for-in, and next() with Generators
- When to Use yield in Python
- Summary
👉What is Python Yield?
Python Yield Keyword Tutorial: The yield keyword is used within a function to create a generator. When a function with yield is called, it doesn’t execute immediately. Instead, it pauses at the yield statement and returns a generator object that can be iterated to retrieve the yielded values.
👉Python Yield Syntax
python
def function_name():
yield value
👉Python Yield Example
Here’s a simple example demonstrating yield in Python:
python
def testyield():
yield “Welcome to Python Yield Tutorial”
output = testyield()
print(output)
👉Output:
csharp
<generator object testyield at 0x00000028265EB9A8>
To print the actual value, you need to iterate over the generator object:
python
for i in output:
print(i)
👉Output:
vbnet
Welcome to Python Yield Tutorial
👉Generators in Python
Generators are special functions that return iterable objects. These objects provide values one at a time, reducing memory consumption and improving performance.
👉Example: Generator Function
python
def generator():
yield “H”
yield “E”
yield “L”
yield “L”
yield “O”
test = generator()
for i in test:
print(i)
👉Output:
mathematica
H
E
L
L
O
👉Difference between yield and return
Feature | yield | return |
Returns | Generator object | Single value |
Execution Flow | Pauses at yield and resumes later | Ends function execution |
Memory Usage | Efficient for large data sets | Uses more memory |
Ideal for | Large data sets or continuous data flow | Small data sets or fixed values |
👉Python Yield Keyword Tutorial Example:
python
# Normal function
def normal_test():
return “Hello World”
# Generator function
def generator_test():
yield “Hello World”
print(normal_test()) # Output: Hello World
print(generator_test()) # Output: <generator object generator_test at 0x00000012F2F5BA20>
To extract values from the generator:
python
print(next(generator_test())) # Output: Hello World
👉How to Read Values from a Generator
You can read generator values using:
- list() Method
python
def even_numbers(n):
for x in range(n):
if x % 2 == 0:
yield x
num = even_numbers(10)
print(list(num))
👉Output:
csharp
[0, 2, 4, 6, 8]
- for-in Loop
python
for i in even_numbers(10):
print(i)
- next() Method
python
num = even_numbers(10)
print(next(num)) # Output: 0
print(next(num)) # Output: 2
👉Generators are One-Time Use
Once a generator is exhausted, it cannot be reused unless recreated.
python
num = even_numbers(10)
for i in num:
print(i)
print(list(num)) # Output: []
👉Example: Fibonacci Series using yield
python
def getFibonacciSeries(num):
c1, c2 = 0, 1
count = 0
while count < num:
yield c1
c1, c2 = c2, c1 + c2
count += 1
fib = getFibonacciSeries(7)
for i in fib:
print(i)
👉Output:
0
1
1
2
3
5
8
👉When to Use yield in Python
✅ When handling large data sets.
✅ When you need faster execution with minimal memory usage.
✅ When processing continuous data streams or infinite sequences.
👉Python Yield Keyword Tutorial Summary
- The yield keyword is ideal for creating generator functions in Python.
- Unlike return, yield pauses the function’s execution and returns a generator object.
- Generators provide efficient memory management, especially for large data sets.
- You can extract values from a generator using list(), for-in loops, or the next() method.