I am still learning the idiosyncrasies of the Python language, and ran into something I find very strange. I could not find another post on SO about this, but please feel free to close as duplicate if I missed it.
The example is as follows:
x = (True, False)
y = (True, True)
print(x and y) # (True, True)
print(x or y) # (True, False)
Based on this answer about truthy and falsy values, I would expect the results above to be the same as:
bool(x) and bool(y) # True
bool(x) or bool(y) # True
however the value of x and y
is not a single boolean, it is a tuple of booleans. Why is that, and how is this tuple computed?
Given that the value is a tuple, the next reasonable thing I would expect is that e.g. x and y
would be equal to (x[0] and y[0], x[1] and y[1])
, but as you can see that is not the case. The exact same results are obtained when using a list instead of a tuple.
Could a gentle Pythonista please shed some light for me as to what is happening here?