Javatpoint Logo
Javatpoint Logo

Yield Keywords in Python

Numerous tools in Python make life for programmers much easier. The yield keyword in Python is one such instrument. In typical Python processes, this keyword can be used in place of return statements. We will cover the yield keyword, its use in generator functions, the distinction between a return statement and a yield statement, and the circumstances in which we can use a yield statement in place of a return statement.

What is the Yield Keyword in Python?

Python's yield keyword is like another one we use to return an expression or object, typically in functions, called return. There is a small amount of fluctuation, though. The yield statement of a function returns a generator object rather than just returning a value to the call of the function that contains the statement.

The Python interpreter halts execution of the function when a yield statement is encountered when calling a function in a programme. The generator class returns an object to the caller.

To put it another way, any expression that is passed to the yield keyword will be converted into a generator object, which will subsequently be returned to the caller. As a result, to acquire the values, we must repeatedly access the generator object.

Please note that a generator function is a function that uses the term yield. The local variables' statuses won't be ruined by the yield keyword. Every time a function is called, the most recent yield expression will be used as the beginning point for the execution.

There are new variables created each time we call a function with a return statement. If a generator function is used in place of a standard function, the function's execution will pick up where it left off.

To return several functions through a function, we can combine the generator function and the yield keyword. Multiple values are returned by the yield expression. They give one value in return, pause the execution, save the local state of the stated variable, and then resume.

Syntax of the yield Keyword in Python

An overview of Python's generator functions

In contrast to conventional functions in Python that only return a single expression, generator functions return an iterable object that we refer to as a generator object. We can approach or read the values from the generator function using a simple Python loop, a method like next() or list(), or the variables kept inside a generator object one at a time. We used the def keyword to create a generator function. The example given below is a nice one.

A yield keyword example using a generator function

When we call the functions, specific strings will be printed.

Code

Output:

<class 'generator'>
Yield
Keyword
in
Python

We created a simple generator function in the program, and after utilising several yield expressions to return several values, Python then saved within a generator object we created. A loop through the object may then be used to display the values contained in this generator function on the console.

Sorting Strange Numbers, the use of yield Keywords

Let's create a brand-new generator function that makes use of the yield command. From a list of integers, we'll try to remove any odd number. The information stored inside the generator object must also be output using a variety of methods, including list(), for-in, and next().

Output:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33]

How are functions with yield called?

With yield, we can invoke functions rather than just returning values. Imagine, for example, that we create a function called square() that, when called, returns the square of a given number. The term return is used in this code. One more function, square(), uses the keyword yield to provide squares across a range of integers. In this case, we can combine the yield expression with the square() function to create a simple program. In the part below, look at the example code.

Code

Output:

[0, 1, 4, 9, 16, 25, 36, 49]

What Sets Return Keyword Apart from Yield Keyword

The yield keyword in Python functions similarly to the return keyword used for values, with the exception that the generator function that uses the yield keyword returns a generator object rather than just a value. The return keyword in Python, which terminates the execution of the function, is their primary difference. However, the yield keyword postpones the function's execution. Another difference is that statements after return keywords are never executed. Yield statements are executed as the function continues to run.

When one to use a yield instead than return Keywords?

The keyword yield stops the function's execution, returns a value, and leaves enough state for the generating function to resume execution where yield left off. The function resumes execution when execution is continued exactly where the preceding yield execution left off. This allows its programme to output a series of values over time rather than calculating values simultaneously and returning them as a list data type in output.

Pros from Using Yield:

  • Because it preserves local variable states, memory allocation load is kept under control.
  • The procedure doesn't have to restart because the previous state is preserved, which saves time.

Cons of using yield:

  • The function must be called properly. If not, using yield could occasionally become inaccurate.
  • Time and storage optimisation lead to a rise in coding complexity, whose justification is oftentimes elusive.

Output:

Function with return keyword:  3.0
Function with yield keyword:  [0.0, 1.0, 1.4142135623730951]

While the keyword yield may produce several results, return gives its caller a specific value. We must use yield if we want to loop through a series but don't want to retain the entire series in memory.

Yield return output is used by Python generator functions. The yield keyword is used in place of the return keyword when a generator function that utilises the def keyword needs to generate a value. The function becomes a generator function when the yield is incorporated into a definition's body.

List of distinctions between return and yield keywords

The differences between Python's yield and return are listed here.

Point of Distinction Produced Keyword Back Keyword
Def When a generator function is called, all the values returned by the yield keyword are combined to form a generator object. Code doesn't start running until the caller has iterated through the object, as well. Once it reaches the return statement, the function execution terminates and only one value is returned to us.
Function When we call the generator function, the initial yield is carried out, and the function ends. We are then handed the generator object, which holds the value returned by yield. When we need to access or iterate over this variable, the next yield statement is then executed, and the cycle is repeated. The execution of a normal function begins when we call it and ends when it reaches the return statement. After that, we receive the value back.
Quantity of Keywords for Each Function We can employ a lot of yield statements in generator functions. One return statement can be used in a normal function.
Storage It is not necessary to allocate any storage when using yield keywords. Every returned value is assigned memory.
Applications Extremely memory-efficient, especially when handling large data sets. This keyword should be applied to smaller data sets.
Execution Execution times for large data sets are sped up by using the yield keyword. Because more calculation is needed, larger data sizes cause longer execution times.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA