yield keyword in python by R4R Team

- Yield is a keyword that is used like return, except the function will return a generator.
-when you call the function, the code you have written in the function body does not run.
-It return the list object.


program-

#define function
def square():
list=[1,2,3,4]
for i in list:
yield i*i

#First way to call function and print yield value
x=square()
for i in x:
print(i)

#second way to print yield value
print(*square())


output-

1
4
9
16
1 4 9 16


-In this program, we have a list [1,2,3,4] inside the 'square()' function then we yield each item of the list so it return the list with the generator.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!