{"id":37795,"date":"2016-09-15T09:12:51","date_gmt":"2016-09-15T14:12:51","guid":{"rendered":"http:\/\/karinarava.wordpress.com\/?p=54"},"modified":"2016-09-15T09:12:51","modified_gmt":"2016-09-15T14:12:51","slug":"use-of-loops-with-while-and-loops-with-for","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/use-of-loops-with-while-and-loops-with-for\/","title":{"rendered":"Use of loops with \u201cwhile\u201d and loops with \u201cfor\u201d"},"content":{"rendered":"<p class=\"syndicated-attribution\"><em> --Originally published at <a href=\"https:\/\/karinarava.wordpress.com\/2016\/09\/15\/use-of-loops-with-while-and-loops-with-for\/\"><a href=\"https:\/\/karinarava.wordpress.com\">Start in the world of the #TC101 <\/a><\/a><\/em><br \/><\/p>\n<h2><a href=\"https:\/\/www.cs.uky.edu\/~keen\/115\/Haltermanpythonbook.pdf\">Loops in Think Python<\/a><\/h2>\n<h2>Loops with &#8220;for&#8221;<\/h2>\n<p>Python provides a clean iteration syntax. Note the colon and indentation.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre><code class=\"python\">&gt;&gt; <span class=\"keyword\">for<\/span> i <span class=\"keyword\">in<\/span> range(<span class=\"number\">0<\/span>, <span class=\"number\">3<\/span>):\n&gt;&gt;     <span class=\"keyword\">print<\/span>(i*<span class=\"number\">2<\/span>)\n<span class=\"number\">0<\/span>\n<span class=\"number\">2<\/span>\n<span class=\"number\">4\n<\/span><\/code><\/pre>\n<h2>Loops with &#8220;while&#8221;<\/h2>\n<p>A While loop permits code to execute repeatedly until a certain condition is met. This is useful if the number of iterations required to complete a task is unknown prior to flow entering the loop.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre><code class=\"java\"><span class=\"keyword\">while<\/span> condition:\n    <span class=\"comment\">\/\/do something<\/span><\/code><\/pre>\n<pre><code class=\"python\">&gt;&gt; looping_needed = <span class=\"built_in\">True<\/span>\n&gt;&gt;\n&gt;&gt; <span class=\"keyword\">while<\/span> looping_needed:\n&gt;&gt;     <span class=\"comment\"># some operation on data<\/span>\n&gt;&gt;     <span class=\"keyword\">if<\/span> condition:\n&gt;&gt;          looping_needed = <span class=\"built_in\">False\n<\/span><\/code><\/pre><br \/>  <a rel=\"nofollow\" href=\"http:\/\/feeds.wordpress.com\/1.0\/gocomments\/karinarava.wordpress.com\/54\/\"><img alt=\"\" border=\"0\" src=\"http:\/\/feeds.wordpress.com\/1.0\/comments\/karinarava.wordpress.com\/54\/\" \/><\/a> <img alt=\"\" border=\"0\" src=\"https:\/\/pixel.wp.com\/b.gif?host=karinarava.wordpress.com&#038;blog=115206627&%23038;post=54&%23038;subd=karinarava&%23038;ref=&%23038;feed=1\" width=\"1\" height=\"1\" \/>","protected":false},"excerpt":{"rendered":"<p>&#8211;Originally published at Start in the world of the #TC101 Loops in Think Python Loops with &#8220;for&#8221; Python provides a clean iteration syntax. Note the colon and indentation. Example &gt;&gt; for i in range(0, 3): &gt;&gt; print(i*2) 0 2 4 Loops with &#8220;while&#8221; A While loop permits code to execute repeatedly until a certain condition &hellip; <a href=\"https:\/\/kenscourses.com\/tc101fall2016\/syndicated\/use-of-loops-with-while-and-loops-with-for\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Use of loops with \u201cwhile\u201d and loops with \u201cfor\u201d<\/span><\/a><\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[36],"class_list":["post-37795","post","type-post","status-publish","format-standard","hentry","category-blogs","category-syndicated","tag-tec"],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/comments?post=37795"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37795\/revisions"}],"predecessor-version":[{"id":37796,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/posts\/37795\/revisions\/37796"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/media?parent=37795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/categories?post=37795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2016\/wp-json\/wp\/v2\/tags?post=37795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}