Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python One Line Return if

#1
Python One Line Return if

<div><p><strong>Problem</strong>: How to return from a Python function or method in single line?</p>
<p><strong>Example</strong>: Consider the following “goal” statement: </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(x): return None if x == 0</pre>
<p>However, this leads to a Syntax error:</p>
<figure class="wp-block-image size-large"><img src="https://blog.finxter.com/wp-content/uploads/2020/07/image-34.png" alt="" class="wp-image-11577" srcset="https://blog.finxter.com/wp-content/uploads/2020/07/image-34.png 724w, https://blog.finxter.com/wp-content/uplo...00x119.png 300w, https://blog.finxter.com/wp-content/uplo...150x59.png 150w" sizes="(max-width: 724px) 100vw, 724px" /></figure>
<p>In this tutorial, you’ll learn how to write the return statement with an if expression in a single line of Python code. You can get an overview of the three methods in the interactive code shell:</p>
<p> <iframe src="https://trinket.io/embed/python/76859a967b" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: The code has no output. Print the results of all three function executions for a given x. Is it always the same?</em></p>
<p>Let’s dive into the three methods. </p>
<h2>Method 1: As a Multi-Liner</h2>
<p>The following method is the standard and most Pythonic way to accomplish this but using multiple lines:</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(x): if x==0: return None</pre>
<p>But how to write this as a one-liner?</p>
<h2>Method 2: Direct One-Liner If</h2>
<p>Nothing simpler than that—just write it into a single line!</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(x): if x==0: return None
</pre>
<p>I should note that <a href="https://www.python.org/dev/peps/pep-0008/" target="_blank" rel="noreferrer noopener" title="https://www.python.org/dev/peps/pep-0008/">PEP 8</a> is actually fine with writing if block statements into a single line. Nevertheless, the default return value of a function is <code>None</code> so the code does really nothing.</p>
<h2>Method 3: Ternary Operator</h2>
<p>If you look for something more Pythonic, you can check out the <a href="https://blog.finxter.com/python-one-line-ternary/" title="Python One Line Ternary" target="_blank" rel="noreferrer noopener">ternary operator</a> (also called <a href="https://docs.python.org/2/reference/expressions.html#conditional-expressions" target="_blank" rel="noreferrer noopener" title="https://docs.python.org/2/reference/expressions.html#conditional-expressions">“conditional expression”</a>):</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(x): return None if x==0 else 42
</pre>
<p>In this case, you also have to define a return value for the value 42. You should read the statement like this:</p>
<pre class="wp-block-preformatted">return (None if x == 0 else 42)</pre>
<p>The statement inside the parentheses returns either <code>None</code> or <code>42</code>—depending on the condition <code>x == 0</code>. If it is <code>True</code>, the value <code>None</code> is returned. If it is <code>False</code>, the value 42 is returned. </p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/07/...return-if/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016