{"id":43,"date":"2024-01-24T14:05:15","date_gmt":"2024-01-24T14:05:15","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=43"},"modified":"2024-01-24T14:05:18","modified_gmt":"2024-01-24T14:05:18","slug":"python-break","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-break\/","title":{"rendered":"Python break"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python&nbsp;<code>break<\/code>&nbsp;statement and how to use it to exit a loop prematurely.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python break statement<\/h2>\n\n\n\n<p>Sometimes, you want to terminate a\u00a0<code>for<\/code>\u00a0loop\u00a0or a\u00a0<code>while<\/code>\u00a0loop\u00a0prematurely regardless of the results of the conditional tests. In these cases, you can use the\u00a0<code>break<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>break<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Typically, you use the\u00a0<code>break<\/code>\u00a0statement with the\u00a0<code>if<\/code>\u00a0statement\u00a0to terminate a loop when a condition is\u00a0<code>True<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Python break with for loop<\/h2>\n\n\n\n<p>The following shows how to use the\u00a0<code>break<\/code>\u00a0statement inside a\u00a0<code>for<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(n): <em># more code here<\/em> if condition: break<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax, if the&nbsp;<code>condition<\/code>&nbsp;evaluates to&nbsp;<code>True<\/code>, the&nbsp;<code>break<\/code>&nbsp;statement terminates the loop immediately. It won\u2019t execute the remaining iterations.<\/p>\n\n\n\n<p>This example shows how to use the\u00a0<code>break<\/code>\u00a0statement inside a\u00a0<code>for<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for index in range(0, 10): print(index) if index == 3: break<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0 1 2 3<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>for<\/code>&nbsp;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&nbsp;<code>break<\/code>&nbsp;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\u00a0<code>break<\/code>\u00a0statement in a nested loop, it\u2019ll terminate the innermost loop. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for x in range(5): for y in range(5): <em># terminate the innermost loop<\/em> if y > 1: break <em># show coordinates on the screen<\/em> print(f\"({x},{y})\")<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>(0,0) (0,1) (1,0) (1,1) (2,0) (2,1) (3,0) (3,1) (4,0) (4,1)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This example uses two&nbsp;<code>for<\/code>&nbsp;loops to show the coordinates from&nbsp;<code>(0,0)<\/code>&nbsp;to&nbsp;<code>(5,5)<\/code>&nbsp;on the screen.<\/p>\n\n\n\n<p>The&nbsp;<code>break<\/code>&nbsp;statement in the nested loop terminates the innermost loop when the&nbsp;<code>y<\/code>&nbsp;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\">Using Python break statement with a while loop<\/h2>\n\n\n\n<p>The following shows how to use the\u00a0<code>break<\/code>\u00a0statement inside the\u00a0<code>while<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>while condition: <em># more code<\/em> if condition: break<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the&nbsp;<code>break<\/code>&nbsp;statement inside a&nbsp;<code>while<\/code>&nbsp;loop.<\/p>\n\n\n\n<p>It\u2019ll prompt you for entering your favorite color. The program will stop once you enter\u00a0<code>quit<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print('-- Help: type quit to exit --') while True: color = input('Enter your favorite color:') if color.lower() == 'quit': break<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Help: type quit to exit -- Enter your favorite color:red Enter your favorite color:green Enter your favorite color:blue Enter your favorite color:quit<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<code>while True<\/code>&nbsp;creates an indefinite loop.<\/li>\n\n\n\n<li>Once you enter&nbsp;<code>quit<\/code>, the condition&nbsp;<code>color.lower() == 'quit'<\/code>&nbsp;evaluates True that executes the break statement to terminate the loop.<\/li>\n\n\n\n<li>The&nbsp;<code>color.lower()<\/code>&nbsp;returns the&nbsp;<code>color<\/code>&nbsp;in lowercase so that you can enter&nbsp;<code>Quit<\/code>,&nbsp;<code>QUIT<\/code>&nbsp;or&nbsp;<code>quit<\/code>&nbsp;to exit the program.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python&nbsp;break&nbsp;statement and how to use it to exit a loop prematurely. Introduction to the Python break statement Sometimes, you want to terminate a\u00a0for\u00a0loop\u00a0or a\u00a0while\u00a0loop\u00a0prematurely regardless of the results of the conditional tests. In these cases, you can use the\u00a0break\u00a0statement: Typically, you use the\u00a0break\u00a0statement with the\u00a0if\u00a0statement\u00a0to terminate a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/43\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}