{"id":7180,"date":"2024-03-19T06:44:18","date_gmt":"2024-03-19T06:44:18","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7180"},"modified":"2024-03-19T06:44:18","modified_gmt":"2024-03-19T06:44:18","slug":"python-for-loops","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-for-loops\/","title":{"rendered":"Python For Loops"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-while-loop-in-python\">What is While Loop in Python?<\/a><\/li><li ><a href=\"#while-loop-syntax-in-python\">While Loop Syntax in Python<\/a><\/li><li ><a href=\"#how-to-use-else-statement-with-while-loop-in-python\">How to use  Else Statement with While Loop in Python?<\/a><\/li><li ><a href=\"#syntax-of-while-loop-with-an-else-statement\">Syntax of  &#8216;while Loop&#8217; with an else statement<\/a><\/li><li ><a href=\"#what-is-infinite-while-loop-in-python\">What is Infinite While Loop in Python?<\/a><\/li><li ><a href=\"#what-is-for-loop-in-python\">What is For Loop in Python?<\/a><\/li><li ><a href=\"#syntax-of-for-loop\">Syntax  of &#8216;For Loop&#8217;<\/a><\/li><li ><a href=\"#output\">Output<\/a><\/li><li ><a href=\"#what-is-nested-loops-in-python\">What is Nested Loops in Python?<\/a><\/li><li ><a href=\"#syntax-of-nested-loops\">Syntax of Nested Loops<\/a><\/li><li ><a href=\"#what-is-continue-statement-in-python\">What is Continue Statement in Python?<\/a><\/li><li ><a href=\"#what-is-a-break-statement-in-python\">What is a Break Statement in Python?<\/a><\/li><li ><a href=\"#what-is-a-pass-statement-in-python\">What is a Pass Statement in  Python?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-for-loops-fa-qs\">Python For Loops- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">A Python loop will repeat over the sequence. The sequence can either be a list, tuple, dictionary, set, or a string.  In the Python Programming language, it has two types of loops. Such as For Loop and While Loop.  This article has listed about the Loops In Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Even though <a href=\"https:\/\/www.skillvertex.com\/blog\/c-for-loop\/\" data-type=\"post\" data-id=\"2045\">Loop <\/a>and <a href=\"https:\/\/www.skillvertex.com\/blog\/do-while-loop-in-c\/\" data-type=\"post\" data-id=\"2291\">While Loop<\/a> will differ in basic functionality and their syntax.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-while-loop-in-python\">What is While Loop in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>&#8216;while loop<\/strong>&#8216; mainly functions to run the block of statements iterate till the condition is completed.  However, when the condition becomes false, the line after the<a href=\"https:\/\/www.skillvertex.com\/blog\/do-while-loop-in-c\/\" data-type=\"post\" data-id=\"2291\"> loop i<\/a>n the program will begin to run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"while-loop-syntax-in-python\">While Loop Syntax in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The Loop Syntax is provided below<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while expression:\n    statement(s)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax indicates that the statements will be indented by the same number of <a href=\"https:\/\/www.skillvertex.com\/blog\/character-arithmetic-in-c-and-c\/\" data-type=\"post\" data-id=\"1876\">character <\/a>spaces even after the programming construct and this will be included in the single block of code.  So, Python will use indentation and will be considered as a method of grouping <a href=\"https:\/\/www.skillvertex.com\/blog\/python-nested-if-statements\/\" data-type=\"post\" data-id=\"7159\">statements<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code given below will use the &#8216;<strong>while loop&#8217; <\/strong>to print the Skillvertex.  This <a href=\"https:\/\/www.skillvertex.com\/blog\/python-comments\/\" data-type=\"post\" data-id=\"7009\">Python code<\/a> will print the output ie, Skill vertex three times by adding a variable known as &#8216;count&#8217; from 1 to 3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 0\nwhile (count &lt; 3):\n    count = count + 1\n    print(\"Skillvertex\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill vertex\nSkill vertex\nSkill vertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-else-statement-with-while-loop-in-python\">How to use  Else Statement with While Loop in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-if-else-statement\/\" data-type=\"post\" data-id=\"7153\">else <\/a>clause will run only when the while condition comes false. But, if the loop has a breakout, then the program won&#8217;t be executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-while-loop-with-an-else-statement\">Syntax of  &#8216;while Loop&#8217; with an else statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax of &#8216;<strong>while Loop<\/strong>&#8216; with the else statement is given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while condition:\n     # execute these statements\nelse:\n     # execute these statements<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example provided below, the Python code will print &#8216;Hello Skillvertex three times with the &#8216;<strong>while&#8217; l<\/strong>oop. Hence, after the loop is executed, then it will print  &#8221;<strong>In Else Block<\/strong>&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it will print the &#8221;<strong>In Else Block<\/strong>&#8221;as there happens to be an &#8221;<strong> else <\/strong>&#8221; block associated with the &#8216;<b>while<\/b>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 0\nwhile (count &lt; 3):\n    count = count + 1\n    print(\"Skillvertex\")\nelse:\n    print(\"In Else Block\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Skillvertex\nHello Skillvertex\nHello Skillvertex\nIn Else Block<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-infinite-while-loop-in-python\">What is Infinite While Loop in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The while loop in Python functions to run the block of code an infinite number of times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the Python code will use the &#8216;<strong>while<\/strong>&#8216; loop along with the condition  (count==0). The Loop will then execute according to the count that will be equal to 0. The count will be given as 0, Thus the loop will run the program since the <a href=\"https:\/\/www.skillvertex.com\/blog\/conditional-or-ternary-operator-in-c\/\" data-type=\"post\" data-id=\"2262\">condition<\/a> will be true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 0\nwhile (count == 0):\n    print(\"Hello Geek\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note: It is recommended not to use this loop type as it denotes a never-ending loop.  Due to this, the condition will be true and then you need to stop the terminator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-for-loop-in-python\">What is For Loop in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, for Loop functions for the sequential traversal. Such as the traversing of a list, string, or array. The &#8221;<strong>for in<\/strong>&#8221; will work similarly to the for each loop in the other programming languages. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us look at the example of how to use &#8216;<strong>For loop<\/strong>&#8216; the sequential traversal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-for-loop\">Syntax  of &#8216;For Loop&#8217;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>for iterator_var in sequence:\n    statements(s)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Python code provided below will print the <strong>&#8216;i&#8217;<\/strong> in every iteration of the loop. It will also use the &#8216;<strong>for loop<\/strong>&#8216; as it will repeat the values from 0 to 3 and denoted by the <strong>range(0,n)<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 4\nfor i in range(0, n):\n    print(i)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"output\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-nested-loops-in-python\">What is Nested Loops in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"> The <a href=\"https:\/\/www.skillvertex.com\/blog\/nested-functions-in-c\/\" data-type=\"post\" data-id=\"2555\">nested Loops <\/a>indicate the existence of one loop inside another loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-nested-loops\">Syntax of Nested Loops<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>for iterator_var in sequence:\n   for iterator_var in sequence:\n       statements(s)\n   statements(s)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax of the nested while loop statement in the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-tutorial\/\" data-type=\"post\" data-id=\"6852\">Python Programming language<\/a> is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while expression:\n   while expression: \n       statement(s)\n   statement(s)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here,  it is possible to create <a href=\"https:\/\/www.skillvertex.com\/blog\/python-nested-if-statements\/\" data-type=\"post\" data-id=\"7159\">nested loops<\/a> with the triangular pattern of numbers. It will repeat from 1 to 4 according to the iteration number. The final output will look like a Pyramid-like pattern of numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example 1: Nested for loops\nfor i in range(3):\n    for j in range(4):\n        print(f\"({i}, {j})\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(0, 0)\n(0, 1)\n(0, 2)\n(0, 3)\n(1, 0)\n(1, 1)\n(1, 2)\n(1, 3)\n(2, 0)\n(2, 1)\n(2, 2)\n(2, 3)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-continue-statement-in-python\">What is Continue Statement in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/www.skillvertex.com\/blog\/continue-statement-in-c\/\" data-type=\"post\" data-id=\"2295\">continue statement <\/a>will return with the control to the beginning of the loop. It will neglect the rest of the code inside the loop for the current iteration and then go to the next iteration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\nif i == 2:\nprint(\"Skipping iteration for i =\", i)\ncontinue\nprint(\"Processing iteration for i =\", i)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Processing iteration for i = 0\nProcessing iteration for i = 1\nSkipping iteration for i = 2\nProcessing iteration for i = 3\nProcessing iteration for i = 4\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-break-statement-in-python\">What is a Break Statement in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/www.skillvertex.com\/blog\/break-statement-in-c\/\" data-type=\"post\" data-id=\"2014\">break statement <\/a>in Python will bring the control out of the Loop. It functions to exit the loop prematurely.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example using break statement\nfor i in range(5):\n    if i == 3:\n        print(\"Breaking loop for i =\", i)\n        break\n    print(\"Processing iteration for i =\", i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Processing iteration for i = 0\nProcessing iteration for i = 1\nProcessing iteration for i = 2\nBreaking loop for i = 3\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-pass-statement-in-python\">What is a Pass Statement in  Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"> The <a href=\"https:\/\/www.skillvertex.com\/blog\/how-arrays-are-passed-to-functions-in-c-c\/\" data-type=\"post\" data-id=\"2612\">Pass Statement<\/a> in Python will write the empty loops. It functions for empty control statements, functions, and classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example using pass statement\nfor i in range(5):\n    if i == 2:\n        print(\"Doing nothing for i =\", i)\n        pass\n    else:\n        print(\"Processing iteration for i =\", i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Processing iteration for i = 0\nProcessing iteration for i = 1\nDoing nothing for i = 2\nProcessing iteration for i = 3\nProcessing iteration for i = 4\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To conclude, this article summarizes the use of  Loops in Python Programming Language. It has also included &#8216;<strong>For loop<\/strong>&#8216;, &#8216;<strong>while loop<\/strong>&#8216;, break statement, and Nested Loops along with the example for your reference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-for-loops-fa-qs\">Python For Loops- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1708067220181\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.  What are I and J in Python for loop?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans They are referred to as the variable name in Python.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708067228666\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.  What is the I in a loop called?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  I is referred to the variable name that has a current array index in each of the loop iterations.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708067240813\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is meant by list in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The list is considered as the data structure in Python that is mutable, changeable, and ordered sequence of elements.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>A Python loop will repeat over the sequence. The sequence can either be a list, tuple, dictionary, set, or a string. In the Python Programming language, it has two types of loops. Such as For Loop and While Loop. This article has listed about the Loops In Python. Even though Loop and While Loop will &#8230; <a title=\"Python For Loops\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-for-loops\/\" aria-label=\"More on Python For Loops\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[918,915,917,57,916],"class_list":["post-7180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-continue-statement","tag-loops","tag-nested-loop","tag-python","tag-python-programming","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7180"}],"version-history":[{"count":5,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7180\/revisions"}],"predecessor-version":[{"id":8266,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7180\/revisions\/8266"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7181"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}