Create an account


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

#1
Python Ternary Multiple Lines

<div><p>What if you have a ternary operator that’s very long?</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="">var = 'I want to learn Python' if 42**2&lt;166 else 'I want to learn Go programming'
print(var)
# I want to learn Go programming
</pre>
<p><strong>Problem</strong>: How to write the ternary operator in multiple lines?</p>
<h2>Short Recap: Ternary Operator </h2>
<p><a href="https://blog.finxter.com/python-one-line-ternary/" title="Python One Line Ternary" target="_blank" rel="noreferrer noopener"><strong>Ternary Operator</strong></a>: The most basic ternary operator <code>x if c else y</code> consists of three operands <code>x</code>, <code>c</code>, and <code>y</code>. It is an expression with a return value. The ternary operator returns <code>x</code> if the Boolean expression <code>c</code> evaluates to <code>True</code>. Otherwise, if the expression <code>c</code> evaluates to <code>False</code>, the ternary operator returns the alternative <code>y</code>.</p>
<p><strong>Syntax</strong>: The three operands are written as <code>x if c else y</code> which reads as “return <code>x</code> if <code>c</code> else return <code>y</code>“. Let’s write this more intuitively as:</p>
<pre class="wp-block-preformatted">&lt;OnTrue&gt; if &lt;Condition&gt; else &lt;OnFalse&gt;</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Operand</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>&lt;OnTrue&gt;</td>
<td>The return expression of the operator in case the condition evaluates to <code>True</code></td>
</tr>
<tr>
<td>&lt;Condition&gt;</td>
<td>The condition that determines whether to return the &lt;On True&gt; or the &lt;On False&gt; branch.</td>
</tr>
<tr>
<td>&lt;OnFalse&gt;</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>
<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="The Python Ternary Operator -- And a Surprising One-Liner Hack" width="1400" height="788" src="https://www.youtube.com/embed/9XXcUHXrqZ4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p><strong>Related article</strong>: For a full tutorial on the ternary operator, <a href="https://blog.finxter.com/python-one-line-ternary/" target="_blank" rel="noreferrer noopener" title="Python One Line Ternary">check out our detailed blog article</a>.</p>
<h2>Method: Parenthesis to Extend Logical Line Over Multiple Physical Lines</h2>
<p><strong>Solution</strong>: You can extend any <a href="https://docs.python.org/2/reference/lexical_analysis.html#line-structure" target="_blank" rel="noreferrer noopener" title="https://docs.python.org/2/reference/lexical_analysis.html#line-structure">logical line in Python over multiple physical lines</a> by using the parenthesis.</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="">var = 'I want to learn Python' if 42**2&lt;166 else 'I want to learn Go programming'
print(var) var = ('I want to learn Python' if 42**2&lt;166 else 'I want to learn Go programming')
print(var)
# I want to learn Go programming</pre>
<p>This is the <a href="https://pythonchecker.com/" target="_blank" rel="noreferrer noopener">PEP8 </a>standard way of breaking long lines—if you cannot do it in a more natural way (such as avoiding the ternary operator and using the if statement in this example). </p>
<p><strong>Try it yourself:</strong></p>
<p> <iframe src="https://trinket.io/embed/python/4ff1b0b336" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Write a nested ternary operator and break it into multiple lines!</em></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/...ple-lines/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016