Return vs Yield in Python with examples

Python keywords are reserved words that are pre-defined to convey special meanings(functions) to the interpreter. In today’s tutorial, you will learn about two such keywords:

  1. Return
  2. Yield

Return vs Yield in functions

If you are familiar with functions and their usage in programming, you would have come across the return keyword too. The return keyword is usually used in Python functions to return a value at the end of the function.
The yield keyword, on the other hand, is used to return a series of values instead of a single value at the end of the function.

How is yield different from return?

When the yield keyword is used in a function, that function becomes a generator function.
If you are new to generator functions, you can read Using Generators inside Functions in Python .

On encountering the yield statement, the function pauses its execution and returns the value to the caller. The state of the function is then resumed and the execution continues. The same can be studied as follows:

def eg_func():
    yield "a"
    yield "b"
    yield "c"
generator_obj=eg_func()
res=next(generator_obj)
print(res)
res=next(generator_obj)
print(res)
res=next(generator_obj)
print(res)
a
b
c

Here, every time the yield keyword is encountered, the execution of the program is paused and the result is sent back to the caller. Once this is done, the execution is resumed from the last yield.

However, this is not the case with the return keyword. The return statement is usually the last statement in a function. Once the return keyword is encountered, the control exits the function along with returning the value (if any) and in the case of a loop, breaks it.
The same can be demonstrated as shown below:

no1=1
no2=2
def operation():
    return no1+no2
    return no1-no2
result=operation()
print(result)
result=operation()
print(result)
3
3

As you can see, the control exits the function as soon as the first return is encountered. Anything written after it gets ignored.
The yield statement thus, comes in handy when you need to return more than one value. In the above example, instead of having to define a different function for each operation, we can simply replace the return keyword with yield and use a generator function to help us.

no1=1
no2=2
def operation():
    yield no1+no2
    yield no1-no2
generator_obj=operation()
result=next(generator_obj)
print(result)
result=next(generator_obj)
print(result)
3
-1

Some other differences

Some more differences between the two of these keywords include the facts that the yield keyword, as we can see, can be called multiple times. However, the return keyword can only be called once in a function.

Also, the code written after the yield keyword gets executed as you are simply pausing the execution and keeping the state of the function intact. However, when you use the return keyword, you are exiting the function. Hence, no code written after the return statement is executed. You might have observed the same in the examples discussed above.

Also Read, How to get a single element from a generator in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *