{"id":7259,"date":"2024-03-19T06:46:57","date_gmt":"2024-03-19T06:46:57","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7259"},"modified":"2024-03-19T06:46:57","modified_gmt":"2024-03-19T06:46:57","slug":"python-nested-loops","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-nested-loops\/","title":{"rendered":"Python Nested 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-python-nested-loops\">What is Python Nested Loops<\/a><\/li><li ><a href=\"#what-are-python-nested-loop-example\">What are Python Nested Loop Example?<\/a><ul><li ><a href=\"#example-1-example-of-python-nested-loop\">Example 1- Example of Python Nested Loop<\/a><\/li><li ><a href=\"#example-2-printing-multiplication-table-using-the-python-nested-loops\">Example 2:  Printing Multiplication Table using the Python Nested  Loops<\/a><\/li><li ><a href=\"#example-3-printing-using-different-inner-and-outer-nested-loops\">Example 3: \u00a0Printing using different inner and outer nested loops<\/a><\/li><\/ul><\/li><li ><a href=\"#break-statement-in-nested-loop\">Break Statement in Nested Loop<\/a><\/li><li ><a href=\"#continue-statement-in-nested-loops\">Continue statement in nested loops<\/a><\/li><li ><a href=\"#single-line-nested-loops-using-list-comprehension\">Single-line Nested loops using list comprehension<\/a><ul><li ><a href=\"#what-is-the-list-comprehension\">What is the  List Comprehension?<\/a><\/li><\/ul><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-nested-loops-fa-qs\">Python Nested Loops- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>A nested loop is present inside the loop. Read this article to learn more about Python Nested Loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-nested-loops\">What is Python Nested Loops<\/h2>\n\n\n\n<p>Nested loops in <a href=\"https:\/\/www.skillvertex.com\/blog\/what-is-the-future-of-python-developer\/\" data-type=\"post\" data-id=\"2090\">Python<\/a> refer to the concept of having one loop inside another. For instance, placing a &#8216;<a href=\"https:\/\/www.skillvertex.com\/blog\/python-while-loop\/\" data-type=\"post\" data-id=\"7236\">while<\/a>&#8216; loop within a<a href=\"https:\/\/www.skillvertex.com\/blog\/c-for-loop\/\" data-type=\"post\" data-id=\"2045\"> &#8216;for&#8217; loop<\/a> or vice versa. This approach enables programmers to perform more intricate and dynamic <a href=\"https:\/\/www.skillvertex.com\/blog\/which-part-interprets-program-instructions-and-initiate-control-operations\/\" data-type=\"post\" data-id=\"1998\">operations<\/a>, enhancing the versatility of their code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-python-nested-loop-example\">What are Python Nested Loop Example?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1-example-of-python-nested-loop\">Example 1- Example of Python Nested Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 6):  # Outer loop for rows\n    for j in range(1, 6):  # Inner loop for columns\n        result = i * j\n        print(f\"{i} * {j} = {result}\", end='\\t')\n    print()  # Move to the next line after each row\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 * 1 = 1\t1 * 2 = 2\t1 * 3 = 3\t1 * 4 = 4\t1 * 5 = 5\t\n2 * 1 = 2\t2 * 2 = 4\t2 * 3 = 6\t2 * 4 = 8\t2 * 5 = 10\t\n3 * 1 = 3\t3 * 2 = 6\t3 * 3 = 9\t3 * 4 = 12\t3 * 5 = 15\t\n4 * 1 = 4\t4 * 2 = 8\t4 * 3 = 12\t4 * 4 = 16\t4 * 5 = 20\t\n5 * 1 = 5\t5 * 2 = 10\t5 * 3 = 15\t5 * 4 = 20\t5 * 5 = 25\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2-printing-multiplication-table-using-the-python-nested-loops\">Example 2:  Printing Multiplication Table using the Python Nested  Loops<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Define the range for the multiplication table\nstart_range = 1\nend_range = 10\n\n# Nested loop to generate the multiplication table\nfor i in range(1, end_range + 1):  # Outer loop for rows\n    for j in range(1, end_range + 1):  # Inner loop for columns\n        result = i * j\n        print(f\"{i} * {j} = {result}\", end='\\t')\n    print()  # Move to the next line after each row\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 * 1 = 1\t1 * 2 = 2\t1 * 3 = 3\t1 * 4 = 4\t1 * 5 = 5\t1 * 6 = 6\t1 * 7 = 7\t1 * 8 = 8\t1 * 9 = 9\t1 * 10 = 10\t\n2 * 1 = 2\t2 * 2 = 4\t2 * 3 = 6\t2 * 4 = 8\t2 * 5 = 10\t2 * 6 = 12\t2 * 7 = 14\t2 * 8 = 16\t2 * 9 = 18\t2 * 10 = 20\t\n3 * 1 = 3\t3 * 2 = 6\t3 * 3 = 9\t3 * 4 = 12\t3 * 5 = 15\t3 * 6 = 18\t3 * 7 = 21\t3 * 8 = 24\t3 * 9 = 27\t3 * 10 = 30\t\n4 * 1 = 4\t4 * 2 = 8\t4 * 3 = 12\t4 * 4 = 16\t4 * 5 = 20\t4 * 6 = 24\t4 * 7 = 28\t4 * 8 = 32\t4 * 9 = 36\t4 * 10 = 40\t\n5 * 1 = 5\t5 * 2 = 10\t5 * 3 = 15\t5 * 4 = 20\t5 * 5 = 25\t5 * 6 = 30\t5 * 7 = 35\t5 * 8 = 40\t5 * 9 = 45\t5 * 10 = 50\t\n6 * 1 = 6\t6 * 2 = 12\t6 * 3 = 18\t6 * 4 = 24\t6 * 5 = 30\t6 * 6 = 36\t6 * 7 = 42\t6 * 8 = 48\t6 * 9 = 54\t6 * 10 = 60\t\n7 * 1 = 7\t7 * 2 = 14\t7 * 3 = 21\t7 * 4 = 28\t7 * 5 = 35\t7 * 6 = 42\t7 * 7 = 49\t7 * 8 = 56\t7 * 9 = 63\t7 * 10 = 70\t\n8 * 1 = 8\t8 * 2 = 16\t8 * 3 = 24\t8 * 4 = 32\t8 * 5 = 40\t8 * 6 = 48\t8 * 7 = 56\t8 * 8 = 64\t8 * 9 = 72\t8 * 10 = 80\t\n9 * 1 = 9\t9 * 2 = 18\t9 * 3 = 27\t9 * 4 = 36\t9 * 5 = 45\t9 * 6 = 54\t9 * 7 = 63\t9 * 8 = 72\t9 * 9 = 81\t9 * 10 = 90\t\n10 * 1 = 10\t10 * 2 = 20\t10 * 3 = 30\t10 * 4 = 40\t10 * 5 = 50\t10 * 6 = 60\t10 * 7 = 70\t10 * 8 = 80\t10 * 9 = 90\t10 * 10 = 100\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3-printing-using-different-inner-and-outer-nested-loops\">Example 3: &nbsp;Printing using different inner and outer nested loops<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Define the range for the nested loops\nouter_range = 3\ninner_range = 5\n\n# Nested loops with different ranges\nfor i in range(1, outer_range + 1):  # Outer loop\n    for j in range(1, inner_range + 1):  # Inner loop\n        result = i + j\n        print(f\"Outer: {i}, Inner: {j}, Result: {result}\")\n\n#output\n\n\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Outer: 1, Inner: 1, Result: 2\nOuter: 1, Inner: 2, Result: 3\nOuter: 1, Inner: 3, Result: 4\nOuter: 1, Inner: 4, Result: 5\nOuter: 1, Inner: 5, Result: 6\nOuter: 2, Inner: 1, Result: 3\nOuter: 2, Inner: 2, Result: 4\nOuter: 2, Inner: 3, Result: 5\nOuter: 2, Inner: 4, Result: 6\nOuter: 2, Inner: 5, Result: 7\nOuter: 3, Inner: 1, Result: 4\nOuter: 3, Inner: 2, Result: 5\nOuter: 3, Inner: 3, Result: 6\nOuter: 3, Inner: 4, Result: 7\nOuter: 3, Inner: 5, Result: 8\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"break-statement-in-nested-loop\">Break Statement in Nested Loop<\/h2>\n\n\n\n<p>In Python, the <a href=\"https:\/\/www.skillvertex.com\/blog\/break-statement-in-c\/\" data-type=\"post\" data-id=\"2014\">&#8216;break&#8217; statement<\/a> acts as a loop control mechanism, offering a means for premature termination. When triggered within a loop, be it a &#8216;for&#8217; or &#8216;while&#8217; loop, the &#8216;<a href=\"https:\/\/www.skillvertex.com\/blog\/python-break-statement\/\" data-type=\"post\" data-id=\"7241\">break&#8217; statement<\/a> immediately halts the iteration and exits the loop, allowing the program to resume execution after the loop construct. This provides a valuable tool for managing flow control and responding to specific conditions during program execution.<\/p>\n\n\n\n<p>Check out the example provide below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using break statement in nested loops\n\nfor i in range(1, 4):  # Outer loop\n    for j in range(1, 4):  # Inner loop\n        print(f\"Outer: {i}, Inner: {j}\")\n        if i == 2 and j == 2:\n            print(\"Break statement triggered!\")\n            break  # Exit the inner loop when condition is met\n\n# Output\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Outer: 1, Inner: 1\nOuter: 1, Inner: 2\nOuter: 2, Inner: 1\nOuter: 2, Inner: 2\nBreak statement triggered!\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"continue-statement-in-nested-loops\">Continue statement in nested loops<\/h2>\n\n\n\n<p>&#8220;In Python, there&#8217;s something called a <a href=\"https:\/\/www.skillvertex.com\/blog\/python-continue-statement\/\" data-type=\"post\" data-id=\"7246\">&#8216;continue&#8217; statemen<\/a>t. It works in loops and does the opposite of &#8216;break&#8217;. When &#8216;continue&#8217; appears, it doesn&#8217;t stop the loop; instead, it skips the rest of the code for that round and jumps to the next go-around. This is different from &#8216;break&#8217;, which ends the whole loop altogether.&#8221;<\/p>\n\n\n\n<p>Let&#8217;s look at an example to better understand this concept<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n# Using continue statement in nested loops\n\nfor i in range(1, 4):  # Outer loop\n    for j in range(1, 4):  # Inner loop\n        if i == 2 and j == 2:\n            print(\"Continue statement triggered! Skipping this iteration.\")\n            continue  # Skip the rest of the inner loop and move to the next iteration\n        print(f\"Outer: {i}, Inner: {j}\")\n\n# Output\n\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nOuter: 1, Inner: 1\nOuter: 1, Inner: 2\nContinue statement triggered! Skipping this iteration.\nOuter: 1, Inner: 3\nOuter: 2, Inner: 1\nOuter: 2, Inner: 3\nOuter: 3, Inner: 1\nOuter: 3, Inner: 2\nOuter: 3, Inner: 3\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"single-line-nested-loops-using-list-comprehension\">Single-line Nested loops using list comprehension<\/h2>\n\n\n\n<p>In Python, you can make your code shorter by using something called &#8216;list comprehension.&#8217; It&#8217;s like a shortcut that combines nested loops into a single line. You put an  <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-a-legal-expression-in-sql\/\" data-type=\"post\" data-id=\"1856\">expression<\/a> in brackets and use a &#8216;for&#8217; loop to do the work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-the-list-comprehension\">What is the  List Comprehension?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>newList = &#91; expression(element) for element in oldList if condition ] <\/code><\/pre>\n\n\n\n<p>Look at the example given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using list comprehension to create a list of squares\noriginal_list = &#91;1, 2, 3, 4, 5]\nsquares = &#91;x**2 for x in original_list]\n\nprint(squares)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9, 16, 25]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, &#8220;In conclusion, Python nested loops are a powerful way to perform complex iterations by having one loop inside another. Whether using &#8216;for&#8217; or &#8216;while&#8217; loops, this technique allows for versatile code structures.<\/p>\n\n\n\n<p> Additionally, techniques like &#8216;break&#8217; and &#8216;continue&#8217; statements can enhance control within nested loops. List comprehension further streamlines nested loops, providing a concise and readable solution for handling multiple iterations in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-nested-loops-fa-qs\">Python Nested 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-1708422581280\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How many nested loops does Python allow?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It is possible to add as many loops.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708422590256\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is loop syntax in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It is referred to as a Control flow statement that will run the group of statements until the condition is satisfied.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708422603789\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is a delay loop?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The delay loop functions to delay the running of the program.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>A nested loop is present inside the loop. Read this article to learn more about Python Nested Loop. What is Python Nested Loops Nested loops in Python refer to the concept of having one loop inside another. For instance, placing a &#8216;while&#8216; loop within a &#8216;for&#8217; loop or vice versa. This approach enables programmers to &#8230; <a title=\"Python Nested Loops\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-nested-loops\/\" aria-label=\"More on Python Nested Loops\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[72,57,926,866],"class_list":["post-7259","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-programming","tag-python","tag-python-nested-loops","tag-python-tutorial","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\/7259","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=7259"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7259\/revisions"}],"predecessor-version":[{"id":8272,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7259\/revisions\/8272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7263"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}