{"id":125,"date":"2023-06-02T19:23:26","date_gmt":"2023-06-02T19:23:26","guid":{"rendered":"http:\/\/python.garden\/index.php\/2023\/06\/02\/loops-for-while\/"},"modified":"2023-06-02T19:41:26","modified_gmt":"2023-06-02T19:41:26","slug":"loops-for-while","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/06\/02\/loops-for-while\/","title":{"rendered":"Chapter 2.2: Loops &#8211; For and While"},"content":{"rendered":"\n<p>Understanding and using loops (for, while)<\/p>\n\n\n\n<p>In Python, loops are used to repeatedly execute a block of program statements. The two types of loops in Python are the <code>for<\/code> loop and the <code>while<\/code> loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>for<\/code> Loop<\/h2>\n\n\n\n<p>The <code>for<\/code> loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range) or other iterable objects. Iterating over a sequence is called traversal.<\/p>\n\n\n\n<p>Here&#8217;s a simple example of a <code>for<\/code> loop:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]\n\nfor fruit in fruits:\n    print(fruit)<\/pre><\/div>\n\n\n\n<p>In this example, <code>fruit<\/code> is the loop variable that takes each value in the <code>fruits<\/code> list. The block of code within the loop (here, the <code>print<\/code> statement) is executed once for each item in the list.<\/p>\n\n\n\n<p>You can also combine <code>for<\/code> loops with <code>range()<\/code> function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">for i in range(5):\n    print(i)<\/pre><\/div>\n\n\n\n<p>The <code>range()<\/code> function generates a sequence of numbers from 0 up to (but not including) the number specified as the parameter (in this case, 5).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>while<\/code> Loop<\/h2>\n\n\n\n<p>The <code>while<\/code> loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.<\/p>\n\n\n\n<p>Here&#8217;s a simple example of a <code>while<\/code> loop:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">i = 0\n\nwhile i &lt; 5:\n    print(i)\n    i += 1<\/pre><\/div>\n\n\n\n<p>In this example, the <code>print<\/code> statement will be executed as long as <code>i<\/code> is less than <code>5<\/code>. We then increment <code>i<\/code> by <code>1<\/code> after each loop iteration. If we didn&#8217;t do this, <code>i<\/code> would always be <code>0<\/code>, the condition would always be true, and the loop would run forever, creating an infinite loop.<\/p>\n\n\n\n<p>Remember, the loop variable (<code>i<\/code> in this case) needs to be defined before the loop is started, and you should make sure that something changes from iteration to iteration so that the loop condition will eventually become false. Otherwise, the loop will run forever, creating an infinite loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Loop Control Statements<\/h2>\n\n\n\n<p>Loop control statements change the normal sequence of flow in loop iterations. Python supports the following control statements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>break<\/code> statement: Terminates the loop statement and transfers execution to the statement immediately following the loop.<\/li>\n\n\n\n<li><code>continue<\/code> statement: Causes the loop to skip the remainder of its body and immediately start the next iteration.<\/li>\n\n\n\n<li><code>pass<\/code> statement: The pass statement in Python is used when a statement is required syntactically, but you do not want any command or code to execute.<\/li>\n<\/ul>\n\n\n\n<p>That&#8217;s the basic introduction to <code>for<\/code> and <code>while<\/code> loops in Python. These looping structures are among the most important constructs in programming. They give us the power to repeat certain operations, which can be particularly useful when working with large collections of data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding and using loops (for, while) In Python, loops are used to repeatedly execute a block of program statements. The two types of loops in Python are the for loop&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[99,118,94,117],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-control-structures","category-for-while","category-introduction-to-python-2","category-loops"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}