How to Use the Python Generator Functions Howto

The “Generators” are considered to deal with infinite sequences or large data sets in a well-organized manner. Python generators are special functions that are defined with one or multiple “yield” keywords to produce or generate the sequence of values by retrieving an iterator object.

This Python guide will present a detailed guide on how to use Python generator functions via the below contents:

Python Generator Functions

In Python, the “Generator Functions” are special functions that utilize the “yield” keyword to retrieve a sequence of values at a time. The Python generator functions, unlike the regular functions, which retrieve a value and then terminate, the generator function can pause and resume the execution, allowing them to generate a series of values continuously without storing them in memory all at once.


def generator_function(arguments):
    # statements
    yield something

Now, let’s understand this in detail via several examples:

Example 1: Creating a Python Generator Function

The below-provided example creates a Python generator function by defining a custom function:

def generator_func(num):
    a = 0
    while a < num:
        yield a
        a += 1
for a in generator_func(5):


  • The generator function “generator_func()” is defined with one argument “num”.
  • After that, the while loop is defined and retrieves the sequence of numbers until the condition is satisfied.
  • After defining the function, the for loop is used to iterate over the function calling and retrieve the sequence based on the value.


The sequence has been generated successfully.

Example 2: Creating a Python Generator Function and Calling it Using the Object

The given code is utilized to create a Python generator function by calling it using the object:

def func1(num):
    a = 0
    while a < num:
        yield a
        a += 1
obj = func1(3)

Here, we can define the generator function similar to the previous example. However after defining the function, we created the object and used the object along with the “next()” function to generate the sequence. The “next()” function is used to call the function by utilizing the object again and again until the sequence limit is reached.


The sequence has been generated successfully.

Example 3: Creating Generator Object Using Python Generator Expression

The generator expression can also be used to create a generator object in a concise manner. Just like we can utilize the list comprehension to generate the list, the generator expression is used to generate the Python object. Here is an example code:

gent_expre = (i * i for i in range(7))
for i in gent_expre:

The above code generates the following generator function:

Example 4: Creating Fibonacci Number Series Using Python Generator Function

In the below code, the Fibonacci number series is created using the Python generator function:

def f_series(n):
    x, y = 0, 1
    while x < n:
        yield x
        x, y = y, x + y
for i in f_series(50):
    print(i, end =' ')

In the above-given code:

  • The custom generator function named “f_series()” contains the while loop that continues until the current Fibonacci number is less than the limit number specified.
  • In each iteration, the function yields the current Fibonacci number.
  • Next, it modifies the value of variables “x” and “y” to the next Fibonacci number by swapping “x” with “y” and adding them together.
  • The for loop is used to iterate over the function calling to generate the Fibonacci number until the limit-specified number is reached.


The Fibonacci series number has been created successfully.

Advantages of Python Generator Function

The Python generator function gives various advantages, such as:

  • The Python generator function is executed in a simple, clear, and short way, allowing us to create iterators without storing the complete sequence in memory.
  • The Python generator is memory efficient, which is why it is suitable for working with large data sets or infinite sequences.
  • Python generators can be utilized to create an infinite sequence, like an infinite series of numbers which would be impractical to store in memory as a list.
  • The Python multiple generator function can also be used to pipeline the series of operations to execute multiple generators simultaneously.


In Python, the generator function is similar to the normal function unless the “yield” keyword is used to return the value instead of the “return” keyword. The generator functions are used to generate the sequence of numbers by utilizing its object or by calling the functions multiple times. The generator expression can also be used to create a generator function in a simple, short, and concise way. This blog delivered a detailed tutorial on Python generator functions using various examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.