{"id":19526,"date":"2023-05-31T08:43:13","date_gmt":"2023-05-31T03:13:13","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=19526"},"modified":"2023-05-31T08:43:15","modified_gmt":"2023-05-31T03:13:15","slug":"python-for-loop","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/python-for-loop\/","title":{"rendered":"Python For Loop: Introduction, Syntax, and Examples"},"content":{"rendered":"\n<p>Any mathematician or physicist or statistician would be well-versed with running iterations. It is with these iterations that the desired result is obtained. When it comes to coding there are a handful of techniques through which iterations can be done.<\/p>\n\n\n\n<p>In this article, we shall explore the workings of the <em>for <\/em>loop in Python through each of the below sections:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Introduction to Python<em> for<\/em> loop<\/strong><\/li>\n\n\n\n<li><strong>Syntax of <em>for <\/em>loop<\/strong><\/li>\n\n\n\n<li><strong>Examples of using <em>for <\/em>loops<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python<em> <\/em>For Loop<\/h2>\n\n\n\n<p>A control flow statement that enables the iteration over a sequence until all items in the sequence have been processed is called as <em>for <\/em>loop. This allows you to execute a block of code for each item in the sequence. The iteration ends based on the completion of iterating through all the items in the sequence. The capabilities of the <em>for <\/em>loop extends towards iterating a large number of entities such as string or lists or tuples.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of For Loop<\/h2>\n\n\n\n<p>The <em>for <\/em>loop can be coded using a couple of syntaxes, each given below.<\/p>\n\n\n\n<p><strong>Syntax 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor val1 in val2:\n    &lt;statement block&gt;\n<\/pre><\/div>\n\n\n<p><strong>Syntax 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor val in range (&lt;startvalue&gt;, &lt;endvalue&gt;, &lt;jumps&gt;):\n    &lt;statement block&gt;\n<\/pre><\/div>\n\n\n<p>The latter syntax harnesses the potential of the <em>range <\/em>function which makes it easier to specify the boundaries within which the <em>for <\/em>loop ought to operate. It also provides the flexibility for specifying the increments at which the iterations are to be executed. <\/p>\n\n\n\n<p><strong>Syntax 3:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor val1 in val2:\n\t\tfor val3 in val4:\n\t\t\t&lt;statement block&gt;\n    &lt;statement block&gt;\n<\/pre><\/div>\n\n\n<p>Some might have guessed it by now. The above is the representation of the nested <em>for <\/em>loop. These are iterations within iterations such that the result obtained by \u2018x\u2019 iterations of the innermost condition would then be fed as an input for the \u2018y\u2019 iterations of the outermost condition.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of using For Loop<\/h2>\n\n\n\n<p>This section shall detail the different variations in which the standard <em>for <\/em>loop could be utilised in Python. Let us get started with how to use it with a list.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<p>Given below is a list, each of whose elements is to be incremented by one using the <em>for <\/em>loop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = &#x5B;20, 57, -10, 89]\nfor i in ar1:\n    print(i+1)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"288\" height=\"164\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Adding-using-for-loop-1.jpg\" alt=\"Adding One Using For Loop\" class=\"wp-image-19532\"\/><figcaption class=\"wp-element-caption\">Adding using <em>for <\/em>loop<\/figcaption><\/figure>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<p>Not only numbers but also strings can be iterated through <em>for <\/em>loop. In the below code, let us have a look at how a list of string is iterated using a <em>for <\/em>loop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsnacks = &#x5B;&quot;Samosa&quot;, &quot;Chips&quot;, &quot;Murukku&quot;]\nfor i in snacks:\n    print(i)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"386\" height=\"147\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Printing-elements-using-for-loop.jpg\" alt=\"Printing Elements Using For Loop\" class=\"wp-image-19534\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Printing-elements-using-for-loop.jpg 386w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Printing-elements-using-for-loop-300x114.jpg 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><figcaption class=\"wp-element-caption\">Printing elements using <em>for <\/em>loop<\/figcaption><\/figure>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<p>In both examples, we have mentioned <em>\u2018i\u2019 <\/em>after <em>for, <\/em>which represents each entity within the list that is to be put through the loop. Now let us have a look at how a <em>for <\/em>loop and the <em>range <\/em>function can be put to work together. The below code feeds the data within which the iteration is to be carried out using the <em>range <\/em>function. It states that the data shall start from \u20181\u2019 and end at \u201890\u2019 in increments of \u201810\u2019.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor i in range(1,90,10):\n    print(i\/2)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"285\" height=\"230\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Using-for-loop-and-range-function.jpg\" alt=\"Using For Loop And Range Function\" class=\"wp-image-19537\"\/><figcaption class=\"wp-element-caption\">Using <em>for <\/em>loop and <em>range <\/em>function<\/figcaption><\/figure>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<p>Finally, let us get on with the workings of a nested <em>for <\/em>loop through the below code. Here the numbers from the range specified in the first <em>for <\/em>loop will be printed the same number of counts they stand for (i.e.) the number \u20182\u2019 gets printed 2 times &amp; so on &amp; so forth.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor i in range(2,9,2):\n    for j in range(i):\n        print(i, end = &#039; &#039;)\n    print()\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"294\" height=\"182\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Nested-for-loop.jpg\" alt=\"Nested For Loop\" class=\"wp-image-19539\"\/><figcaption class=\"wp-element-caption\">Nested <em>for <\/em>loop<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on the different variations in which <em>for <\/em>loop can be put to use in Python. Here\u2019s another article that can be our definitive guide to the <a href=\"https:\/\/codeforgeek.com\/python-variables\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/python-variables\/\" rel=\"noreferrer noopener\"><em>variables <\/em>in Python.<\/a> There are numerous other enjoyable and equally informative articles in <em><a href=\"https:\/\/codeforgeek.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/\" rel=\"noreferrer noopener\">CodeforGeek<\/a><\/em> that might be of great help for you to gain valuable insights. Until then, <em>ciao!<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/wiki.python.org\/moin\/ForLoop\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/wiki.python.org\/moin\/ForLoop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Any mathematician or physicist or statistician would be well-versed with running iterations. It is with these iterations that the desired result is obtained. When it comes to coding there are a handful of techniques through which iterations can be done. In this article, we shall explore the workings of the for loop in Python through [&hellip;]<\/p>\n","protected":false},"author":90,"featured_media":19554,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-19526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/05\/Python-For-Loop.png",1000,600,false]},"uagb_author_info":{"display_name":"Arulius Savio","author_link":"https:\/\/codeforgeek.com\/author\/arulius\/"},"uagb_comment_info":0,"uagb_excerpt":"Any mathematician or physicist or statistician would be well-versed with running iterations. It is with these iterations that the desired result is obtained. When it comes to coding there are a handful of techniques through which iterations can be done. In this article, we shall explore the workings of the for loop in Python through&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/19526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=19526"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/19526\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/19554"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=19526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=19526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=19526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}