{"id":14472,"date":"2021-07-11T11:36:17","date_gmt":"2021-07-11T06:06:17","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=14472"},"modified":"2021-07-11T11:36:20","modified_gmt":"2021-07-11T06:06:20","slug":"for-vs-while-loop-python","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/","title":{"rendered":"Comparing for vs while loop in Python"},"content":{"rendered":"\n<p>Loops are one of the most basic entities inside a given programming language. With loop statements, we can execute a given piece of code till a certain condition is fulfilled. Once the condition becomes false, the program will then stop iterating over the loop statement. It will then execute the code after the loop statement. We can use loops to iterate over a given sequence, such as a list, a dictionary, a tuple, etc., or we can use it to execute a piece of code repeatedly. If you are new to python, then this article will be great for you in order to understand the difference between the two loops in python &#8211; <strong>for vs while loop.<\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#For_vs_While_Loop_in_Python\" >For vs While Loop in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#Types_of_loops_in_Python\" >Types of loops in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#The_While_Loop_In_Python\" >The While Loop In Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#The_syntax_of_pythons_while_loop_is\" >The syntax of python&#8217;s while loop is:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#The_For_Loop_In_Python\" >The For Loop In Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#The_syntax_of_for_loop_in_python_is\" >The syntax of for loop in python is:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#For_loop_vs_while_loop_python\" >For loop vs while loop python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#for_vs_while_loop_Python_Speed_Comparison\" >for vs while loop Python [Speed Comparison]<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#Byte_Code_Approach\" >Byte Code Approach<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#Practical_Approach\" >Practical Approach<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#Trending_Right_Now\" >Trending Right Now<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-for-vs-while-loop-in-python\"><span class=\"ez-toc-section\" id=\"For_vs_While_Loop_in_Python\"><\/span>For vs While Loop in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Basis of Comparison<\/strong><\/td><td><strong>For Loop<\/strong><\/td><td><strong>While Loop<\/strong><\/td><\/tr><tr><td>Declaration<\/td><td><code>for object in sequence:<br>&lt;indentation&gt;body<\/code><\/td><td><code>while condition:<br>&lt;indentation&gt;body<\/code><\/td><\/tr><tr><td>Format<\/td><td>Initialization, condition checking, and iteration statements are written at the top<\/td><td>Only initialization and condition checking is written at the top<\/td><\/tr><tr><td>Use Case<\/td><td>When you already know the number of iterations<\/td><td>When you don&#8217;t know the number of iteration.<\/td><\/tr><tr><td>Iteration<\/td><td>Every element is fetched through the iterator\/generator. Example, range()<\/td><td>Every element is explicitly incremented or decremented by the user<\/td><\/tr><tr><td>Generator Support<\/td><td>For loop can be iterated on generators in Python.<\/td><td>While loop cannot be iterated on Generators directly.<\/td><\/tr><tr><td>Disassembly<\/td><td>For loop with range() uses 3 operations. range() function is implemented in C, so, its faster.<\/td><td>While loop with incrementing variable uses 10 operations. i+=1 is interpreted, hence, it&#8217;s slower than range()<\/td><\/tr><tr><td>Speed (May Vary on Conditions)<\/td><td>On basis of disassembly, for loop is faster than while loop.<\/td><td>On basis of disassembly, the while loop is slower than for loop.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-types-of-loops-in-python\"><span class=\"ez-toc-section\" id=\"Types_of_loops_in_Python\"><\/span>Types of loops in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>In python, we have two different loop statements in order to loop over a piece of code in two different ways. They have the same functionality &#8211; i.e., they will execute a certain piece of code only if a condition is met. Yet, they differ in syntax and some other aspects.<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>While loop<\/strong> &#8211; This loop statement checks for a condition at the beginning and till the condition is fulfilled, it will execute the body of the loop.<\/li><li><strong>For loop<\/strong> &#8211; For loops are used to sequentially iterate over a python sequence. When the sequence has been iterated completely, the for loop ends and thus executes the next piece of code.     <\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-while-loop-in-python\"><span class=\"ez-toc-section\" id=\"The_While_Loop_In_Python\"><\/span>The While Loop In Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"789\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-While-Loop-Flowchart-1024x789.png\" alt=\"Python While Loop Flowchart\" class=\"wp-image-14550\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-While-Loop-Flowchart-1024x789.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-While-Loop-Flowchart-300x231.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-While-Loop-Flowchart-768x592.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-While-Loop-Flowchart.png 1331w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The while loop statement is used to repeat a block of code till a condition is fulfilled. When we don&#8217;t know the exact number of times, a loop statement has to be executed. We make use of while loops. This way, till the test expression holds true, the loop body will be executed. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-the-syntax-of-python-s-while-loop-is\"><span class=\"ez-toc-section\" id=\"The_syntax_of_pythons_while_loop_is\"><\/span><strong>The syntax of python&#8217;s while loop is:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">while condition:\n  #The loop body<\/pre>\n\n\n\n<p>The<em> &#8216;condition&#8217;<\/em> will be the criteria based on which the loop body will be executed. Till the condition holds true, the loop body is executed. As soon as it becomes false, python will stop executing the loop body.<\/p>\n\n\n\n<p>Let us understand with the help of an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nn = int(input(&quot;Enter number N :&quot;))\nsum = 0\ntemp = n\nwhile n &gt; 0 :\n  sum = sum + n\n  n = n - 1\nprint(&quot;Sum of&quot;,temp,&quot;numbers is:&quot;,sum)\n<\/pre><\/div>\n\n\n<p>Here, the above code is for printing the sum of n numbers as entered by the user. We don&#8217;t know here what will be the number that the user will enter. So, we shall use a while loop in order to print the sum of <em>&#8216;n&#8217; <\/em>numbers. First, we take <em>&#8216;n&#8217; <\/em>as input from the user. Then we have two variables,<em> &#8216;sum&#8217; , <\/em>which will calculate the total summation and <em>&#8216;temp&#8217; <\/em>for storing the value of<em> &#8216;n&#8217;.<\/em><\/p>\n\n\n\n<p>Inside the while loop, the condition to be fulfilled is that the value of<em> &#8216;n&#8217; <\/em>should always be greater than zero. Inside the loop, we add the value of<em> &#8216;n&#8217; <\/em>to<em> &#8216;sum&#8217; <\/em>and then decrement <em>&#8216;n&#8217;. <\/em>While the value of <em>&#8216;n&#8217;<\/em> will become zero, the while loop will stop executing and then print the<em> &#8216;sum&#8217; <\/em>variable.<\/p>\n\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Enter number N :15\nSum of 15 numbers is: 120<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-for-loop-in-python\"><span class=\"ez-toc-section\" id=\"The_For_Loop_In_Python\"><\/span>The For Loop In Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"880\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-For-Loop-Flowchart-1024x880.png\" alt=\"Python For Loop Flowchart\" class=\"wp-image-14551\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-For-Loop-Flowchart-1024x880.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-For-Loop-Flowchart-300x258.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-For-Loop-Flowchart-768x660.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-For-Loop-Flowchart-1536x1320.png 1536w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Python-For-Loop-Flowchart.png 1737w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The for loop in python is used to iterate over a given sequence. The sequence can be a string, a list, a tuple, <a href=\"http:\/\/www.pythonpool.com\/iterate-through-set-python\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><span style=\"text-decoration: underline;\">a set<\/span><\/strong><\/a>, a dictionary, etc. As long as the length of the sequence is not reached, it will iterate over that sequence. The for loop contains initialization, the test expression, and the increment\/decrement expression in the C language. Whereas in the case of python, we only have to mention the value and the sequence to be iterated. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-the-syntax-of-for-loop-in-python-is\"><span class=\"ez-toc-section\" id=\"The_syntax_of_for_loop_in_python_is\"><\/span>The syntax of for loop in python is:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">for value in sequence:\n  #The loop body<\/pre>\n\n\n\n<p>Here, the value can be either an iterable or a sequence item. If it is an index, we use the <em>range() <\/em>function instead of the sequence. Otherwise, we can mention the sequence.<\/p>\n\n\n\n<p><strong>Example of for loop:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncolor = &#x5B;'black', 'white', 'gray']\nfor item in color:\n  print(item)\n<\/pre><\/div>\n\n\n<p>Here, we have taken a simple example to show the usage of a for loop. We have a list named<em> &#8216;color&#8217;, <\/em>which contains three colors. Then we use a for loop where we <a href=\"http:\/\/www.pythonpool.com\/python-pass\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><span style=\"text-decoration: underline;\">pass<\/span><\/strong><\/a><em> &#8216;item&#8217;, <\/em>which represents each individual item in the sequence, and<em> &#8216;color&#8217; <\/em>as the list sequence, which has to be traversed. Inside the for loop, we will print each <em>&#8216;item&#8217;.<\/em><\/p>\n\n\n\n<p><strong>The output will be each list element printed one at a time.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">black\nwhite\ngray<\/pre>\n\n\n\n<p>If we want to iterate using indexing, we can achieve that using the <em><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#func-range\" target=\"_blank\" rel=\"noreferrer noopener\">range()<\/a> <\/em>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncolor = &#x5B;'black', 'white', 'gray']\nfor index in range(len(color)):\n  print(color&#x5B;index])\n<\/pre><\/div>\n\n\n<p>Here, we pass the sequence<em> &#8216;color&#8217; length <\/em>as an argument to the<em> range() <\/em>function. Using the <em>&#8216;index&#8217;, <\/em>we print each individual sequence element.<\/p>\n\n\n\n<p>The output is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">black\nwhite\ngray<\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:19px;\" href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-for-loop-vs-while-loop-python\"><span class=\"ez-toc-section\" id=\"For_loop_vs_while_loop_python\"><\/span>For loop vs while loop python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The for loop and while loop are two different approaches to executing the loop statements in python. They both serve the same functionality of looping over a python code till a condition is being fulfilled. For loops and while loops differ in their syntax. In while loops, we have to mention only the condition before starting the loop. Whereas in the case of for loops, we have to mention the iterable as well as the sequence over which we iterate. <\/p>\n\n\n\n<p>We use while loops when we don&#8217;t know the number of times we want to loop over a code, but we know the condition which determines the execution of the loop body. Whereas for loops are particularly used to <strong><span style=\"text-decoration: underline;\"><a href=\"http:\/\/www.pythonpool.com\/python-iterate-through-list\/\" target=\"_blank\" rel=\"noreferrer noopener\">iterate over a sequence<\/a><\/span><\/strong>. When you know the number of times the loop has to be executed, then using a range function in for loop, we can achieve that.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:19px;\" href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-for-vs-while-loop-python-speed-comparison\"><span class=\"ez-toc-section\" id=\"for_vs_while_loop_Python_Speed_Comparison\"><\/span>for vs while loop Python [Speed Comparison]<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-byte-code-approach\"><span class=\"ez-toc-section\" id=\"Byte_Code_Approach\"><\/span>Byte Code Approach<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We cannot depend on inner timer modules on how the while loop and for loop performs in python. There are many external factors that come into account.<\/p>\n\n\n\n<p>Disassembly is a detailed breakdown of how each piece of code performs in Python. After disassembling using <a href=\"https:\/\/docs.python.org\/3\/library\/dis.html\">dis module<\/a>, we can get to know that while loop has <strong>10 operations<\/strong> if you are incrementing a variable in it and checking condition. Whereas, for loop has <strong>3 operations<\/strong> for looping through range() function.<\/p>\n\n\n\n<p class=\"has-white-color has-vivid-red-background-color has-text-color has-background\"><strong>As a result, using dissassembly, you can clearly observe that for loop with range() function is clearly faster than the while loop with increment method.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-practical-approach\"><span class=\"ez-toc-section\" id=\"Practical_Approach\"><\/span>Practical Approach<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let us take two examples for iterating over a sequence &#8211; one using for loop and the other using while loop. We shall determine the speed using the <a href=\"http:\/\/www.pythonpool.com\/python-timer\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><span style=\"text-decoration: underline;\">time() method<\/span><\/strong><\/a> present on the time module. <\/p>\n\n\n\n<p><strong>Program for finding time taken by the for loop :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\n\nstart = time.time()\n\ncolor = &#x5B;'black', 'white', 'gray', 'red', 'green', 'blue', 'yellow', 'orange']\nfor index in range(len(color)):\n  print(color&#x5B;index])\n\n\nend = time.time()\nprint(end-start)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">black\nwhite\ngray\nred\ngreen\nblue\nyellow\norange\n0.0034780502319335938<\/pre>\n\n\n\n<p><strong>Program for the time taken by the while loop:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\n\nstart = time.time()\n\ncolor = &#x5B;'black', 'white', 'gray', 'red', 'green', 'blue', 'yellow', 'orange']\ni = 0\nwhile i != len(color):\n  print(color&#x5B;i])\n  i = i + 1\n\n\nend = time.time()\nprint(end-start)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">black\nwhite\ngray\nred\ngreen\nblue\nyellow\norange\n0.007262706756591797<\/pre>\n\n\n\n<p>Here, we can see that the time taken for executing the while loop over the same sequence is more than the time taken for executing the for loop.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p>That wraps up the comparison between for vs while loop in python. If you have any questions in your mind, do let us know in the comments below. <\/p>\n\n\n\n<p><em>Until next time, Keep Learning!<\/em><\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-charlie monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-1\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Right_Now\"><\/span>Trending Right Now<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/print-vs-return-in-python\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:17px;\">Print Vs Return in Python: The Ultimate Showdown<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/python-input-vs-raw_input\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:17px;\">Python input() Vs raw_input(): Which One to Choose?<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/python-threading-vs-multiprocessing\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:17px;\">Python Performance Showdown: Threading vs. Multiprocessing<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:17px;\">Comparing for vs while loop in Python<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Loops are one of the most basic entities inside a given programming language. With loop statements, we can execute a given piece of code till &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Comparing for vs while loop in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#more-14472\" aria-label=\"More on Comparing for vs while loop in Python\">Read more<\/a><\/p>\n","protected":false},"author":20,"featured_media":14548,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4047],"tags":[4338,4339,4340,4337],"class_list":["post-14472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-comparison","tag-for-vs-while-loop-python","tag-python-for-loop-vs-while-loop","tag-while-loop-vs-for-loop-python","tag-while-vs-for-loop-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Comparing for vs while loop in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"For vs while loop in python is a comparison between two loops which execute a given piece of code till a certain condition is fulfilled.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Comparing for vs while loop in Python\" \/>\n<meta property=\"og:description\" content=\"Loops are one of the most basic entities inside a given programming language. With loop statements, we can execute a given piece of code till a certain\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-11T06:06:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-11T06:06:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dhruvi Vikma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"Comparing for vs while loop in Python\",\"datePublished\":\"2021-07-11T06:06:17+00:00\",\"dateModified\":\"2021-07-11T06:06:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\"},\"wordCount\":1271,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg\",\"keywords\":[\"for vs while loop python\",\"python for loop vs while loop\",\"while loop vs for loop python\",\"while vs for loop python\"],\"articleSection\":[\"Comparison\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\",\"url\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\",\"name\":\"Comparing for vs while loop in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg\",\"datePublished\":\"2021-07-11T06:06:17+00:00\",\"dateModified\":\"2021-07-11T06:06:20+00:00\",\"description\":\"For vs while loop in python is a comparison between two loops which execute a given piece of code till a certain condition is fulfilled.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg\",\"width\":1200,\"height\":628,\"caption\":\"for vs while loop python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Comparing for vs while loop in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Comparing for vs while loop in Python - Python Pool","description":"For vs while loop in python is a comparison between two loops which execute a given piece of code till a certain condition is fulfilled.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/","og_locale":"en_US","og_type":"article","og_title":"Comparing for vs while loop in Python","og_description":"Loops are one of the most basic entities inside a given programming language. With loop statements, we can execute a given piece of code till a certain","og_url":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/","og_site_name":"Python Pool","article_published_time":"2021-07-11T06:06:17+00:00","article_modified_time":"2021-07-11T06:06:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg","type":"image\/jpeg"}],"author":"Dhruvi Vikma","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"Comparing for vs while loop in Python","datePublished":"2021-07-11T06:06:17+00:00","dateModified":"2021-07-11T06:06:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/"},"wordCount":1271,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg","keywords":["for vs while loop python","python for loop vs while loop","while loop vs for loop python","while vs for loop python"],"articleSection":["Comparison"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/","url":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/","name":"Comparing for vs while loop in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg","datePublished":"2021-07-11T06:06:17+00:00","dateModified":"2021-07-11T06:06:20+00:00","description":"For vs while loop in python is a comparison between two loops which execute a given piece of code till a certain condition is fulfilled.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/While-Loop-Vs.-For-Loop.jpg","width":1200,"height":628,"caption":"for vs while loop python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/for-vs-while-loop-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Comparing for vs while loop in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=14472"}],"version-history":[{"count":21,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14472\/revisions"}],"predecessor-version":[{"id":14559,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14472\/revisions\/14559"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/14548"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=14472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=14472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=14472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}