Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Return Keyword in Python – A Simple Illustrated Guide

#1
Return Keyword in Python – A Simple Illustrated Guide

<div><div class="wp-block-image">
<figure class="aligncenter size-large"><img 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/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-pale-cyan-blue-background-color has-background">Python’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>—or even a return expression—<strong><em>after </em></strong>the <code>return</code> keyword. If you don’t provide a return value, Python will return the default value <code>None</code> to the caller.</p>
<h2>Python Return Keyword Video </h2>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<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>
</div>
</figure>
<h2>Return Keyword Followed by Return Value</h2>
<p>Here’s an example of the return keyword in combination with a return value:</p>
<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())
# OUTPUT: 4</pre>
<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>
</p>
<h2>Return Keyword Followed by Return Expression</h2>
<p>Here’s an example of the return keyword in combination with a return expression:</p>
<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())
# OUTPUT: 4</pre>
<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>
<h2>Return Keyword Followed by No Value</h2>
<p>Here’s an example of the return keyword without defining a return value:</p>
<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())
# OUTPUT: None</pre>
<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>
<h2>Interactive Code Shell</h2>
<p>Run the following code in your browser:</p>
<p> <iframe loading="lazy" src="https://trinket.io/embed/python/e0d733e100" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Change the three return values to 42, 42, and ‘Alice’ in the interactive code shell!</em></p>
<p>The post <a href="https://blog.finxter.com/python-return/">Return Keyword in Python – A Simple Illustrated Guide</a> first appeared on <a href="https://blog.finxter.com">Finxter</a>. </p>
</div>


https://www.sickgaming.net/blog/2021/01/...ted-guide/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016