What does the Yield Keyword do?

yield keyword is used to create a generator function. A type of function that is memory efficient and can be used like an iterator object.

In layman terms, the yield keyword will turn any expression that is given with it into a generator object and return it to the caller. Therefore, you must iterate over the generator object if you wish to obtain the values stored there. we will see the yield python example.

Python | yield Keyword

In this article, we will cover the yield keyword in Python. Before starting, let’s understand the yield keyword definition.

Syntax of the Yield Keyword in Python

def gen_func(x):
    for i in range(x):
        yield i

Similar Reads

What does the Yield Keyword do?

yield keyword is used to create a generator function. A type of function that is memory efficient and can be used like an iterator object....

Difference between return and yield Python

The yield keyword in Python is similar to a return statement used for returning values in Python which returns a generator object to the one who calls the function which contains yield, instead of simply returning a value. The main difference between them is, the return statement terminates the execution of the function. Whereas, the yield statement only pauses the execution of the function. Another difference is return statements are never executed. whereas, yield statements are executed when the function resumes its execution....

Contact Us