{"id":122735,"date":"2021-01-02T10:06:31","date_gmt":"2021-01-02T10:06:31","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=19987"},"modified":"2021-01-02T10:06:31","modified_gmt":"2021-01-02T10:06:31","slug":"return-keyword-in-python-a-simple-illustrated-guide","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2021\/01\/02\/return-keyword-in-python-a-simple-illustrated-guide\/","title":{"rendered":"Return Keyword in Python \u2013 A Simple Illustrated Guide"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/01\/return-1-1024x576.jpg\" alt=\"Python return keyword - visual example\" class=\"wp-image-19999\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/01\/return-1-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/01\/return-1-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/01\/return-1-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/01\/return-1-150x84.jpg 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;s <strong><code>return<\/code> keyword<\/strong> commands the execution flow to <strong>exit a function immediately<\/strong> and return a value to the caller of the function. You can specify an <strong><em>optional return value<\/em><\/strong>&#8212;or even a return expression&#8212;<strong><em>after <\/em><\/strong>the <code>return<\/code> keyword. If you don&#8217;t provide a return value, Python will return the default value <code>None<\/code> to the caller.<\/p>\n<h2>Python Return Keyword Video <\/h2>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python return Keyword - A Simple Illustrated Guide\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/9nDzD6splRk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<h2>Return Keyword Followed by Return Value<\/h2>\n<p>Here&#8217;s an example of the return keyword in combination with a return value:<\/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=\"\">def f(): return 4 print(f())\n# OUTPUT: 4<\/pre>\n<p>Within function <code>f()<\/code>, Python returns the result 4 to the caller. The <code><a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" title=\"Python Print Function [And Its SECRET Separator &amp; End Arguments]\" target=\"_blank\" rel=\"noreferrer noopener\">print()<\/a><\/code> function then prints the output to the shell. <\/p>\n<\/p>\n<h2>Return Keyword Followed by Return Expression<\/h2>\n<p>Here&#8217;s an example of the return keyword in combination with a return expression:<\/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=\"\">def f(): return 2+2 print(f())\n# OUTPUT: 4<\/pre>\n<p>Within function <code>f()<\/code>, Python evaluates the expression <code>2+2=4<\/code> and returns the result 4 to the caller. The <code><a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" target=\"_blank\" rel=\"noreferrer noopener\">print()<\/a><\/code> function then prints the output to the shell.<\/p>\n<h2>Return Keyword Followed by No Value<\/h2>\n<p>Here&#8217;s an example of the return keyword without defining a return value:<\/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=\"\">def f(): return print(f())\n# OUTPUT: None<\/pre>\n<p>Within function <code>f()<\/code>, Python returns the default value <code>None<\/code> to the caller. The <code><a href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" target=\"_blank\" rel=\"noreferrer noopener\">print()<\/a><\/code> function then prints the output to the shell.<\/p>\n<h2>Interactive Code Shell<\/h2>\n<p>Run the following code in your browser:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/e0d733e100\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Change the three return values to 42, 42, and &#8216;Alice&#8217; in the interactive code shell!<\/em><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-return\/\">Return Keyword in Python \u2013 A Simple Illustrated Guide<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\">Finxter<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s return keyword commands the execution flow to exit a function immediately and return a value to the caller of the function. You can specify an optional return value&#8212;or even a return expression&#8212;after the return keyword. If you don&#8217;t provide a return value, Python will return the default value None to the caller. Python Return [&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-122735","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\/122735","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=122735"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122735\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=122735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=122735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=122735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}