{"id":115280,"date":"2020-07-11T06:54:55","date_gmt":"2020-07-11T06:54:55","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=10694"},"modified":"2020-07-11T06:54:55","modified_gmt":"2020-07-11T06:54:55","slug":"python-ternary-tuple-syntax-hack","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/11\/python-ternary-tuple-syntax-hack\/","title":{"rendered":"Python Ternary \u2014 Tuple Syntax Hack"},"content":{"rendered":"<p><em>You may know the ternary operator <code>x if c else y<\/code> in Python which reads as &#8220;return <code>x<\/code> if <code>c<\/code> else return <code>y<\/code>&#8220;. But you may not know that you can also write the ternary operator in a shorter form as <code>(y, x)[c]<\/code>. Let&#8217;s dive into this concise way of hacking your own ternary operator!<\/em><\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/07\/tuple-1024x576.jpg\" alt=\"Python Ternary Tuple Syntax\" class=\"wp-image-10701\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/07\/tuple-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/07\/tuple-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/07\/tuple-768x432.jpg 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<p>A shorthand form of the <a href=\"https:\/\/blog.finxter.com\/python-one-line-ternary\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python One Line Ternary\">ternary operator<\/a> is the following <strong>tuple syntax<\/strong> hack.<\/p>\n<p><strong>Syntax<\/strong>: You can use the <a href=\"https:\/\/blog.finxter.com\/python-tuple-to-integer\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Tuple to Integer\">tuple <\/a>syntax <code>(x, y)[c]<\/code> consisting of a tuple <code>(x, y)<\/code> and a condition <code>c<\/code> enclosed in a square bracket. Here&#8217;s a more intuitive way to represent this tuple syntax.<\/p>\n<pre class=\"wp-block-preformatted\">(&lt;OnFalse&gt;, &lt;OnTrue&gt;)[&lt;Condition&gt;]<\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Operand<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&lt;OnTrue&gt;<\/td>\n<td>The return expression of the operator in case the condition evaluates to <code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td>&lt;Condition&gt;<\/td>\n<td>The condition that determines whether to return the &lt;On True&gt; or the &lt;On False&gt; branch.<\/td>\n<\/tr>\n<tr>\n<td>&lt;OnFalse&gt;<\/td>\n<td>The return expression of the operator in case the condition evaluates to <code>False<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table><figcaption><em>Tuple Syntax of the Ternary Operator<\/em><\/figcaption><\/figure>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/8b1118a41c\" width=\"100%\" height=\"500\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the code and compare both ternary operators&#8212;the original and the tuple syntax hack. <\/em><\/p>\n<p>In fact, the order of the <code>&lt;OnFalse><\/code> and <code>&lt;OnTrue><\/code> operands is just flipped when compared to the <a href=\"https:\/\/blog.finxter.com\/if-then-else-in-one-line-python\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/blog.finxter.com\/if-then-else-in-one-line-python\/\">basic ternary operator<\/a>. First, you have the branch that&#8217;s returned if the condition does NOT hold. Second, you run the branch that&#8217;s returned if the condition holds.<\/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=\"\">age = 17\nprint(('wtf', 'What?')[age&lt;20]) 'What?'<\/pre>\n<p>The condition <code>age&lt;20<\/code> holds so the return value passed into 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]\">print()<\/a><\/code> function is the <code>&lt;OnTrue><\/code> branch <code>'What?'<\/code>. Don&#8217;t worry if this confuses you&#8212;you&#8217;re not alone. Let&#8217;s clarify why this tuple syntax works the way it does!<\/p>\n<p>First, you create a tuple <code>('wtf', 'What?')<\/code>. To access the first tuple value <code>'wtf'<\/code>, you&#8217;d use the standard <a href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Introduction to Slicing in Python\">indexing syntax<\/a> <code>('wtf', 'What?')<strong>[0]<\/strong><\/code>. To access the second tuple value <code>'What?'<\/code>, you&#8217;d use the standard indexing syntax <code>('wtf', 'What?')<strong>[1]<\/strong><\/code>.<\/p>\n<p>Second, you create a condition <code>age&lt;20<\/code>. You use this condition as the indexing value. You end up with either <code>('wtf', 'What?')<strong>[False]<\/strong><\/code> or <code>('wtf', 'What?')<strong>[True]<\/strong><\/code>. As you may know, the Booleans <code>False<\/code> and <code>True<\/code> are represented through integers <code>0<\/code> and <code>1<\/code> in <a href=\"https:\/\/blog.finxter.com\/python-crash-course\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Programming Tutorial [+Cheat Sheets]\">Python<\/a>. Thus, you get <code>('wtf', 'What?')<strong>[0]<\/strong><\/code> and <code>('wtf', 'What?')<strong>[1]<\/strong><\/code>, respectively.<\/p>\n<p><strong>In other words<\/strong>: if your condition evaluates to <code>False<\/code>, you access the first tuple value. If your condition evaluates to <code>True<\/code>, you access the second tuple value. <\/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>You may know the ternary operator x if c else y in Python which reads as &#8220;return x if c else return y&#8220;. But you may not know that you can also write the ternary operator in a shorter form as (y, x)[c]. Let&#8217;s dive into this concise way of hacking your own ternary operator! [&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-115280","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\/115280","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=115280"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/115280\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=115280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=115280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=115280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}