Python Yield

Yield is a Python built-in keyword that returns the value(s) from a function. The execution of the function is not terminated. Rather, it returns the value to the caller and maintains the execution state of the function. The execution of the function is resumed from the last yield statement. The yield allows us to produce a sequence of values rather than one value. It is used inside a function body. The function that contains a yield statement is known as the generator function.

There are several advantages to yield keyword. For instance, it controls the memory allocation and saves the local variable state. However, it increases the complexity of the code.

This article explains the use of the yield keyword with examples.

Syntax of Yield

The yield syntax is simple and straightforward. The yield is initiated with the yield keyword and syntax as follows:

yield value

Examples

Now, let’s see examples to understand the use and works of yield statements. Traditionally, the return keyword terminates the execution of the program and return a value at the end, while yield returns the sequence of values. It does not store the value in memory and returns the value to the caller at run time. In the given below example, a generator function is defined to determine the leap year. A leap is that year when divisible by four returns zero as a remainder. The yield keyword returns the value of leap year to the caller. As it will get the value of leap year, it will pause the program execution, return the value, and then resume the execution from where it was stopped.

#declaring a generator to determine leap year
def leapfunc(my_list):
    for i in my_list:
        if(i%4==0):
            #using yield
            yield i
#declaring the years list
year_list=[2010,2011,2012,2016,2020,2024]
print("Printing the leap year values")
for x in leapfunc(year_list):
    print(x)

Output

The output shows the series of leap years.

Let’s see another example where the generator function yields various numbers and strings.

#declaring a generator function
def myfunc():
    yield "Mark"
    yield "John"
    yield "Taylor"
    yield "Ivan"
    yield 10
    yield 20
    yield 30
    yield 40
    yield 50
#calling and iterating through the generator function
for i in myfunc():
    #printing values
    print(i)

Output

Let’s implement a generator function to calculate and print the cube value of sequence of numbers. We are generating the cube values from 1 to 30.

#declaring a generator function to calculate cube value
def calcube():
    val=1
    #the infinite while loop
    while True:
            #calcumating cube
           yield val*val*val
           #incrementing value by 1
           val=val+1
print("The cube values are: ")
#calling the generator function
for i in calcube():
    if i>30:
        break
    print(i)

Output

The output shows the cube value less than 30.

Conclusion

Yield is a Python built-in keyword that does not terminate the execution of the program and generate a series of values. In comparison to the return keyword, the yield keyword produces multiple values and returns to the caller. This article explains the Python Yield with examples.



from Linux Hint https://ift.tt/33bmPYY

Post a Comment

0 Comments