Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python AND Operator On Two Objects or Lists [What’s The Result?]
1
Python AND Operator On Two Objects or Lists [What’s The Result?]

You may already know Python’s and operator when applied to two Booleans:

Code:
>>> True and False False >>> True and True True

Simple enough. Yet, that’s not the whole story: you can use the
Code:
and
operator even on complex data types such as lists or custom objects. So you may ask (and rightly so):

What If You Apply the AND Operator To Two Objects?

To understand the output, you have to understand two things:
  • How does the
    Code:
    and
    operator work?

  • What’s the truth value of any object – such as a list?

Let’s answer those two questions quickly.

How does the
Code:
and
operator work?


Let’s start with the official documentation for Boolean operators:

Operation |
Definitions

Code:
x or y
|
if x is false, then y, else x

Code:
x and y
|
if x is false, then x, else y

Code:
not x
|
if x is false, then
Code:
True
, else
Code:
False

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.

In the
Code:
x and y
operation, if the value of
Code:
x
is evaluated to
Code:
True
, Python simply returns the value of
Code:
y
. It doesn’t even look at what the value of
Code:
y
actually is. If you’re using Boolean operators
Code:
x
and
Code:
y
, this is expected behavior because if
Code:
x
is
Code:
True
, then the
Code:
y
determines whether
Code:
x
and
Code:
y
is
Code:
True
.

This leads to the interesting behavior: if
Code:
x
and
Code:
y
are objects, the result of the operation
Code:
x and y
will be an object, too! (And not a Boolean value.)

In combination with the next piece of Python knowledge, this leads to an interesting behavior:

What’s the truth value of any object – such as a list?

The Python convention is simple: if the object is “empty”, the truth value is
Code:
False
. Otherwise, it’s
Code:
True
. So an empty list, an empty string, or a 0 integer value are all
Code:
False
. Most other values will be
Code:
True
.

Now, you’re equipped with the basics to understand the answer to the following question:

What If You Apply the AND Operator To Two Objects?

Say, you’ve got two non-Boolean objects
Code:
x
and
Code:
y
. What’s the result of the operation
Code:
x and y
?

The answer is simple: the result is
Code:
y
if
Code:
x
is non-empty (and, thus, evaluates to
Code:
True
).

What If You Apply the AND Operator To Two Lists?

Here’s an example for two list objects:

Code:
>>> [1, 2, 3] and [0, 0, 0, 0] [0, 0, 0, 0]

The first argument of the
Code:
and
operation is non-empty and evaluates to
Code:
True
. Therefore, the result of the operation is the second list argument
Code:
[0, 0, 0, 0]
.

But what if the first argument is empty?

Code:
>>> [] and [0, 0, 0, 0] []

The result is the first argument (and not a Boolean value
Code:
False
). If you’re in doubt why, consult the above definition again:

Code:
x and y
: if x is false, then x, else y

Summary

You’ve learned that the
Code:
and
operator returns the first operand if it evaluates to
Code:
False
, otherwise the second operand.

You’ve also learned that you can use the
Code:
and
operator even for non-Boolean types in which case the result will be an object, not a Boolean value.

Finally, you’ve also learned that an empty object usually evaluates to
Code:
False
.

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:

[Image: image-1.png]

Python One-Liners [No Starch Press]

Get your book!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: