{"id":109274,"date":"2020-02-14T08:17:55","date_gmt":"2020-02-14T08:17:55","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=6295"},"modified":"2020-02-14T08:17:55","modified_gmt":"2020-02-14T08:17:55","slug":"python-and-operator-on-two-objects-or-lists-whats-the-result","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/02\/14\/python-and-operator-on-two-objects-or-lists-whats-the-result\/","title":{"rendered":"Python AND Operator On Two Objects or Lists [What\u2019s The Result?]"},"content":{"rendered":"<p>You may already know Python&#8217;s and operator when applied to two Booleans: <\/p>\n<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\nFalse\n>>> True and True\nTrue<\/pre>\n<p>Simple enough. Yet, that&#8217;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>\n<h2>What If You Apply the AND Operator To Two Objects?<\/h2>\n<p>To understand the output, you have to understand two things:<\/p>\n<ul>\n<li>How does the <code>and<\/code> operator work?<\/li>\n<li>What&#8217;s the truth value of any object &#8211; such as a list?<\/li>\n<\/ul>\n<p>Let&#8217;s answer those two questions quickly.<\/p>\n<p><strong>How does the <code>and<\/code> operator work?<\/strong><\/p>\n<p>Let&#8217;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>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Operation<\/strong><\/td>\n<td><strong>Definitions<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>x or y<\/code><\/td>\n<td>if <em>x<\/em> is false, then <em>y<\/em>, else <em>x<\/em><\/td>\n<\/tr>\n<tr>\n<td><code>x and y<\/code><\/td>\n<td>if <em>x<\/em> is false, then <em>x<\/em>, else <em>y<\/em><\/td>\n<\/tr>\n<tr>\n<td><code>not x<\/code><\/td>\n<td>if <em>x<\/em> is false, then <code>True<\/code>, else <code>False<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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>\n<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&#8217;t even look at what the value of <code>y<\/code> actually <em>is<\/em>. If you&#8217;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>\n<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>\n<p>In combination with the next piece of Python knowledge, this leads to an interesting behavior:<\/p>\n<p><strong>What&#8217;s the truth value of any object &#8211; such as a list?<\/strong><\/p>\n<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 &#8220;empty&#8221;, the truth value is <code>False<\/code>. Otherwise, it&#8217;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>\n<p>Now, you&#8217;re equipped with the basics to understand the answer to the following question:<\/p>\n<p><strong>What If You Apply the AND Operator To Two Objects?<\/strong><\/p>\n<p>Say, you&#8217;ve got two non-Boolean objects <code>x<\/code> and <code>y<\/code>. What&#8217;s the result of the operation <code>x and y<\/code>?<\/p>\n<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>\n<p><strong>What If You Apply the AND Operator To Two Lists?<\/strong><\/p>\n<p>Here&#8217;s an example for two list objects:<\/p>\n<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]\n[0, 0, 0, 0]<\/pre>\n<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>\n<p>But what if the first argument is empty?<\/p>\n<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]\n[]<\/pre>\n<p>The result is the first argument (and not a Boolean value <code>False<\/code>). If you&#8217;re in doubt why, consult the above definition again:<\/p>\n<p><code>x and y<\/code>: <em>if x is false, then x, else y<\/em><\/p>\n<h2>Summary<\/h2>\n<p>You&#8217;ve learned that the <code>and<\/code> operator returns the first operand if it evaluates to <code>False<\/code>, otherwise the second operand. <\/p>\n<p>You&#8217;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>\n<p>Finally, you&#8217;ve also learned that an empty object usually evaluates to <code>False<\/code>.<\/p>\n<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>\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.amazon.com\/Python-One-Liners-Christian-Mayer\/dp\/1718500505\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/02\/image-1.png\" alt=\"\" class=\"wp-image-5969\" width=\"111\" height=\"146\"\/><\/a><\/figure>\n<p><a href=\"https:\/\/www.amazon.com\/Python-One-Liners-Christian-Mayer\/dp\/1718500505\/\">Python One-Liners [No Starch Press]<\/a><\/p>\n<p>Get your book!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You may already know Python&#8217;s and operator when applied to two Booleans: >>> True and False False >>> True and True True Simple enough. Yet, that&#8217;s not the whole story: you can use the and operator even on complex data types such as lists or custom objects. So you may ask (and rightly so): What [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-109274","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/109274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=109274"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/109274\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=109274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=109274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=109274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}