Answer: The “yield” keyword in Python is used to turn a function into a generator.
By using the ‘yield’ keyword, the generator returns multiple values, one at a time, instead of returning all the values at once. It also stops the execution of the function once all the values are returned. In this blog let’s explore the multiple ways how the ‘yield’ keyword in Python can be used along with its functionalities.
Table of Contents:
Features of the ‘yield’ keyword
- Pause and resume: The ‘yield’ keyword allows you to pause the function and resume it later. And allowing you to produce multiple values one at a time.
- Memory efficient: This keyword generates values without storing them all in the memory which makes it more memory efficient.
- Iterable: Function when used with the ‘yield’ keyword returns the generator that you can loop through easily.
- State saving: It remembers the function state between the pauses, so you can continue from where you left off.
- Multiple values: Allows you to get a sequence of numbers over time instead of getting them all at once.
Advantages of the ‘yield’ keyword
- Producing the items when needed, can make the program more efficient and responsive.
- Values are computed as per the requirement, which can save resources and improve performance in scenarios where all generated values are not required.
- Generators with `yield` make code easier to read and shorter than traditional methods.
Usage of ‘yield’ Keyword in Python
Now, we’ll discuss about some of the usages of the ‘yield’ keyword in Python:
Usage 1: Generator Function and ‘yield’ Keyword in Python
This shows how a generator function works and how the yield keyword is used to return values from the function one at a time.
Example 1:
Output:
Example 2:
The display_status() function is a generator. It uses yield to pause and return control to the caller. Here we can resume the function where it has stopped.
The program prints the next status only when the next() function is called.
Output:
Usage 2: Using the “yield” Keyword as a Boolean
You can use the ‘yield’ keyword along with Boolean logic (like if statements) to control what values the generator function returns. Depending on the condition, it can yield different values or pause at different points.
Example 1:
Output:
Usage 3: To Generate an Infinite Sequence using the ‘yield’ Keyword
The infinite sequence of numbers is generated using the ‘yield’ keyword, which returns the number by incrementing it with +1. Using the ‘yield’ keyword is better than traditional looping because it is memory efficient as it stores only the last generated value.
Example 1:
Output:
Usage 4: Using the ‘yield’ Keyword in the List
The ‘yield’ keyword is used to go through a list and extract only the odd numbers. Instead of returning all the numbers at once, it yields each odd number one by one.
Example 1:
Output:
Difference Between the ‘yield’ and ‘return’ Keywords in Python
Features | yield keyword | return keyword |
Purpose | Pauses the function and return a generator | End the function and return the value |
State | Saves the function state | Does not save the state |
Iteration | Produces a sequence of value | Produces a single-value |
Memory usage | More memory efficient | Uses more memory when returning large data |
Use case | Useful for large data sets | Useful for single results or small data sets |
Conclusion
The “yield” keyword in Python is used to create generator functions, allowing efficient memory use, and can generate values one at a time. Understanding the examples discussed above, helps you use the ‘yield’ keyword in an effective way in Python.