Python Yield vs. Return

Python is the widely used general-purpose programming language of recent times. The yield is a built-in Python keyword that is used to create the generator functions. Its function generates a series of results. It pauses the execution of the program, sends the result value back to the caller, and resume the execution from the last yield. Besides that, the yield function sends the generated series of results in the form of a generator object. On the other hand, the return is also a built-in keyword in Python that terminates the function and sends the value back to the caller.

This article outlines the differences between the yield and returns with examples.

Differences between Yield and Return

To begin with, there are many prominent differences between the yield and return. Let’s first discuss what are these.

Return Yield
The return statement returns only a single value to the caller. The yield statement can return a series of results to the caller in the form of a generator object.
The return exits the function, and in the case of a loop, it dismisses the loop. It is the last statement to be placed inside the function. It does not abolish the function’s local variables. It suspends the execution and send the value back to the caller, and continue the execution of the program from the last yield statement.
Logically, a function should have only a return statement. There could be more than one yield statement inside the function.
The return statement can only run one time. The yield statement can run multiple times.
The return statement is placed inside a regular Python function. The yield statement converts a regular function into a generator function.

Example1: Return vs. Yield

Now, let’s see the difference between return and yield statements through examples. In the example program given below, we have used multiple return statements. You can observe that the execution of the program will terminate after the first return statement, and the rest of the code will not be executed.

#a program to show the working of the return statement

#defining a number variable

num1 =10

#defining a number variable

num2=20        

#creating a function to perform arithmetic operations

def mathOP():

    #calculating the sum value

    return num1+num2

    #calculating the difference

    return num1-num2

    #calculating the multiplication value

    return num1*num2

    #calculating the division value

    return num1/num2

#calling the function

print(mathOP())

Output

In the output, it can be seen that the function only returns the first value, and the program is terminated.

To perform a similar task with multiple return statements, we need to create four different functions for each type of arithmetic operation.

#a program to show the working of the return statement

#defining a number variable

num1 =10

#defining a number variable

num2=20

#creating a function to perform arithmetic operations

def sumOP():

    #calculating the sum value

    return num1+num2

def subtractOP():

    #calculating the difference

    return num1-num2

def multiplicationOP():

    #calculating the multiplication value

    return num1*num2

def divisionOP():

    #calculating the division value

    return num1/num2

#calling the sum function

print("The sum value is: ",sumOP())

#calling the subtraction function

print("The difference value is: ",subtractOP())

#calling the multiplication function

print("The multiplication value is: ",multiplicationOP())

#calling the division function

print("The division value is: ",divisionOP())

Output

We can perform these multiple arithmetic operations inside a single generator function with multiple yield statements.

#a program to show the working of yield statement

#defining a number variable

num1 =10

#defining a number variable

num2=20        

#creating a function to perform arithmetic operations

def mathOP():

    #calculating the sum value

    yield num1+num2

    #calculating the difference

    yield num1-num2

    #calculating the multiplication value

    yield num1*num2

    #calculating the division value

    yield num1/num2

#calling the function

print("Printing the values:")

#using for loop to access the values from the generator object

for i in mathOP():

    print(i)

Output

Example2: Return vs. Yield

Let’s see another example of return and yield statements. In the given example, we have a list of numbers which is passed to the mod() function as an argument. We are performing the modulus operation on each number of list and checking what are those numbers when divided by 10 return zero as a remainder value.

First, let’s implement this example in our Python script with return statement.

#defining a list of numbers

myList=[10,20,25,30,35,40,50]

#defining a function to perform the modulus operation

def mod(myList):

    for i in myList:

        #performing modulus operation

        if(i%10==0):

            return i

print(mod(myList))

Output

The return statement only return the first number to the caller and terminates the execution of function.

Now, let’s implement the same example in our Python script with the yield statement.

#defining a list of numbers

myList=[10,20,25,30,35,40,50]

#defining a function to perform the modulus operation

def mod(myList):

    for i in myList:

        #performing modulus operation

        if(i%10==0):

            #the yield statement

            yield i

for i in mod(myList):

    print(i)

Output

Conclusion

In conclusion, the return and yield are two built-in Python keywords or statements. The return statement is used to return the value to the caller from a function and terminates the execution of the program, whilst the yield statement produces a generator object and can return the multiple values to the caller without terminating the execution of the program. This article list down all the prominent differences between return and yield statements with examples.



from Linux Hint https://ift.tt/3kG0cle

Post a Comment

0 Comments