Yield

This lesson discusses the yield keyword and its uses.

We'll cover the following...

Yield

The word yield is defined as to produce or provide or to give way to arguments or pressure. When you are on the road you may come across "yield to pedestrians" sign boards which require you to stop and give way to pedestrians crossing the road. Python's use of the word yield can both produce values and also give way as we shortly explain.

Consider the program below, which returns a string.

def keep_learning_synchronous():
    return "Educative"

We can invoke this function simply as:

    str = keep_learning_synchronous()
    print(str)

The above program is synchronous, it completes executing the method keep_learning_synchronous() and then the control returns to the invoking script and the print statement is executed. The code above appears below in the runnable widget.

Press + to interact
def keep_learning_synchronous():
return "Educative"
if __name__ == "__main__":
str = keep_learning_synchronous()
print(str)

Now let's replace the return statement with yield and see what happens. The changes are:

def keep_learning_asynchronous():
    yield "Educative"


if __name__ == "__main__":
    str =
...