{"id":2417,"date":"2020-01-11T17:00:49","date_gmt":"2020-01-11T11:30:49","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=2417"},"modified":"2023-12-30T16:01:20","modified_gmt":"2023-12-30T10:31:20","slug":"python-break","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-break\/","title":{"rendered":"Python Break | How To Use Break Statement In Python"},"content":{"rendered":"\n<p>The&nbsp;Python Break statement can be used to terminate the execution of a loop. It can only appear within a&nbsp;<em>for&nbsp;<\/em>or&nbsp;<em>while&nbsp;<\/em>loop. It allows us to break out of the nearest enclosing loop. If the loop has an&nbsp;<em>else<\/em>&nbsp;clause, then&nbsp;the code block associated with it will not be executed if we use the<em>&nbsp;<strong>break<\/strong><\/em>&nbsp;statement.<\/p>\n\n\n\n<p>So Basically The&nbsp;break&nbsp;statement in Python is a handy way for <strong>exiting a loop<\/strong> from anywhere within the loop\u2019s body.&nbsp; Jump Statements in Python<\/p>\n\n\n\n<p> It is sometimes desirable to&nbsp;<strong>skip<\/strong>&nbsp;some statements inside the loop or terminate the loop immediately without checking the test expression. In such cases, we can use&nbsp;<strong>break statements<\/strong>&nbsp;in Python. The break statement allows you to exit a loop from any point within its body, bypassing its normal&nbsp;<strong>termination<\/strong>&nbsp;expression. <\/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\/python-break\/#Introduction_to_Break_Keyword\" >Introduction to Break Keyword<\/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\/python-break\/#Working_of_the_break_statement_in_Python\" >Working of the break statement 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\/python-break\/#Break_in_Python\" >Break in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Why_and_When_to_Use_Break_in_Python\" >Why and When to Use Break 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-5\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Syntax_of_break\" >Syntax of break<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-break\/#What_do_break_keyword_do_in_Python\" >What do break keyword do 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-7\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Flowchart_of_Break_Statement_in_Python\" >Flowchart of Break Statement in Python<\/a><\/li><\/ul><\/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\/python-break\/#Python_Break_for_while_and_for_Loop\" >Python Break for while and for Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Example_of_Python_break_statement_in_while_loop\" >Example of Python break statement in while loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Example_of_Python_break_statement_in_for_loop\" >Example of Python break statement in for loop<\/a><\/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\/python-break\/#Why_doesnt_Python_support_labeled_break_statements\" >Why doesn\u2019t Python support labeled break statements?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Why_there_is_no_colon_after_the_break_statement\" >Why there is no colon: after the break statement?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Important_points_about_the_break_statement\" >Important points about the break statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-break\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-introduction-to-break-keyword\"><span class=\"ez-toc-section\" id=\"Introduction_to_Break_Keyword\"><\/span>Introduction to Break Keyword<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python-like other languages provide a special purpose statement called a <strong>break<\/strong>. This statement terminates the loop immediately and control is returned to the statement right after the body of the loop.  <\/p>\n\n\n\n<p>It terminates the current loop and resumes execution at the next statement, just like the traditional break statement in C.<\/p>\n\n\n\n<p>An infinite loop is a loop that goes on forever with no end.<\/p>\n\n\n\n<p>Normally in programs, infinite loops are not what the programmer desires. The programmer normally wants to create loops that have an end.<\/p>\n\n\n\n<p>In Python, the keyword&nbsp;<code>break<\/code>&nbsp;causes the program to exit a loop early.&nbsp;<code>break<\/code>&nbsp;causes the program to jump out of for loops even if the for loop hasn&#8217;t run the specified number of times.<code>break<\/code>&nbsp;causes the program to jump out of while loops even if the logical condition that defines the loop is still&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-working-of-the-break-statement-in-python\"><span class=\"ez-toc-section\" id=\"Working_of_the_break_statement_in_Python\"><\/span><strong>Working<\/strong> of the break statement in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While entering the loop, a particular condition is being checked. If it satisfies, statements in the loop are executed. If it is not fulfilled, that loop iteration gets broken, and flow is redirected to the next statement outside the loop. Here, a break statement is used to break the flow of the loop in case any trigger occurs other than the stopping condition occurs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-break-in-python\"><span class=\"ez-toc-section\" id=\"Break_in_Python\"><\/span><strong>Break in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python break is generally used to terminate a loop. This means whenever the interpreter encounters the&nbsp;<code>break<\/code>&nbsp;keyword,&nbsp;<strong>it simply exits out of the loop<\/strong>. Once it breaks out of the loop, the control shifts to the immediate next statement.<\/p>\n\n\n\n<p>Also, if the break statement is used inside a nested loop, it terminates the innermost loop, and the control shifts to the next statement in the outer loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-and-when-to-use-break-in-python\"><span class=\"ez-toc-section\" id=\"Why_and_When_to_Use_Break_in_Python\"><\/span>Why and When to Use Break in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The typical use of break is found in a sequential search algorithm. For example, if you need to search for an object in a collection, you will have to execute a comparison expression in a loop. However, if the required object is found, an early exit from the loop is sought, without traversing the remaining collection. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-syntax-of-break\"><span class=\"ez-toc-section\" id=\"Syntax_of_break\"><\/span>Syntax of break<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Python break&nbsp;statement has very simple syntax where we only use&nbsp;<code>break<\/code>&nbsp;keyword. We generally check for a condition with&nbsp;<code>if-else<\/code>&nbsp;blocks and then use&nbsp;<code>break<\/code>&nbsp;. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">break<\/pre>\n\n\n\n<p><strong><em>Syntax of Break in for and while loop.<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor value in sequence:\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # code for for block\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if condition:\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #code for for loop\n#outside of for loop\n\nwhile expression:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #code for while loop\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if if_expression:\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #code for while loop\n#outside of while loop\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-do-break-keyword-do-in-python\"><span class=\"ez-toc-section\" id=\"What_do_break_keyword_do_in_Python\"><\/span><strong>What do break keyword do in Python?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>break keyword in Python is often used with loops\u00a0<strong>for<\/strong>\u00a0and\u00a0<strong>while<\/strong>\u00a0to modify the flow of loops.<\/p>\n\n\n\n<p>Loops are used to execute a statement again and again until the expression becomes\u00a0<strong>False\u00a0<\/strong>or the sequence of elements becomes empty. But what if, we want to terminate the loop before the expression becomes False or we reach the end of the sequence, and that&#8217;s the situation when the break comes in to play.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-flowchart-of-break-statement-in-python\"><span class=\"ez-toc-section\" id=\"Flowchart_of_Break_Statement_in_Python\"><\/span><strong>Flowchart of Break Statement in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"500\" height=\"585\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/flowchart-break-statement.jpg\" alt=\"python break flowchart\" class=\"wp-image-2426\" style=\"width:386px;height:auto\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/flowchart-break-statement.jpg 500w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/flowchart-break-statement-256x300.jpg 256w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-break-for-while-and-for-loop\"><span class=\"ez-toc-section\" id=\"Python_Break_for_while_and_for_Loop\"><\/span>Python Break for while and for Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The break statement is used for prematurely exiting a current loop.break can be used for both for and while loops. If the break statement is used inside a nested loop, the innermost loop will be terminated. Then the statements of the outer loop are executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-of-python-break-statement-in-while-loop\"><span class=\"ez-toc-section\" id=\"Example_of_Python_break_statement_in_while_loop\"><\/span>Example of Python break statement in while loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-example-1-python-break-while-loop\">Example 1: Python break while loop <\/h5>\n\n\n\n<p>In the following example,&nbsp;<code>while<\/code>&nbsp;loop is set to print the first 8 items in the tuple. But what actually happens is,&nbsp;when the&nbsp;<code>count<\/code>&nbsp;is equal to 4, it triggers&nbsp;<code>if<\/code>&nbsp;statement and the&nbsp;<code>break<\/code>&nbsp;statement inside it is invoked making the flow of program jump out of the loop.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#declaring a tuple\nnum = (1,2,3,4,5,6,7,8)\ncount = 0\nwhile (count&lt;9):\n  print (num[count])\n  count = count+1\n  if count == 4:\n     break\nprint ('End of program')<\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n2\n3\n4\nEnd of program<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-example-2-python-break-while-loop\">Example 2: Python break while loop  <\/h5>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ni\u00a0=\u00a00;\u00a0\u00a0\n\nwhile\u00a01:\u00a0\n\u00a0\u00a0\u00a0\u00a0print(i,&quot;\u00a0&quot;,end=&quot;&quot;),\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0i=i+1;\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0if\u00a0i\u00a0==\u00a010:\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;\u00a0\u00a0\n\nprint(&quot;came\u00a0out\u00a0of\u00a0while\u00a0loop&quot;);\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0  1  2  3  4  5  6  7  8  9  came out of while loop<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-example-3-python-break-while-loop\">Example 3: Python break while loop   <\/h5>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ni=1\nwhile i &lt; 11:\n    if i==6:\n        break\n    print(i)\n    i=i+1\n    \nprint('Bye')\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\nBye<\/code><\/pre>\n\n\n\n<p class=\"has-very-dark-gray-color has-light-green-cyan-background-color has-text-color has-background has-normal-font-size\"><strong>Also Read:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group has-vivid-cyan-blue-background-color has-background\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group has-very-light-gray-background-color has-background\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<a href=\"http:\/\/www.pythonpool.com\/python-remove-duplicates-from-list\/\">Python Remove Duplicates From List<\/a> <\/br>\n\n<a href=\"http:\/\/www.pythonpool.com\/python-print-without-newline\/\">Python Print Without Newline<\/a> <\/br>\n\n\n<a href=\"http:\/\/www.pythonpool.com\/learn-python-the-hard-way-pdf\/\">Learn Python the Hard Way Review PDF<\/a> <\/br>\n\n<a href=\"http:\/\/www.pythonpool.com\/data-science-internship\/\">How to Get a Data Science Internship With No Experience<\/a> <\/br>\n\n<a href=\"http:\/\/www.pythonpool.com\/python-map-function\/\">Python Map<\/a>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-of-python-break-statement-in-for-loop\"><span class=\"ez-toc-section\" id=\"Example_of_Python_break_statement_in_for_loop\"><\/span><strong>Example of Python break statement in for loop<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-example-1-python-break-for-loop\">Example 1: Python break for loop<\/h5>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist\u00a0=&#x5B;1,2,3,4]\u00a0\u00a0\ncount\u00a0=\u00a01;\u00a0\u00a0\n\nfor\u00a0i\u00a0in\u00a0list:\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0if\u00a0i\u00a0==\u00a04:\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print(&quot;item\u00a0matched&quot;)\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0count\u00a0=\u00a0count\u00a0+\u00a01;\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break\u00a0\u00a0\n\nprint(&quot;found\u00a0at&quot;,count,&quot;location&quot;);\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">item matched\nfound at 2 location<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-example-2-python-break-for-loop\">Example 2: Python break for loop  <\/h5>\n\n\n\n<p>Following example will do the same exact thing as the above program but using a&nbsp;<code>for<\/code>&nbsp;loop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#declaring a tuple\nnum = (1,2,3,4,5,6,7,8)\ncount = 0\nfor item in num:\n  print (item)\n  count = count+1\n  if count == 4:\n     break\nprint ('End of program')\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n2\n3\n4\nEnd of program<\/pre>\n\n\n\n<p>Programming Tips<code>break<\/code>&nbsp;statement is always used with&nbsp;<code>if<\/code>&nbsp;statement inside a loop and loop will be terminated whenever&nbsp;<code>break<\/code>&nbsp;statement is encountered. <\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-example-3-python-break-for-loop\">Example 3: Python break for loop <\/h5>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor i in range(10):\n  print(i)\n  if(i == 7):\n    print('break');\n    break\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0\n1\n2\n3\n4\n5\n6\n7\n<strong>break<\/strong><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-doesn-t-python-support-labeled-break-statements\"><span class=\"ez-toc-section\" id=\"Why_doesnt_Python_support_labeled_break_statements\"><\/span>Why doesn\u2019t Python support labeled break statements?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Many popular programming languages support a labeled break statement. It\u2019s mostly used to break out of the outer loop in case of nested loops. However, Python doesn\u2019t support labeled break statements.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.python.org\/dev\/peps\/pep-3136\/\" target=\"_blank\" rel=\"noreferrer noopener\">PEP 3136<\/a>\u00a0was raised to add label support to the break statement. But, it was rejected because it would add unnecessary complexity to the language. A better alternative is available for this scenario \u2013 move the code to a function and add the\u00a0return statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-why-there-is-no-colon-after-the-break-statement\"><span class=\"ez-toc-section\" id=\"Why_there_is_no_colon_after_the_break_statement\"><\/span>Why there is no colon: after the break statement?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C<em>olon<\/em><strong>:<\/strong>&nbsp; are not required to use next to the&nbsp;<strong>break<\/strong>&nbsp;statement, unlike when declaring a conditional statement like&nbsp;<strong>if, else, elif<\/strong>&nbsp;where Python requires us to use a&nbsp;<em>colon<\/em><strong>:<\/strong>&nbsp;because a colon is only needed before an upcoming&nbsp;<strong>indented<\/strong>&nbsp;<em>block of statements<\/em>, connected to the conditional statements like&nbsp;<strong>if, else, elif<\/strong>&nbsp;or with a looping construct like&nbsp;<strong>while, for<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-important-points-about-the-break-statement\"><span class=\"ez-toc-section\" id=\"Important_points_about_the_break_statement\"><\/span>Important points about the break statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The execution moves to the next line of code outside the loop block after the break statement.<\/li>\n\n\n\n<li>If the break statement is used in an inner loop, its scope will be an inner loop only. That is, the execution will move to the outer loop after exiting the inner loop.<\/li>\n\n\n\n<li>The break statement can be used with for or while loops.  otherwise, a&nbsp;<em>compile error<\/em>&nbsp;is issued by the compiler at the time of compilation of the program.<\/li>\n\n\n\n<li>Within a loop, you can associate the&nbsp;<strong>break<\/strong>&nbsp;statement either&nbsp;<em>with or without<\/em>&nbsp;a conditional statement like&nbsp;<strong>if<\/strong>.<\/li>\n\n\n\n<li>If the&nbsp;<strong>break<\/strong>&nbsp;statement is associated with a conditional&nbsp;<strong>if<\/strong>&nbsp;statement, then it will execute only when the&nbsp;<em>condition<\/em>&nbsp;of the&nbsp;<strong>if<\/strong>&nbsp;the statement is evaluated to&nbsp;<strong>true<\/strong>.<\/li>\n\n\n\n<li>If the&nbsp;<strong>break<\/strong>&nbsp;statement is not associated with any conditional statement within a loop, then it will just exit the loop right when it is encountered for the first time.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Python\u00a0break statement is a loop\u00a0control statement that terminates the normal execution of a sequence of statements in a loop and passes it to the next statement after the current loop exits. a break can be used in many loops \u2013 for, while and all kinds of nested loop. <\/p>\n\n\n\n<p>If you still have doubts about the Python Break statement, comment below. We will try to solve your query ASAP.<\/p>\n\n\n\n<p> <strong>Happy  Coding!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;Python Break statement can be used to terminate the execution of a loop. It can only appear within a&nbsp;for&nbsp;or&nbsp;while&nbsp;loop. It allows us to break out &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Break | How To Use Break Statement In Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-break\/#more-2417\" aria-label=\"More on Python Break | How To Use Break Statement In Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2427,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","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>Python Break | How To Use Break Statement In Python - Python Pool<\/title>\n<meta name=\"description\" content=\"Python Break statement can be used to terminate the execution of a loop. It can only appear within a for or while loop. The break allows terminating Python\" \/>\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\/python-break\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Break | How To Use Break Statement In Python\" \/>\n<meta property=\"og:description\" content=\"The&nbsp;Python Break statement can be used to terminate the execution of a loop. It can only appear within a&nbsp;for&nbsp;or&nbsp;while&nbsp;loop. It\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-break\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-11T11:30:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-30T10:31:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1018\" \/>\n\t<meta property=\"og:image:height\" content=\"614\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Python Pool\" \/>\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=\"Python Pool\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/\"},\"author\":{\"name\":\"Python Pool\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\"},\"headline\":\"Python Break | How To Use Break Statement In Python\",\"datePublished\":\"2020-01-11T11:30:49+00:00\",\"dateModified\":\"2023-12-30T10:31:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/\"},\"wordCount\":1295,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg\",\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-break\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-break\/\",\"name\":\"Python Break | How To Use Break Statement In Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg\",\"datePublished\":\"2020-01-11T11:30:49+00:00\",\"dateModified\":\"2023-12-30T10:31:20+00:00\",\"description\":\"Python Break statement can be used to terminate the execution of a loop. It can only appear within a for or while loop. The break allows terminating Python\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-break\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg\",\"width\":1018,\"height\":614,\"caption\":\"python break\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-break\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Break | How To Use Break Statement 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\/f87448ee54c0ffd2889fbf2408c18998\",\"name\":\"Python Pool\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"caption\":\"Python Pool\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Break | How To Use Break Statement In Python - Python Pool","description":"Python Break statement can be used to terminate the execution of a loop. It can only appear within a for or while loop. The break allows terminating Python","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\/python-break\/","og_locale":"en_US","og_type":"article","og_title":"Python Break | How To Use Break Statement In Python","og_description":"The&nbsp;Python Break statement can be used to terminate the execution of a loop. It can only appear within a&nbsp;for&nbsp;or&nbsp;while&nbsp;loop. It","og_url":"https:\/\/www.pythonpool.com\/python-break\/","og_site_name":"Python Pool","article_published_time":"2020-01-11T11:30:49+00:00","article_modified_time":"2023-12-30T10:31:20+00:00","og_image":[{"width":1018,"height":614,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg","type":"image\/jpeg"}],"author":"Python Pool","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Python Pool","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-break\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-break\/"},"author":{"name":"Python Pool","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998"},"headline":"Python Break | How To Use Break Statement In Python","datePublished":"2020-01-11T11:30:49+00:00","dateModified":"2023-12-30T10:31:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-break\/"},"wordCount":1295,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-break\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-break\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-break\/","url":"https:\/\/www.pythonpool.com\/python-break\/","name":"Python Break | How To Use Break Statement In Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-break\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-break\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg","datePublished":"2020-01-11T11:30:49+00:00","dateModified":"2023-12-30T10:31:20+00:00","description":"Python Break statement can be used to terminate the execution of a loop. It can only appear within a for or while loop. The break allows terminating Python","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-break\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-break\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-break\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-4.jpg","width":1018,"height":614,"caption":"python break"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-break\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python Break | How To Use Break Statement 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\/f87448ee54c0ffd2889fbf2408c18998","name":"Python Pool","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","caption":"Python Pool"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/2417","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=2417"}],"version-history":[{"count":12,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/2417\/revisions"}],"predecessor-version":[{"id":31322,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/2417\/revisions\/31322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/2427"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=2417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=2417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=2417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}