What is the use of the "yield" keyword in Python?
What is the use of the "yield" keyword in Python?
547
11-Apr-2023
Aryan Kumar
17-Apr-2023The Yield keyword in Python is similar to a return statement used for returning values or objects in Python. However, there is a slight difference. The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.
Krishnapriya Rajeev
12-Apr-2023The yield keyword is used in Python in order to create a generator function, i.e., one that can be used like an iterator object. In such a function, the yield keyword converts the expression following it into a generator object, which may be iterated over again and again to return the values contained inside it.
An example of a Python program implementing the yield keyword is: