0

The title pretty much summarizes it all.

Why, in Python, performing a logical and with an empty list and a boolean literal (like True or False) returns an empty list, but performing a logical or returns a boolean value?

Directly in Python here is what I did, but have not understood it:

$ python
Python 3.10.6 (main, Aug 24 2022, 14:30:54) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> empty_list = []
>>> empty_list
[]
>>> empty_list and True
[]
>>> empty_list and False
[]
>>> empty_list or True
True
>>> empty_list or False
False

I know I can use a cast bool(empty_list) and then I'd only have boolean as a return, but my question is whats is so different in using an and or or that the return different types are different? Aren't they both logical operators?

gmauch
  • 1,316
  • 4
  • 25
  • 39

0 Answers0