{"id":224,"date":"2020-10-05T00:45:42","date_gmt":"2020-10-05T00:45:42","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=224"},"modified":"2025-03-26T10:20:39","modified_gmt":"2025-03-26T10:20:39","slug":"python-break","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-break\/","title":{"rendered":"Python break"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>break<\/code> statement and how to use it to exit a loop prematurely.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-break-statement'>Introduction to the Python break statement <a href=\"#introduction-to-the-python-break-statement\" class=\"anchor\" id=\"introduction-to-the-python-break-statement\" title=\"Anchor for Introduction to the Python break statement\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to terminate a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-for-range\/\"><code>for<\/code> loop<\/a> or a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-while\/\"><code>while<\/code> loop<\/a> prematurely regardless of the results of the conditional tests. In these cases, you can use the <code>break<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">break<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Typically, you use the <code>break<\/code>  statement with the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-if\/\"><code>if<\/code> statement<\/a> to terminate a loop when a condition is <code>True<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-python-break-with-for-loop'>Using Python break with for loop <a href=\"#using-python-break-with-for-loop\" class=\"anchor\" id=\"using-python-break-with-for-loop\" title=\"Anchor for Using Python break with for loop\">#<\/a><\/h2>\n\n\n\n<p>The following shows how to use the <code>break<\/code> statement inside a <code>for<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> index <span class=\"hljs-keyword\">in<\/span> range(n):\n    <span class=\"hljs-comment\"># more code here <\/span>\n    <span class=\"hljs-keyword\">if<\/span> condition:\n        <span class=\"hljs-keyword\">break<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, if the <code>condition<\/code> evaluates to <code>True<\/code>, the <code>break<\/code> statement terminates the loop immediately. It won&#8217;t execute the remaining iterations.<\/p>\n\n\n\n<p>This example shows how to use the <code>break<\/code> statement inside a <code>for<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> index <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">10<\/span>):\n    print(index)\n    <span class=\"hljs-keyword\">if<\/span> index == <span class=\"hljs-number\">3<\/span>:\n        <span class=\"hljs-keyword\">break<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Zm9yIGluZGV4IGluIHJhbmdlKDAsIDEwKToKICAgIHByaW50KGluZGV4KQogICAgaWYgaW5kZXggPT0gMzoKICAgICAgICBicmVhaw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-number\">3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>for<\/code> loop iterates over 10 numbers from 0 to 9 and displays each of them on the screen.<\/li>\n\n\n\n<li>However, when the loop counter is 3, the <code>break<\/code> statement terminates the loop immediately. Therefore, the program shows only 4 numbers, from 0 to 3 on the screen.<\/li>\n<\/ul>\n\n\n\n<p>When you use the <code>break<\/code> statement in a nested loop, it&#8217;ll terminate the innermost loop. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> x <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">5<\/span>):\n    <span class=\"hljs-keyword\">for<\/span> y <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">5<\/span>):\n        <span class=\"hljs-comment\"># terminate the innermost loop<\/span>\n        <span class=\"hljs-keyword\">if<\/span> y &gt; <span class=\"hljs-number\">1<\/span>:\n            <span class=\"hljs-keyword\">break<\/span>\n        <span class=\"hljs-comment\"># show coordinates on the screen<\/span>\n        print(<span class=\"hljs-string\">f\"(<span class=\"hljs-subst\">{x}<\/span>,<span class=\"hljs-subst\">{y}<\/span>)\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Zm9yIHggaW4gcmFuZ2UoNSk6CiAgICBmb3IgeSBpbiByYW5nZSg1KToKICAgICAgICAjIHRlcm1pbmF0ZSB0aGUgaW5uZXJtb3N0IGxvb3AKICAgICAgICBpZiB5ID4gMToKICAgICAgICAgICAgYnJlYWsKICAgICAgICAjIHNob3cgY29vcmRpbmF0ZXMgb24gdGhlIHNjcmVlbgogICAgICAgIHByaW50KGYiKHt4fSx7eX0pIik%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">0<\/span>)\n(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">1<\/span>)\n(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">0<\/span>)\n(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">1<\/span>)\n(<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">0<\/span>)\n(<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">1<\/span>)\n(<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">0<\/span>)\n(<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">1<\/span>)\n(<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">0<\/span>)\n(<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example uses two <code>for<\/code> loops to show the coordinates from <code>(0,0)<\/code> to <code>(5,5)<\/code> on the screen.<\/p>\n\n\n\n<p>The <code>break<\/code> statement in the nested loop terminates the innermost loop when the <code>y<\/code> is greater than one.<\/p>\n\n\n\n<p>Therefore, you only see the coordinates whose y values are zero and one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-python-break-statement-with-a-while-loop'>Using Python break statement with a while loop <a href=\"#using-python-break-statement-with-a-while-loop\" class=\"anchor\" id=\"using-python-break-statement-with-a-while-loop\" title=\"Anchor for Using Python break statement with a while loop\">#<\/a><\/h2>\n\n\n\n<p>The following shows how to use the <code>break<\/code> statement inside the <code>while<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">while<\/span> condition:\n    <span class=\"hljs-comment\"># more code<\/span>\n    <span class=\"hljs-keyword\">if<\/span> condition:\n        <span class=\"hljs-keyword\">break<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example uses the <code>break<\/code> statement inside a <code>while<\/code> loop. <\/p>\n\n\n\n<p>It&#8217;ll prompt you for entering your favorite color. The program will stop once you enter <code>quit<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(<span class=\"hljs-string\">'-- Help: type quit to exit --'<\/span>)\n<span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n    color = input(<span class=\"hljs-string\">'Enter your favorite color:'<\/span>)\n    <span class=\"hljs-keyword\">if<\/span> color.lower() == <span class=\"hljs-string\">'quit'<\/span>:\n        <span class=\"hljs-keyword\">break<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">-- Help: type quit to exit --\nEnter your favorite color:red\nEnter your favorite color:green\nEnter your favorite color:blue\nEnter your favorite color:quit<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>while True<\/code> creates an indefinite loop.<\/li>\n\n\n\n<li>Once you enter <code>quit<\/code>, the condition <code>color.lower() == 'quit'<\/code> evaluates True that executes the break statement to terminate the loop.<\/li>\n\n\n\n<li>The <code>color.lower()<\/code> returns the <code>color<\/code> in lowercase so that you can enter <code>Quit<\/code>, <code>QUIT<\/code> or <code>quit<\/code> to exit the program.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Python <code>break<\/code> statement to terminate a for loop or a while loop prematurely.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=break\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"224\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-break\/\"\n\t\t\t\tdata-post-title=\"Python break\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"224\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-break\/\"\n\t\t\t\tdata-post-title=\"Python break\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about the Python break statement and how to use it to exit a loop prematurely. Introduction to the Python break statement # Sometimes, you want to terminate a for loop or a while loop prematurely regardless of the results of the conditional tests. In these cases, you can use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-224","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/224\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}