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

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:

```>>> 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?

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]

https://www.sickgaming.net/blog/2020/02/...he-result/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] Python – How to Sort a List of Dictionaries? xSicKxBot 0 8 Yesterday, 04:25 PM Last Post: xSicKxBot [Tut] How to Read All Lines of a File in a Python One-Liner? xSicKxBot 0 12 04-01-2020, 02:08 PM Last Post: xSicKxBot [Tut] How to Sort a List Alphabetically in Python? xSicKxBot 0 8 03-31-2020, 12:17 PM Last Post: xSicKxBot [Tut] Python: How to Count Elements in a List Matching a Condition? xSicKxBot 0 7 03-30-2020, 11:23 PM Last Post: xSicKxBot [Tut] Python One-Liners – The Ultimate Collection xSicKxBot 0 21 03-28-2020, 05:37 PM Last Post: xSicKxBot [Tut] Python List index() xSicKxBot 0 19 03-27-2020, 02:40 PM Last Post: xSicKxBot [Tut] How to Get the Key with Maximum Value in a Python Dictionary? xSicKxBot 0 21 03-26-2020, 10:42 AM Last Post: xSicKxBot [Tut] Python List count() xSicKxBot 0 18 03-24-2020, 08:14 PM Last Post: xSicKxBot [Tut] Python Regex Quantifiers – Question Mark (?) vs Plus (+) vs Asterisk (*) xSicKxBot 0 26 03-23-2020, 05:41 PM Last Post: xSicKxBot [Tut] Python List reverse() xSicKxBot 0 18 03-22-2020, 01:39 PM Last Post: xSicKxBot

Forum Jump:

 Upcoming Events

 Discord

 Latest Threads Steam - Now Available on Steam – メモリーズオフ... Last Post: xSicKxBot Today 01:01 AM » Replies: 0 » Views: 6 Xbox Wire - Behold the Duck of War in Wo... Last Post: xSicKxBot Today 01:01 AM » Replies: 0 » Views: 7 News - Get a job: Fire Hose Games is hir... Last Post: xSicKxBot Today 01:01 AM » Replies: 0 » Views: 6 News - QuakeCon 25 called off over COVID... Last Post: xSicKxBot Today 01:01 AM » Replies: 0 » Views: 8 News - Phil Spencer Talks Xbox Series X ... Last Post: xSicKxBot Yesterday 07:49 PM » Replies: 0 » Views: 6 [Tut] Python – How to Sort a List of Dic... Last Post: xSicKxBot Yesterday 04:25 PM » Replies: 0 » Views: 8 (Indie Deal) Pirate Warriors 4's out, CI... Last Post: xSicKxBot Yesterday 04:25 PM » Replies: 0 » Views: 8 (Free Game Key) Gone Home, Hob and Drawf... Last Post: xSicKxBot Yesterday 04:25 PM » Replies: 0 » Views: 8 Raylib 3.0 Released Last Post: xSicKxBot Yesterday 01:29 PM » Replies: 0 » Views: 9 Mobile - The award-winning platformer Gr... Last Post: xSicKxBot Yesterday 01:29 PM » Replies: 0 » Views: 9