What is the difference between return and yield in Python?

2 min readMar 25, 2022

I have been working with the yield statement within a function, and not being quite sure how it differed from the return statement, I decided to research the matter. I looked on the Geek for Geeks website and learned quite a bit about the yield statement and how it differs from the return statement.

The yield statement

A yield statement is generally used to convert a regular Python function to a generator. A generator is a function that returns an iterator that can be iterated over one value at a time.

A yield replaces a return of a function to suspend its execution without destroying local variables.

A yield is used when the generator returns an intermediate result to the caller.

Code written after the yield statement will be executed in the next function call.

A yield can be run multiple times.

And finally, a yield statement function is executed from the last state from whence the function was passed.

An example of how a yield can be used in a function can be seen in the screenshot of matplotlib’s oscilloscope emitter function below:-

The return statement

A return is generally used for the end of the execution and returns the result to the caller statement.

A return exits a function and returns a value to the caller.

A return is used when a function is ready to send a value.

Code written after a return statement will not be executed.

A return runs only a single time.

And finally, when using a return statement, every function call runs the function from the beginning.

An example of how a return can be used in a function can be seen in the screenshot of matplotlib’s oscilloscope update function seen below:-

I have prepared a code review of a script that contains both a return and a yield, which can be viewed here:- https://www.youtube.com/watch?v=XqkI8f0cz-g


I have close to five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.