

{"id":5390,"date":"2017-12-22T06:56:46","date_gmt":"2017-12-22T01:26:46","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5390"},"modified":"2026-04-28T14:38:20","modified_gmt":"2026-04-28T09:08:20","slug":"python-loop","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-loop\/","title":{"rendered":"Python Loop &#8211; Python For Loop, Nested For Loop"},"content":{"rendered":"<p>In this Python Loop Tutorial, we will learn about different types of Python Loop.<\/p>\n<p>Here, we will study Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python with their subtypes, syntax, and examples.<\/p>\n<p>So, let&#8217;s start Python Loop Tutorial.<\/p>\n<h3>What is a Python Loop?<\/h3>\n<p>When you want some statements to execute a hundred times, you don\u2019t repeat them 100 times.<\/p>\n<p>Think of when you want to print numbers 1 to 99. Or that you want to say Hello to 99 friends.<\/p>\n<p>In such a case, you can use loops in Python.<\/p>\n<p><strong>Benefits of using loops in Python:<\/strong><\/p>\n<ul>\n<li><strong>Prevents mistakes:<\/strong> While writing code, it prevents errors made during typing, copying and pasting.<\/li>\n<li><strong>Handles big data:<\/strong> In a huge data set, it can go through multiple items in a loop.<\/li>\n<\/ul>\n<p>Here, we will discuss 4 types of Loops in Python:<\/p>\n<ul>\n<li>Python For Loop<\/li>\n<li>Python While Loop<\/li>\n<li>Python Loop Control Statements<\/li>\n<li>Nested For Loop in Python<\/li>\n<\/ul>\n<h3>Python While Loop<\/h3>\n<p>A while loop in Python iterates till its condition becomes False. In other words, it executes the statements under itself while the condition it takes is True.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" class=\"wp-image-5396 size-medium aligncenter\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python-300x300.jpg\" alt=\"&quot;Python\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python-300x300.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python-150x150.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python-768x768.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python-100x100.jpg 100w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/while-loops-in-Python.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>When the program control reaches the while loop, the condition is checked. If the condition is true, the block of code under it is executed.<\/p>\n<p>Remember to indent all statements under the loop equally. After that, the condition is checked again.<\/p>\n<p>This continues until the condition becomes false.<\/p>\n<p>Then, the first statement, if any, after the loop is executed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=3\r\n&gt;&gt;&gt; while(a&gt;0):\r\n        print(a)\r\n        a-=1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<br \/>\n2<br \/>\n1<\/div>\n<p>This loop prints numbers from 3 to 1. In Python, a\u2014wouldn\u2019t work. We use a-=1 for the same.<a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"628\" class=\"size-full wp-image-35635\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01.jpg\" alt=\"&quot;Python\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/While-loops-01-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h4>1. An Infinite Loop<\/h4>\n<p>Be careful while using a while loop. Because if you forget to increment the counter variable in Python, or write flawed logic, the condition may never become false.<\/p>\n<p>In such a case, the loop will run infinitely, and the conditions after the loop will starve. To stop execution, press Ctrl+C.<\/p>\n<p>However, an infinite loop may actually be useful. This in cases when a semaphore is needed, or for client\/server programming.<\/p>\n<p>A semaphore is a variable used solely for synchronisation in accessing shared resources.<\/p>\n<h4>2. The else statement for the while loop<\/h4>\n<p>A while loop may have an else statement after it. When the condition becomes false, the block under the else statement is executed.<\/p>\n<p>However, it doesn\u2019t execute if you break out of the loop or if an exception is raised.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=3\r\n&gt;&gt;&gt; while(a&gt;0):\r\n        print(a)\r\n        a-=1\r\nelse:\r\n    print(\"Reached 0\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<br \/>\n2<br \/>\n1<br \/>\nReached 0<\/div>\n<p>In the following code, we put a break statement in the body of the while loop for a==1.So, when that happens, the statement in the else block is not executed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=3\r\n&gt;&gt;&gt; while(a&gt;0):\r\n        print(a)\r\n        a-=1\r\n        if a==1: break;\r\nelse:\r\n    print(\"Reached 0\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<br \/>\n2<\/div>\n<h4>3. Single Statement while<\/h4>\n<p>Like an if statement, if we have only one statement in while\u2019s body, we can write it all in one line.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=3\r\n&gt;&gt;&gt; while a&gt;0: print(a); a-=1;<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<br \/>\n2<br \/>\n1<\/div>\n<p>You can see that there were two statements in while\u2019s body, but we used semicolons to separate them.Without the second statement, it would form an infinite loop<\/p>\n<h3>Python For Loop<\/h3>\n<p>A for loop in Python can iterate over a sequence of items. The structure of a for loop in Python is different from that in C++ or Java.<\/p>\n<p>That is, for(int i=0;i&lt;n;i++) won\u2019t work here. In Python, we use the \u2018in\u2019 keyword.<\/p>\n<p>Let&#8217;s see a Python for loop Example<\/p>\n<div id=\"attachment_5394\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5394\" class=\"wp-image-5394 size-medium\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python-300x300.jpg\" alt=\"Python Loop Tutorial - Python for Loop\" width=\"300\" height=\"300\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python-300x300.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python-150x150.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python-768x768.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python-100x100.jpg 100w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-in-Python.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-5394\" class=\"wp-caption-text\">Python Loop Tutorial &#8211; Python for Loop<\/p><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for a in range(3):\r\n        print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n1<br \/>\n2<\/div>\n<p>If we wanted to print 1 to 3, we could write the following code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for a in range(3):\r\n        print(a+1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<\/div>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"628\" class=\"size-full wp-image-35636\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01.jpg\" alt=\"&quot;Python\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/for-loops-01-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h4>1. The range() function in Python<\/h4>\n<p>This function yields a sequence of numbers. When called with one argument, say n, it creates a sequence of numbers from 0 to n-1.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(10))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<\/div>\n<p>We use the list function to convert the range object into a list object.<\/p>\n<p>Calling it with two arguments creates a sequence of numbers from the first to the second.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(2,7))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[2, 3, 4, 5, 6]<\/p>\n<\/div>\n<p>You can also pass three arguments. The third argument is the interval.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(2,12,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[2, 4, 6, 8, 10]<\/p>\n<\/div>\n<p>Remember, the interval can also be negative.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(12,2,-2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[12, 10, 8, 6, 4]<\/p>\n<\/div>\n<p>However, the following codes will return an empty list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(12,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[]<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(2,12,-2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[]<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(12,2,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[]<\/p>\n<\/div>\n<h4>2. Iterating on lists or similar constructs<\/h4>\n<p>You aren\u2019t bound to use the range() function, though. You can use the loop to iterate on a list or a similar construct.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for a in [1,2,3]:\r\n         print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in {2,3,3,4}:\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n3<br \/>\n4<\/div>\n<p>You can also iterate on a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in 'wisdom':\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">w<br \/>\ni<br \/>\ns<br \/>\nd<br \/>\no<br \/>\nm<\/div>\n<h4>3. Iterating on indices of a list or a similar construct<\/h4>\n<p>The len() function returns the length of the list. When you apply the range() function on that, it returns the indices of the list on a range object.<\/p>\n<p>You can iterate on that.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list=['Romanian','Spanish','Gujarati']\r\n&gt;&gt;&gt; for i in range(len(list)):\r\n        print(list[i])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Romanian<br \/>\nSpanish<br \/>\nGujarati<\/div>\n<h4>4. The else statement for the for-loop<\/h4>\n<p>Like a while loop, a for loop may also have an else statement after it.<\/p>\n<p>When the loop is exhausted, the block under the else statement executes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in range(10):\r\n     print(i)\r\nelse:\r\n     print(\"Reached else\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<br \/>\nReached else<\/div>\n<p>Like in the while loop, it doesn\u2019t execute if you break out of the loop or if an exception is raised.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in range(10):\r\n        print(i)\r\n        if(i==7): break\r\nelse: print(\"Reached else\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<\/div>\n<h4>Nested for Loops in Python<\/h4>\n<p>You can also nest a loop inside another. You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while.<\/p>\n<p>Or you can put a loop inside a loop inside a loop. You can go as far as you want.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in range(1,6):\r\n        for j in range(i):\r\n            print(\"*\",end=' ')\r\n        print()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">*<br \/>\n* *<br \/>\n* * *<br \/>\n* * * *<br \/>\n* * * * *<\/div>\n<p>Let\u2019s look at some nested while loops to print the same pattern.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; i=6\r\n&gt;&gt;&gt; while(i&gt;0):\r\n        j=6\r\n        while(j&gt;i):\r\n            print(\"*\",end=' ')\r\n            j-=1\r\n        i-=1\r\n        print()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">*<br \/>\n* *<br \/>\n* * *<br \/>\n* * * *<br \/>\n* * * * *<\/div>\n<h3>Loop Control Statements in Python<\/h3>\n<p>Sometimes, you may want to break out of normal execution in a loop.<\/p>\n<p>For this, we have three keywords in Python: break, continue, and pass.<\/p>\n<div id=\"attachment_35637\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-35637\" class=\"size-full wp-image-35637\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1.jpg\" alt=\"Python Loop Tutorial - Loop Control Statements in Python\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Loop-Control-Statements_Mesa-de-trabajo-1-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-35637\" class=\"wp-caption-text\">Python Loop Tutorial &#8211;<br \/>Loop Control Statements in Python<\/p><\/div>\n<h4>1. break statement in Python<\/h4>\n<p>When you put a break statement in the body of a loop, the loop stops executing, and control shifts to the first statement outside it.<\/p>\n<p>You can put it in a for or while loop.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in 'break':\r\n        print(i)\r\n        if i=='a': break;<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">b<br \/>\nr<br \/>\ne<br \/>\na<\/div>\n<h4>2. continue statement in Python<\/h4>\n<p>When the program control reaches the continue statement, it skips the statements after \u2018continue\u2019.<\/p>\n<p>It then shifts to the next item in the sequence and executes the block of code for it. You can use it with both for and while loops.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; i=0\r\n&gt;&gt;&gt; while(i&lt;8):\r\n        i+=1\r\n        if(i==6): continue\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n7<br \/>\n8<\/div>\n<p>If here, the iteration i+=1 succeeds the if condition, it prints to 5 and gets stuck in an infinite loop.<\/p>\n<p>You can break out of an infinite loop by pressing Ctrl+C.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; i=0\r\n&gt;&gt;&gt; while(i&lt;8):\r\n      if(i==6): continue\r\n      print(i)\r\n      i+=1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>01<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#14&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\nwhile(i&lt;8):<br \/>\nKeyboardInterrupt<\/p>\n<\/div>\n<h4>3. pass statement in Python<\/h4>\n<p>In Python, we use the pass statement to implement stubs.<\/p>\n<p>When we need a particular loop, class, or function in our program, but don\u2019t know what goes in it, we place the pass statement in it.<\/p>\n<p>It is a null statement. The interpreter does not ignore it, but it performs a no-operation (NOP).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in 'selfhelp':\r\n        pass\r\n&gt;&gt;&gt; print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">p<\/div>\n<p>To run this code, save it in a .py file and press F5. It causes a syntax error in the shell.<\/p>\n<h3>Python Interview Questions on Loops<\/h3>\n<p>1. What is a loop in Python? Explain with an example.<\/p>\n<p>2. What is the syntax of a for loop in Python?<\/p>\n<p>3. How do you write a loop in Python?<\/p>\n<p>4. How many types of loops are there in Python?<\/p>\n<p>5. What are loops used for in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>Python offers two core loop forms: for and while. A for loop walks over each item produced by an iterable\u2014be it a list, a file object, or a generator\u2014so you avoid manual index bookkeeping. Underneath, the loop calls iter() once and then next() each turn until StopIteration signals the end. A while loop repeats as long as its condition stays True, ideal for polling or open-ended tasks.<\/p>\n<p>Lastly, we learnt about break, continue, and pass statements to control loops. So, try out your own combinations in the shell, and don\u2019t forget to leave your feedback in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python Loop Tutorial, we will learn about different types of Python Loop. Here, we will study Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":35633,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[4853,8413,9039,10668,16118],"class_list":["post-5390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-for-loop-in-python","tag-loops-in-python","tag-nested-loops-in-python","tag-python-loops","tag-while-loops-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Loop - Python For Loop, Nested For Loop - DataFlair<\/title>\n<meta name=\"description\" content=\"Python Loop Tutorial - Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python With their Subtypes, Syntax, Exmaples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Loop - Python For Loop, Nested For Loop - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python Loop Tutorial - Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python With their Subtypes, Syntax, Exmaples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-loop\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-22T01:26:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:08:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Loops-01.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=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Loop - Python For Loop, Nested For Loop - DataFlair","description":"Python Loop Tutorial - Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python With their Subtypes, Syntax, Exmaples","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:\/\/data-flair.training\/blogs\/python-loop\/","og_locale":"en_US","og_type":"article","og_title":"Python Loop - Python For Loop, Nested For Loop - DataFlair","og_description":"Python Loop Tutorial - Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python With their Subtypes, Syntax, Exmaples","og_url":"https:\/\/data-flair.training\/blogs\/python-loop\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-12-22T01:26:46+00:00","article_modified_time":"2026-04-28T09:08:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Loops-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-loop\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Loop &#8211; Python For Loop, Nested For Loop","datePublished":"2017-12-22T01:26:46+00:00","dateModified":"2026-04-28T09:08:20+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-loop\/"},"wordCount":1324,"commentCount":26,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Loops-01.jpg","keywords":["for loop in python","loops in python","nested loops in python","Python loops","while loops in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-loop\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-loop\/","url":"https:\/\/data-flair.training\/blogs\/python-loop\/","name":"Python Loop - Python For Loop, Nested For Loop - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Loops-01.jpg","datePublished":"2017-12-22T01:26:46+00:00","dateModified":"2026-04-28T09:08:20+00:00","description":"Python Loop Tutorial - Python For Loop, Python While Loop, Python Loop Control Statements, and Nested For Loop in Python With their Subtypes, Syntax, Exmaples","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-loop\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Loops-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Loops-01.jpg","width":1200,"height":628,"caption":"Python Loop Tutorial - Python For Loop, Nested For Loop"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-loop\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Loop &#8211; Python For Loop, Nested For Loop"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5390"}],"version-history":[{"count":26,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5390\/revisions"}],"predecessor-version":[{"id":148006,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5390\/revisions\/148006"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/35633"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}