You may already know Python’s and operator when applied to two Booleans:
>>> True and False False >>> True and True True
Simple enough. Yet, that’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 If You Apply the AND Operator To Two Objects?
To understand the output, you have to understand two things:
- How does the
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 and
operator work?
Let’s start with the official documentation for Boolean operators:
Operation | Definitions |
x or y | if x is false, then y, else x |
x and y | if x is false, then x, else y |
not x | if x is false, then True , else 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 x and y
operation, if the value of x
is evaluated to True
, Python simply returns the value of y
. It doesn’t even look at what the value of y
actually is. If you’re using Boolean operators x
and y
, this is expected behavior because if x
is True
, then the y
determines whether x
and y
is True
.
This leads to the interesting behavior: if x
and y
are objects, the result of the operation 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 False
. Otherwise, it’s True
. So an empty list, an empty string, or a 0 integer value are all False
. Most other values will be 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 x
and y
. What’s the result of the operation x and y
?
The answer is simple: the result is y
if x
is non-empty (and, thus, evaluates to True
).
What If You Apply the AND Operator To Two Lists?
Here’s an example for two list objects:
>>> [1, 2, 3] and [0, 0, 0, 0] [0, 0, 0, 0]
The first argument of the and
operation is non-empty and evaluates to True
. Therefore, the result of the operation is the second list argument [0, 0, 0, 0]
.
But what if the first argument is empty?
>>> [] and [0, 0, 0, 0] []
The result is the first argument (and not a Boolean value False
). If you’re in doubt why, consult the above definition again:
x and y
: if x is false, then x, else y
Summary
You’ve learned that the and
operator returns the first operand if it evaluates to False
, otherwise the second operand.
You’ve also learned that you can use the 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 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:
Python One-Liners [No Starch Press]
Get your book!