---Advertisement---

Python Yield Keyword Tutorial: Generator & Yield vs Return Explained with Examples Best Guide 2025

By Bhavani

Updated On:

---Advertisement---
 Python Yield Keyword Tutorial

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.

  • 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

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

def function_name():

    yield value


Here’s a simple example demonstrating yield in Python:

python

def testyield():

    yield “Welcome to Python Yield Tutorial”

output = testyield()

print(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)

vbnet

Welcome to Python Yield Tutorial


Generators are special functions that return iterable objects. These objects provide values one at a time, reducing memory consumption and improving performance.

python

def generator():

    yield “H”

    yield “E”

    yield “L”

    yield “L”

    yield “O”

test = generator()

for i in test:

    print(i)

mathematica

H

E

L

L

O


Featureyieldreturn
ReturnsGenerator objectSingle value
Execution FlowPauses at yield and resumes laterEnds function execution
Memory UsageEfficient for large data setsUses more memory
Ideal forLarge data sets or continuous data flowSmall data sets or fixed values

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


You can read generator values using:

  1. 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))

csharp

[0, 2, 4, 6, 8]

  1. for-in Loop

python

for i in even_numbers(10):

    print(i)

  1. next() Method

python

num = even_numbers(10)

print(next(num))  # Output: 0

print(next(num))  # Output: 2


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: []


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)

0

1

1

2

3

5

8


✅ When handling large data sets.
✅ When you need faster execution with minimal memory usage.
✅ When processing continuous data streams or infinite sequences.


  • 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.

 Python timeit() Function with Examples

Download Python

Leave a Comment

Index