What are Python Generators?

Generator functions in Python make it easy to generate data, allowing for efficient memory utilization and lazy evaluation. That is totally different from normal functions, which run completely and return a single value, but generator functions simply employ the ‘yield’ keyword to generate values one at a time as the condition is stated. Because of this difference, generator functions are suited for working with enormous datasets or infinite sequences.

Yield statement

Yield statement allows a generator function to produce a value to the caller without losing the current state of the function. When the generator function is called again, execution resumes from where it left off, continuing to yield values one at a time. 

Let us see its syntax

def generator_function():
# Initialization or setup if needed
while condition:
# Calculate or generate a value
yield value_to_yield
# Logic to be applied if necessary

Now we will see an example of a generator function to understand it better:

Python3




# generator function
def Square():
     
    number = 2
     
    #Create infinite loop
    while True:
        # Yield the current value of 'number'
        yield number
         
        # Calculate the square of 'number' and update its value
        number *= number
 
# Create a generator object 'Sq' by calling the 'Square()' generator function
Sq = Square()
 
# Function call
print(next(Sq))  # Output: 2
 
  
print(next(Sq))  # Output: 4


Output

2
4



Difference between Generator and Normal Function

Normal functions in Python are used for traditional computation tasks, with execution proceeding from start to finish, typically returning a single result. On the other hand, generator functions employ the `yield` statement to produce values lazily, preserving their state across multiple calls. This allows generators to efficiently handle large datasets or infinite sequences by yielding values one at a time and pausing execution when necessary, making them a valuable tool for memory-efficient and iterative tasks. In this article, we’ll look into Python generator functions and normal function differences i.e. how different are their syntax, how is data handled, and practical applications.

Similar Reads

What are Python Generators?

Generator functions in Python make it easy to generate data, allowing for efficient memory utilization and lazy evaluation. That is totally different from normal functions, which run completely and return a single value, but generator functions simply employ the ‘yield’ keyword to generate values one at a time as the condition is stated. Because of this difference, generator functions are suited for working with enormous datasets or infinite sequences....

Normal function in Python

...

Difference between generator and normal function

Normal function performs a specific task and can be called from other parts of the program. Also normal function  return a single value and terminate the session....