{"id":237,"date":"2020-11-10T10:19:32","date_gmt":"2020-11-10T10:19:32","guid":{"rendered":"http:\/\/lamiaiftekhar.com\/?page_id=237"},"modified":"2020-11-10T10:19:32","modified_gmt":"2020-11-10T10:19:32","slug":"20-minutes-python-day-17","status":"publish","type":"page","link":"https:\/\/lamiaiftekhar.com\/index.php\/20-minutes-python-day-17\/","title":{"rendered":"20-Minutes Python: Day 17"},"content":{"rendered":"<body>\n<p>This is the last day focusing on loop basics and we will learn about an interesting feature in Python that we haven\u2019t seen in C++ loops!<\/p>\n\n\n\n<p>\u2018Else\u2019 in loops!<\/p>\n\n\n\n<p>In Python, there can be an else clause in loops (just like in if-else conditions). The \u2018else\u2019 clause is executed if the loop condition fails.<\/p>\n\n\n\n<p>If there is \u2018break\u2019 in the loop and the break condition is executed, then the \u2018else\u2019 clause is skipped totally.<\/p>\n\n\n\n<p>However, if there is \u2018continue\u2019 in the loop, and the \u2018continue\u2019 condition is executed, the \u2018else\u2019 clause is still considered.<\/p>\n\n\n\n<p>This may be a bit confusing but is usually helpful in \u2018search\u2019 loops. If we are searching for an item and we do not find it in the loop, the else clause can be executed.<\/p>\n\n\n\n<p>According to the official Python docs:<br>Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>First let\u2019s do some revision. Look at the following code, and predict what may be the output. Then type and run to check whether you got it correct or not<\/li><\/ol>\n\n\n\n<p><strong>for i in range(4):<br>print(\u201cValue of i is %d\u201d %i)<br>else:<br>print(\u201cDone with iteration\u201d)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>That was a for loop revision. Now let\u2019s refresh our while loop concept as well as the rand number generation skill . Type and run the following. (Try to predict beforehand how the output may look like \u2013 you can\u2019t predict the exact output as there is a random number generator, but you can figure out what the general look would be)<\/li><\/ol>\n\n\n\n<p><strong>from random import randint #we are importing a function \u2018randint\u2019<br>#from a special library \u2018random\u2019<br>#that will help us generate random numbers<\/strong><\/p>\n\n\n\n<p><strong>x=0<br>while x&lt;4:<br>age= randint(20,30) # generate a number between 20 to 30<br>print(\u201cage is %d\u201d %age)<br>x = x+1<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Now, we will put at \u2018else\u2019 clause at the end of the while loop. Look what happens when you type and run the following<\/li><\/ol>\n\n\n\n<p><strong>from random import randint #we are importing a function \u2018randint\u2019<br>#from a special library \u2018random\u2019<br>#that will help us generate random numbers<\/strong><\/p>\n\n\n\n<p><strong>x=0<br>while x&lt;4:<br>age= randint(20,30) # generate a number between 20 to 30<br>print(\u201cage is %d\u201d %age)<br>x = x+1<br>else:<br>print(\u201cWe are done looking at the age of %d people\u201d %x)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Actually, in #3, you could have not written the \u2018from random import randint..\u2019 part as you have already imported the randint function for the current kernel in your code for #2 (hopefully your kernel is not ruined as you proceeded from #2 to #3).<\/li><\/ol>\n\n\n\n<p>Ok, now we will use another function from \u2018sample\u2019 from that library ( so we have to import it) and use it on a new example to see how to use \u2018else\u2019 with a for loop. Sampling also generates random numbers, but it generate a list according to the length you request and ensures all numbers in the list are unique.<\/p>\n\n\n\n<p>Type and run the following. Well, run it several times and see the results each time.<\/p>\n\n\n\n<p><strong>from random import sample # We are importing a function \u2018sample\u2019<br># from a special library \u2018random\u2019<br># that will help us generate a LIST of random numbers with no repetition<\/strong><\/p>\n\n\n\n<p><strong>aRandomList=sample(range(20,30), 5) # Make a list of 5 numbers, sampling from 20 to 30<\/strong><\/p>\n\n\n\n<p><strong>print(aRandomList)<\/strong><\/p>\n\n\n\n<p><strong>for x in aRandomList:<br>print(x)<br>if x==27:<br>break<br>else:<br>print(\u201cNobody in the 27 Club!\u201d)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>Now get back to while loop. Check out the simple code below. Try to predict what will be the output. Then type and run.<\/li><\/ol>\n\n\n\n<p><strong>n = 5<br>while n &gt; 0:<br>n = n \u2013 1<br>if n == 2:<br>break<br>print(n)<br>else:<br>print(\u201cLoop has ended\u201d)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\"><li>EXERCISE: Modify the code in #5 by changing ONLY ONE WORD (no number) , such that your output is as follows:<\/li><\/ol>\n\n\n\n<p><strong>4<br>3<br>1<br>0<br>Loop has ended<\/strong><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>This is the last day focusing on loop basics and we will learn about an interesting feature in Python that we haven\u2019t seen in C++ loops! \u2018Else\u2019 in loops! In Python, there can be an else clause in loops (just like in if-else conditions). The \u2018else\u2019 clause is executed if the loop condition fails. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-237","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":1,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/237\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/237\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/media?parent=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}