{"id":129131,"date":"2022-10-22T17:52:36","date_gmt":"2022-10-22T17:52:36","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=818828"},"modified":"2022-10-22T17:52:36","modified_gmt":"2022-10-22T17:52:36","slug":"whats-the-difference-between-return-and-break-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/22\/whats-the-difference-between-return-and-break-in-python\/","title":{"rendered":"What\u2019s the Difference Between return and break in Python?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;818828&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: What is the difference between <code>return<\/code> and <code>break<\/code>? When to use which?<\/p>\n<p>Let&#8217;s first look at a short answer before we dive into a simple example to understand the differences and similarities between <code>return<\/code> and <code>break<\/code>.<\/p>\n<h2>Comparison<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"682\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-190-1024x682.png\" alt=\"\" class=\"wp-image-818971\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-190-1024x682.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-190-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-190-768x512.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-190.png 1327w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>Both <code><a href=\"https:\/\/blog.finxter.com\/python-return\/\" data-type=\"post\" data-id=\"19987\" target=\"_blank\" rel=\"noreferrer noopener\">return<\/a><\/code> and <code><a href=\"https:\/\/blog.finxter.com\/python-cheat-sheet\/\" data-type=\"post\" data-id=\"305\" target=\"_blank\" rel=\"noreferrer noopener\">break<\/a><\/code> are keywords in Python.<\/p>\n<ul>\n<li>The keyword <code>return<\/code> ends a function and passes a value to the caller.<\/li>\n<li>The keyword <code>break<\/code> ends a loop immediately without doing anything else. It can be used within or outside a function.<\/li>\n<\/ul>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th><code>return<\/code><\/th>\n<th><code>break<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Used to end a function<\/td>\n<td>Used to end a <code>for<\/code> or <code>while<\/code> loop<\/td>\n<\/tr>\n<tr>\n<td>Passes an optional value to the caller of the function (e.g., <code>return 'hello'<\/code>)<\/td>\n<td>Doesn&#8217;t pass anything to the &#8220;outside&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>While they serve a different purpose, i.e., ending a function vs ending a loop, there are some cases where they can be used interchangeably. <\/p>\n<h2>Similar Use Cases<\/h2>\n<p>The following use case shows why you may have confused both keywords <code>return<\/code> and <code>break<\/code>. In both cases, you can use them to end a loop inside a function and return to the outside. <\/p>\n<p>Here&#8217;s the variant using <code>return<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def f(): for i in range(10): print(i) if i>3: return f()\n<\/pre>\n<p>And here&#8217;s the variant using <code>break<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def f(): for i in range(10): print(i) if i>3: break f()\n<\/pre>\n<p>Both code snippets do exactly the same&#8212;printing out the first 5 values 0, 1, 2, 3, and 4.<\/p>\n<p><strong>Output: <\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0\n1\n2\n3\n4<\/pre>\n<p>However, this is where the similarity between those two keywords ends. Let&#8217;s dive into a more common use case where they both perform different tasks in the code.<\/p>\n<h2>Different Use Cases<\/h2>\n<p>The following example uses both keywords <code>break<\/code> and <code>return<\/code>. It uses the keyword <code>break<\/code> to end the loop as soon as the loop variable <code>i<\/code> is <a href=\"https:\/\/blog.finxter.com\/python-greater-than\/\" data-type=\"post\" data-id=\"30762\" target=\"_blank\" rel=\"noreferrer noopener\">greater than<\/a> 3. <\/p>\n<p>So the line <code><a href=\"https:\/\/blog.finxter.com\/python-print\/\" data-type=\"post\" data-id=\"20731\" target=\"_blank\" rel=\"noreferrer noopener\">print(i)<\/a><\/code> is never executed after variable <code>i<\/code> reaches the value 4&#8212;<a href=\"https:\/\/blog.finxter.com\/how-to-stop-a-for-loop-in-python\/\" data-type=\"post\" data-id=\"729488\" target=\"_blank\" rel=\"noreferrer noopener\">the loop ends<\/a>.<\/p>\n<p>But the function doesn&#8217;t end because <code>break<\/code> only ends the loop and not the function. That&#8217;s why the statement <code>print('hi')<\/code> is still executed, and the return value of the function is <code>42<\/code> (which we also print in the final line). <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4,7\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def f(): for i in range(10): if i>3: break print(i) print('hi') return 42 print(f())\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0\n1\n2\n3\nhi\n42<\/pre>\n<h2>Summary<\/h2>\n<p>The keyword <code>return<\/code> is different and more powerful than the keyword <code>break<\/code> because it allows you to specify an optional return value. But it can only be used in a function context and <strong><em>not<\/em><\/strong> outside a function. <\/p>\n<ul>\n<li>You use the keyword <code>return<\/code> to give back a value to the caller of the function or terminate the whole function.<\/li>\n<li>You use the keyword <code>break<\/code> to immediately stop a <code>for<\/code> or <code>while<\/code> <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-loops\/\" data-type=\"post\" data-id=\"4596\" target=\"_blank\">loop<\/a>.<\/li>\n<\/ul>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Rule<\/strong>: Only if you want to exit a loop inside a function and this would also exit the whole function, you can use both keywords. In that case, I&#8217;d recommend using the keyword <code>return<\/code> instead of <code>break<\/code> because it gives you more degrees of freedom, i.e., specifying the return value. Plus, it is more explicit which improves the readability of the code.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<p>Thanks for reading over the whole tutorial&#8212;if you want to keep learning, feel free to join my <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" data-type=\"page\" data-id=\"12278\" target=\"_blank\" rel=\"noreferrer noopener\">email academy<\/a>. It&#8217;s fun! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Recommended Video<\/h2>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/whats-the-difference-between-return-and-break-in-python\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F9nDzD6splRk%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Question: What is the difference between return and break? When to use which? Let&#8217;s first look at a short answer before we dive into a simple example to understand the differences and similarities between return and break. Comparison Both return and break are keywords in Python. The keyword return ends a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-129131","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129131"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129131\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}