{"id":116131,"date":"2020-07-31T10:54:32","date_gmt":"2020-07-31T10:54:32","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=11576"},"modified":"2020-07-31T10:54:32","modified_gmt":"2020-07-31T10:54:32","slug":"python-one-line-return-if","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/31\/python-one-line-return-if\/","title":{"rendered":"Python One Line Return if"},"content":{"rendered":"<p><strong>Problem<\/strong>: How to return from a Python function or method in single line?<\/p>\n<p><strong>Example<\/strong>: Consider the following &#8220;goal&#8221; statement: <\/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(x): return None if x == 0<\/pre>\n<p>However, this leads to a Syntax error:<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" 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\/uploads\/2020\/07\/image-34-300x119.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/07\/image-34-150x59.png 150w\" sizes=\"(max-width: 724px) 100vw, 724px\" \/><\/figure>\n<p>In this tutorial, you&#8217;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>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/76859a967b\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<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>\n<p>Let&#8217;s dive into the three methods. <\/p>\n<h2>Method 1: As a Multi-Liner<\/h2>\n<p>The following method is the standard and most Pythonic way to accomplish this but using multiple lines:<\/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(x): if x==0: return None<\/pre>\n<p>But how to write this as a one-liner?<\/p>\n<h2>Method 2: Direct One-Liner If<\/h2>\n<p>Nothing simpler than that&#8212;just write it into a single line!<\/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(x): if x==0: return None\n<\/pre>\n<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>\n<h2>Method 3: Ternary Operator<\/h2>\n<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\">&#8220;conditional expression&#8221;<\/a>):<\/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(x): return None if x==0 else 42\n<\/pre>\n<p>In this case, you also have to define a return value for the value 42. You should read the statement like this:<\/p>\n<pre class=\"wp-block-preformatted\">return (None if x == 0 else 42)<\/pre>\n<p>The statement inside the parentheses returns either <code>None<\/code> or <code>42<\/code>&#8212;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>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: How to return from a Python function or method in single line? Example: Consider the following &#8220;goal&#8221; statement: def f(x): return None if x == 0 However, this leads to a Syntax error: In this tutorial, you&#8217;ll learn how to write the return statement with an if expression in a single line of Python [&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-116131","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\/116131","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=116131"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/116131\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=116131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=116131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=116131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}