[Tut] Python AND Operator On Two Objects or Lists [What’s The Result?] - 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 AND Operator On Two Objects or Lists [What’s The Result?] (/thread-93641.html) |
[Tut] Python AND Operator On Two Objects or Lists [What’s The Result?] - xSicKxBot - 02-17-2020 Python AND Operator On Two Objects or Lists [What’s The Result?] <div><p>You may already know Python’s and operator when applied to two Booleans: </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="">>>> True and False False >>> True and True True</pre> <p>Simple enough. Yet, that’s not the whole story: you can use the <code>and</code> operator even on complex data types such as lists or custom objects. So you may ask (and rightly so):</p> <h2>What If You Apply the AND Operator To Two Objects?</h2> <p>To understand the output, you have to understand two things:</p> <ul> <li>How does the <code>and</code> operator work?</li> <li>What’s the truth value of any object – such as a list?</li> </ul> <p>Let’s answer those two questions quickly.</p> <p><strong>How does the <code>and</code> operator work?</strong></p> <p>Let’s start with the <a href="https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">official documentation</a> for Boolean operators:</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><strong>Operation</strong></td> <td><strong>Definitions</strong></td> </tr> <tr> <td><code>x or y</code></td> <td>if <em>x</em> is false, then <em>y</em>, else <em>x</em></td> </tr> <tr> <td><code>x and y</code></td> <td>if <em>x</em> is false, then <em>x</em>, else <em>y</em></td> </tr> <tr> <td><code>not x</code></td> <td>if <em>x</em> is false, then <code>True</code>, else <code>False</code></td> </tr> </tbody> </table> </figure> <p>You must understand the deeper meaning of those definitions: all of them are short-circuit which means that as soon as the condition is fullfilled, they will abort further processing. </p> <p>In the <code>x and y</code> operation, if the value of <code>x</code> is evaluated to <code>True</code>, Python simply returns the value of <code>y</code>. It doesn’t even look at what the value of <code>y</code> actually <em>is</em>. If you’re using Boolean operators <code>x</code> and <code>y</code>, this is expected behavior because if <code>x</code> is <code>True</code>, then the <code>y</code> determines whether <code>x</code> and <code>y</code> is <code>True</code>.</p> <p>This leads to the interesting behavior: if <code>x</code> and <code>y</code> are objects, the result of the operation <code>x and y</code> will be an object, too! (And not a Boolean value.)</p> <p>In combination with the next piece of Python knowledge, this leads to an interesting behavior:</p> <p><strong>What’s the truth value of any object – such as a list?</strong></p> <p>The <a rel="noreferrer noopener" aria-label="Python convention (opens in a new tab)" href="https://docs.python.org/2/library/stdtypes.html#truth-value-testing" target="_blank">Python convention</a> is simple: if the object is “empty”, the truth value is <code>False</code>. Otherwise, it’s <code>True</code>. So an empty list, an empty string, or a 0 integer value are all <code>False</code>. Most other values will be <code>True</code>.</p> <p>Now, you’re equipped with the basics to understand the answer to the following question:</p> <p><strong>What If You Apply the AND Operator To Two Objects?</strong></p> <p>Say, you’ve got two non-Boolean objects <code>x</code> and <code>y</code>. What’s the result of the operation <code>x and y</code>?</p> <p>The answer is simple: the result is <code>y</code> if <code>x</code> is non-empty (and, thus, evaluates to <code>True</code>).</p> <p><strong>What If You Apply the AND Operator To Two Lists?</strong></p> <p>Here’s an example for two list objects:</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="">>>> [1, 2, 3] and [0, 0, 0, 0] [0, 0, 0, 0]</pre> <p>The first argument of the <code>and</code> operation is non-empty and evaluates to <code>True</code>. Therefore, the result of the operation is the second list argument <code>[0, 0, 0, 0]</code>. </p> <p>But what if the first argument is empty?</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="">>>> [] and [0, 0, 0, 0] []</pre> <p>The result is the first argument (and not a Boolean value <code>False</code>). If you’re in doubt why, consult the above definition again:</p> <p><code>x and y</code>: <em>if x is false, then x, else y</em></p> <h2>Summary</h2> <p>You’ve learned that the <code>and</code> operator returns the first operand if it evaluates to <code>False</code>, otherwise the second operand. </p> <p>You’ve also learned that you can use the <code>and</code> operator even for non-Boolean types in which case the result will be an object, not a Boolean value.</p> <p>Finally, you’ve also learned that an empty object usually evaluates to <code>False</code>.</p> <p>If you find this interesting, feel free to check out my upcoming Python book that shows you hundreds of small Python tricks like this one:</p> <figure class="wp-block-image size-large is-resized"><a href="https://www.amazon.com/Python-One-Liners-Christian-Mayer/dp/1718500505/"><img src="https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png" alt="" class="wp-image-5969" width="111" height="146"/></a></figure> <p><a href="https://www.amazon.com/Python-One-Liners-Christian-Mayer/dp/1718500505/">Python One-Liners [No Starch Press]</a></p> <p>Get your book!</p> </div> https://www.sickgaming.net/blog/2020/02/14/python-and-operator-on-two-objects-or-lists-whats-the-result/ |