Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] {AND, OR, NOT} How to Apply Logical Operators to All List Elements in Python?

#1
{AND, OR, NOT} How to Apply Logical Operators to All List Elements in Python?

<div><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="{AND, OR, NOT} How to Apply Logical Operators to All List Elements in Python?" width="1400" height="788" src="https://www.youtube.com/embed/sb1A4Qf5nSk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p><strong>Problem</strong>: Given a <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">list </a>of Boolean elements. What’s the best way to <a href="https://blog.finxter.com/python-join-list/" target="_blank" rel="noreferrer noopener">join</a> all elements using the logical “OR” and logical “AND” operations?</p>
<p><strong>Example</strong>: Convert the list <code>[True, True, False]</code> using </p>
<ul>
<li>The logical “AND” operation to <code>True and True and False = False</code>,</li>
<li>The logical “OR” operation to <code>True or True or False = True</code>, and</li>
<li>The logical “NOT” operation to <code>[not True, not True, not False] = [False, False, True]</code>. </li>
</ul>
<figure class="wp-block-image size-large is-resized"><img src="https://blog.finxter.com/wp-content/uploads/2020/06/logicalOpsList-1024x576.jpg" alt="" class="wp-image-9207" width="768" height="432" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/logicalOpsList-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w" sizes="(max-width: 768px) 100vw, 768px" /></figure>
<p><strong>Solution</strong>: </p>
<ul>
<li>To perform logical “AND”, use the built-in Python function <code>all()</code>,</li>
<li>To perform logical “OR”, use the built-in Python function <code>any()</code>, and</li>
<li>To perform logical “NOT”, use a list comprehension statement <code>[not x for x in list]</code>. </li>
</ul>
<p>Here’s the solution for our three examples:</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="">lst = [True, True, False] # Logical "AND"
print(all(lst))
# False # Logical "OR"
print(any(lst))
# True # Logical "NOT"
print([not x for x in lst])
# [False, False, True]
</pre>
<p>This way, you can combine an arbitrary iterable of Booleans into a single Boolean value.</p>
<p><em><strong>Puzzle</strong>: Guess the output of this interactive code snippet—and run it to check if you were correct!</em></p>
<p> <iframe src="https://trinket.io/embed/python/dd8afa5044" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="356" frameborder="0"></iframe> </p>
<p>The challenge in the puzzle is to know that Python comes with <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-check-if-a-python-list-is-empty/" target="_blank">implicit Boolean type conversion</a>: every object has an associated Boolean value. Per convention, all objects are True except “empty” or “zero” objects such as <code>[]</code>, <code>''</code>, <code>0</code>, and <code>0.0</code>. Thus, the result of the function call <code>all([True, True, 0])</code> is <code>False</code>.</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/06/...in-python/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016