Create an account


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

#1
Python One Line Ternary

<div><p><strong>Ternary</strong> (from Latin <em>ternarius</em>) is an adjective meaning <em>“composed of three items”</em>. (<a href="https://en.wikipedia.org/wiki/Ternary" target="_blank" rel="noreferrer noopener" title="https://en.wikipedia.org/wiki/Ternary">source</a>) So, literally, the ternary operator in Python is composed of three operands.</p>
<p><strong>Syntax</strong>: The three operands are written in an intuitive combination <code>... if ... else ...</code>.</p>
<pre class="wp-block-preformatted">&lt;On True> if &lt;Condition> else &lt;On False></pre>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Operand</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>&lt;On True></td>
<td>The return expression of the operator in case the condition evaluates to <code>True</code></td>
</tr>
<tr>
<td>&lt;Condition></td>
<td>The condition that determines whether to return the &lt;On True> or the &lt;On False> branch.</td>
</tr>
<tr>
<td>&lt;On False></td>
<td>The return expression of the operator in case the condition evaluates to <code>False</code></td>
</tr>
</tbody>
</table><figcaption><em>Operands of the Ternary Operator</em></figcaption></figure>
<p>Let’s have a look at a minimum example in our interactive code shell:</p>
<p> <iframe src="https://trinket.io/embed/python/42f3df6ddd" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Run the code and input your age. What’s the output? Run the code again and try to change the output!</em></p>
<p>Let’s dive into the different variants of the Ternary operator in Python!</p>
<h2>Python Ternary Examples</h2>
<p>Let’s have a quick overview of a few examples on different methods to use the ternary operator:</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 # Method 1: Basic Ternary
print('wtf' if age&lt;20 else 'What?') 'wtf' # Method 2: Ternary Tuple
# (onFalse, onTrue) [condition]
print(('wtf', 'What?') [age&lt;20]) 'What?' # Method 3: Ternary Dictionary
# Use Dictionary True/False values
print({True: 'wtf', False: 'What?'} [age&lt;20]) 'wtf' # Method 4: Ternary Lambda
# Lambda function with 0 arguments
# Execute only one branch expression --> more efficient
print((lambda: 'wtf', lambda:'What?') [age&lt;20]()) 'What?'
</pre>
<p>Some of them are pretty confusing, right? Stay with me for a moment because you’ll learn about each of those next! <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Python Ternary Tuple</h2>
</p>
</p>
<h2>Python Ternary Dictionary</h2>
<h2>Python Ternary Lambda</h2>
<h2>Python Ternary Multiple Lines</h2>
<h2>Python Ternary Elif</h2>
<h2>Python Ternary Nested</h2>
<h2>Python Ternary Plot</h2>
<h2>Python Ternary Diagram</h2>
<h2>Python Ternary Evaluation Order</h2>
<h2>Python Ternary Try Except</h2>
<h2>Python Ternary Tree</h2>
<h2>Python Ternary in List Comprehension</h2>
<h2>Python Ternary Pep8 Pythonic</h2>
<h2>Python Ternary Can’t Assign to Conditional Expression</h2>
<h2>Python Ternary For Loop</h2>
<h2>Python Ternary Break</h2>
<h2>Python Ternary None</h2>
<h2>Python Ternary Multiple Conditions</h2>
<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>
<h2>Python One-Liners Book</h2>
<p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p>
<figure class="wp-block-image size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img src="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg" alt="Python One-Liners" class="wp-image-10007" width="512" height="472" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x277.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x708.jpg 768w" sizes="(max-width: 512px) 100vw, 512px" /></a></figure>
<p><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noreferrer noopener" title="https://www.amazon.com/gp/product/B07ZY7XMX8"><em>Python One-Liners</em> </a>will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.</p>
<p>The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:</p>
<p><strong>•</strong>&nbsp;&nbsp;Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution<br /><strong>•</strong>&nbsp;&nbsp;Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics<br /><strong>•</strong>&nbsp;&nbsp;Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning<br /><strong>•</strong>&nbsp;&nbsp;Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators<br /><strong>•</strong>&nbsp;&nbsp;Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting</p>
<p>By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.</p>
<p><strong><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noreferrer noopener" title="https://www.amazon.com/gp/product/B07ZY7XMX8"><em>Get your Python One-Liners Now!!</em></a></strong></p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python One-Liners - Trick 3 Lambda, Map, and Ternary Operator" width="1400" height="788" src="https://www.youtube.com/embed/mPowPkO1KSw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
</div>


https://www.sickgaming.net/blog/2020/07/...e-ternary/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016