{"id":5274,"date":"2020-10-02T16:59:59","date_gmt":"2020-10-02T11:29:59","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=5274"},"modified":"2020-10-02T17:00:01","modified_gmt":"2020-10-02T11:30:01","slug":"yield-in-python-tutorial","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/yield-in-python-tutorial\/","title":{"rendered":"Yield in Python Tutorial"},"content":{"rendered":"\n<p>While working with a large amount of data the programmer sometimes needs to control the function\u2019s start and stop state. The Yield gives the programmer the power to execute the program the way he\/she wants rather than computing them at once. Let\u2019s take a look at an example. Suppose you have a function that prints \u201cHello World\u201d 5 times and functions that Yields \u201cHello World\u201d five times. Let\u2019s take a look at the difference between Yield and simple return for Yield in <a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\">Python<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code>def normal():<br>print(\"Hello World\")<br>print(\"Hello World\")<br>print(\"Hello World\")<br>print(\"Hello World\")<br>print(\"Hello World\")<br>def yielding():<br>yield \"Hello World\"<br>yield \"Hello World\"<br>yield \"Hello World\"<br>yield \"Hello World\"<br>yield \"Hello World\"<br><br>normal()<br>print(\"Now Calling Yeilding function\")<br>print(yielding())<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/zOhS2lDqgOgebMmI6sM3xL6YWGYMmAh8tjhrHvgwGM9xEW_LNdTd-jnfJ0eMWq_Y2hK54PYBXBKjnu3x3viuKDzenfbIjMb2ef7CXglDHhI_7nfFAzXU53rZKBkz150oNKZutBK8dpTKk6BnDw\" alt=\"Yield in Python\" title=\"\"><\/figure>\n\n\n\n<p>Take a look at the above output. The normal function gives us back 5 print statements but the yielding function gives a generator.<\/p>\n\n\n\n<p>Now we need to loop over the generator to get the output.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code>def yielding():<br>yield \"Hello World\"<br>yield \"Hello World\"<br>yield \"Hello World\"<br>yield \"Hello World\"<br>yield \"Hello World\"<br><br>print(\"Now Calling Yeilding function\")<br>output = yielding()<br>for i in output:<br>print(i)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/NOWijxQ5vIymA1Jb1OMHu0XeMmt3TZh3Dr-J20Wdzulwece1qkeFk2DeNlWSF03HaTUjXvQ0L2TSvrkECkGGz6Biot2nHIpofB7rVLwJ-8rcQ_UJ_GI7nuwB01aOsoeNGz2gDqtBUShLWPl83g\" alt=\"yield Hello World\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to read the values from the generator?<\/h2>\n\n\n\n<p>Let\u2019s take a look at different ways to read values from generators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using : list()<\/h3>\n\n\n\n<p>We can use the list function to generate all the output and store it in a list.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code>def even_num(x):<br>for i in range(x):<br>&nbsp; if (i%3==0):<br>&nbsp; &nbsp; &nbsp; yield i<br>num = even_num(20)<br>print(num)<br>print(list(num))<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/mpHTBh0jqGD5CImdKa8nzbUsPFppYhJISwikwQXFKi0as7kyP5-cqse22NQYHICFDmWQslfQO89vLHEBJcDC9vxoHtklfetJ1lLaAwGBLI9ErNxBguDCcwkxBD6_sX3SqC3SNWwxq-cjA02YLQ\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Using : for-loop<\/h3>\n\n\n\n<p>In Yield in Python, We used for loop to iterate over the generator in the first example. Let\u2019s take another example.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code>def even_num(x):<br>for i in range(x):<br>&nbsp; if (i%3==0):<br>&nbsp; &nbsp; &nbsp; yield i<br>num = even_num(20)<br>print(num)<br>for i in num:<br>print(i)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/ah1eqxoyga9Qk1iu7AhvYx90YLsfaKgO7MHFvf0iWLbBHntZPPtYW3qlW0KBcG2eKJAOxuVkgFvFaB8cnMk0-9Jk39vAsU0KOipMoN8KX4fR_vmaMzQpREkBOD3vWDab4fcB2R62loJYBVRQkw\" alt=\"yield Hello World\" title=\"\"><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using next()<\/strong><\/h3>\n\n\n\n<p>There is a <a href=\"https:\/\/www.webopedia.com\/TERM\/B\/built_in_function.html#:~:text=A%20function%20that%20is%20built,in%20a%20row%20or%20column.\" rel=\"nofollow noopener\" target=\"_blank\">built-in function<\/a> next which is used to generate the next number from generator. After generating the last item it will give an error.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><code>def even_num(x):<br>for i in range(x):<br>&nbsp; if (i%3==0):<br>&nbsp; &nbsp; &nbsp; yield i<br>num = even_num(10)<br>print(num)<br>print(next(num))<br>print(next(num))<br>print(next(num))<br>print(next(num))<br>print(next(num))<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/4wbtVSppTZDvV8Wwx2rfF7TtShUcHcXbj5hRk7vyZHLmQd3wBCEjVYOBqewt1WRXgsV4HeEzA8zQ_JqKdDMexUXSXVtRA0-dXUjl4aPJwpoAA7gsEL-lOBs_TOVoA8eMYV9DvWmBWtXwhGfcDw\" alt=\"\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>While working with a large amount of data the programmer sometimes needs to control the function\u2019s start and stop state. The Yield gives the programmer the power to execute the program the way he\/she wants rather than computing them at once. Let\u2019s take a look at an example. Suppose you have a function that prints [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5317,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-5274","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=5274"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/5274\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/5317"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=5274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=5274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=5274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}