You are reading the article Understand 5 Examples Of Python Yield Statement updated in September 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Understand 5 Examples Of Python Yield StatementIntroduction to Python yield Statement
In Python, yield is the keyword that works similarly as the return statement does in any program by returning the function’s values. As in any programming language, if we execute a function and it needs to perform some task and give its result to return these results, we use the return statement. The return statement only returns the value from the function, but yield statements also return multiple values by returning one value and wait, then it again resumes by saving that local state. Such functions that use yield statements instead of return statements are known as generator functions. These generator functions can have one or more yield statements.
Start Your Free Software Development Course
Web development, programming languages, Software testing & othersWorking of yield Statement
Syntax:def function_name: statement (s) yield statement (s) Examples of Python yield
Let’s take a simple and easy example to understand the yield statement:Example #1
Code:def yield_function(): yield 10 yield 20 yield 30 for y in yield_function(): print(y)
From the above example, we can see the yield_function(), which wants to return more than one value, so in this case, return statement cannot be used, but to do so, we can use yield statement to print or return more than one value from the function. So yield statements are usually used on the functions that are called as generator function because the yield statement is used when we want to iterate over a sequence of values to be returned by the single function, but you do not want to save all these values in memory which means as a how the yield statement generates value to be returned each time the memory is overwritten as to it iterates and returns all the value without using memory to all the values which yield statement returns.
To print iterable values, we use for loop in normal functions. The generator function is also like a normal function, but if we use yield statements, then it generator function needs to print the iterable values returned by the functions.Example #2
Code:def yield_func(): n = range(3) for i in n: yield i*i gen = yield_func() print(gen) for i in gen: print(i)
Now let us see an example with the code that demonstrates generating generator objects and printing the return values using a loop.
Code:def gen_func(): x = 0 while x < 5: yield x x += 1 print("Simple function call without using loop:n") print(gen_func()) print("n") print("Below is with using a loop:") for y in gen_func(): print(y)
In this above code, the gen_func() when it is called for the first time it returns ‘0’ as the value. Next, when it is called, the value returned previously is incremented by 1 as inside the function’s code, and then it returns ‘1’. Again the value of x is incremented and returns ‘2’ as a value; this loop continues till less than 5 as mentioned in the while loop above in the code. So it prints values ‘0’, ‘1’, ‘2’, ‘3’, ‘4’.
The main points why yield statements can be used instead of the return statement:
Firstly we can easily create a function that is iterable using yield, which is also called a generator function.
Whenever there are continuous calls made to a function, it starts from the last yield statement itself, so you can again save time.
Lastly but very important, the yield statement is used when you want to return more than one value from the function. So the return value from the yield statement stores data in a local state so that the allocation of memory is also saved and how every time the different value is returned. Hence from this, even the memory is also saved.
In this topic, when the function is called after it has completed the loop, then we will get an error, and this error can be caught and raise the error by using the next() method, which can be shown in the below example.Example #4 def yield_func(l): total = 0 for n in l: yield total total += n new_lst = yield_func() print(next(new_lst)) print(next(new_lst)) print(next(new_lst))
So in the above, we get StopIteration error, and it can be done using the next() method. This is done as below.Example #5
Code:def yield_func(l): total = 0 for n in l: yield total total += n new_lst = yield_func([10,20,30]) print(next(new_lst)) print(next(new_lst)) print(next(new_lst))
Like other programming languages, Python can return a single value, but in this, we can use yield statements to return more than one value for the function. The function that uses the yield keyword is known as a generator function. So this function can be used when you want the iterable values to be returned. The yield statement not only helps to return more than one value, but it also saves time and memory by using more functions, and it can save the memory as every time the function is called, it stores its value in local memory, and it uses it again for the next call.Recommended Articles
We hope that this EDUCBA information on “Python yield” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Understand 5 Examples Of Python Yield Statement
Update the detailed information about Understand 5 Examples Of Python Yield Statement on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!