[Tut] Python Ternary — Tuple Syntax Hack - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Ternary — Tuple Syntax Hack (/thread-96143.html) |
[Tut] Python Ternary — Tuple Syntax Hack - xSicKxBot - 07-12-2020 Python Ternary — Tuple Syntax Hack <div><p><em>You may know the ternary operator <code>x if c else y</code> in Python which reads as “return <code>x</code> if <code>c</code> else return <code>y</code>“. But you may not know that you can also write the ternary operator in a shorter form as <code>(y, x)[c]</code>. Let’s dive into this concise way of hacking your own ternary operator!</em></p> <figure class="wp-block-image size-large is-resized"><img 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="(max-width: 768px) 100vw, 768px" /></figure> <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> <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’s a more intuitive way to represent this tuple syntax.</p> <pre class="wp-block-preformatted">(<OnFalse>, <OnTrue>)[<Condition>]</pre> <figure class="wp-block-table is-style-stripes"> <table> <thead> <tr> <th>Operand</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><OnTrue></td> <td>The return expression of the operator in case the condition evaluates to <code>True</code></td> </tr> <tr> <td><Condition></td> <td>The condition that determines whether to return the <On True> or the <On False> branch.</td> </tr> <tr> <td><OnFalse></td> <td>The return expression of the operator in case the condition evaluates to <code>False</code></td> </tr> </tbody> </table><figcaption><em>Tuple Syntax of the Ternary Operator</em></figcaption></figure> <p> <iframe src="https://trinket.io/embed/python/8b1118a41c" width="100%" height="500" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p> <p><em><strong>Exercise</strong>: Run the code and compare both ternary operators—the original and the tuple syntax hack. </em></p> <p>In fact, the order of the <code><OnFalse></code> and <code><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’s returned if the condition does NOT hold. Second, you run the branch that’s returned if the condition holds.</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="">age = 17 print(('wtf', 'What?')[age<20]) 'What?'</pre> <p>The condition <code>age<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 & End Arguments]">print()</a></code> function is the <code><OnTrue></code> branch <code>'What?'</code>. Don’t worry if this confuses you—you’re not alone. Let’s clarify why this tuple syntax works the way it does!</p> <p>First, you create a tuple <code>('wtf', 'What?')</code>. To access the first tuple value <code>'wtf'</code>, you’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’d use the standard indexing syntax <code>('wtf', 'What?')<strong>[1]</strong></code>.</p> <p>Second, you create a condition <code>age<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> <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> <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/11/python-ternary-tuple-syntax-hack/ |