02-17-2020, 04:47 PM
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:
Simple enough. Yet, that’s not the whole story: you can use the 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:
Let’s answer those two questions quickly.
How does the operator work?
Let’s start with the official documentation for Boolean operators:
Operation |
Definitions
|
if x is false, then y, else x
|
if x is false, then x, else y
|
if x is false, then, else
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 operation, if the value of is evaluated to , Python simply returns the value of . It doesn’t even look at what the value of actually is. If you’re using Boolean operators and , this is expected behavior because if is , then the determines whether and is .
This leads to the interesting behavior: if and are objects, the result of the operation 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. Otherwise, it’s . So an empty list, an empty string, or a 0 integer value are all . Most other values will be .
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 and . What’s the result of the operation ?
The answer is simple: the result is if is non-empty (and, thus, evaluates to ).
What If You Apply the AND Operator To Two Lists?
Here’s an example for two list objects:
The first argument of the operation is non-empty and evaluates to . Therefore, the result of the operation is the second list argument .
But what if the first argument is empty?
The result is the first argument (and not a Boolean value). If you’re in doubt why, consult the above definition again:
: if x is false, then x, else y
Summary
You’ve learned that the operator returns the first operand if it evaluates to , otherwise the second operand.
You’ve also learned that you can use the 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.
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]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
Python One-Liners [No Starch Press]
Get your book!
You may already know Python’s and operator when applied to two Booleans:
Code:
>>> True and False
False
>>> True and True
TrueSimple enough. Yet, that’s not the whole story: you can use the
Code:
andWhat If You Apply the AND Operator To Two Objects?
To understand the output, you have to understand two things:
- How does the operator work?Code:
and
- What’s the truth value of any object – such as a list?
Let’s answer those two questions quickly.
How does the
Code:
andLet’s start with the official documentation for Boolean operators:
Operation |
Definitions
Code:
x or yif x is false, then y, else x
Code:
x and yif x is false, then x, else y
Code:
not xif x is false, then
Code:
TrueCode:
FalseYou 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 yCode:
xCode:
TrueCode:
yCode:
yCode:
xCode:
yCode:
xCode:
TrueCode:
yCode:
xCode:
yCode:
TrueThis leads to the interesting behavior: if
Code:
xCode:
yCode:
x and yIn 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:
FalseCode:
TrueCode:
FalseCode:
TrueNow, 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:
xCode:
yCode:
x and yThe answer is simple: the result is
Code:
yCode:
xCode:
TrueWhat 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:
andCode:
TrueCode:
[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:
FalseCode:
x and ySummary
You’ve learned that the
Code:
andCode:
FalseYou’ve also learned that you can use the
Code:
andFinally, you’ve also learned that an empty object usually evaluates to
Code:
FalseIf 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]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
Python One-Liners [No Starch Press]
Get your book!

