Note that the operators and and or do not necessarily produce Boolean values. This can be explained by the fact that the expression x and y is equivalent to:
def and_as_function(x,y):
if not x:
return x
else:
return y
Correspondingly, the expression x or y is equivalent to:
def or_as_function(x,y):
if x:
return x
else:
return y
Interestingly, this means that when executing the statement True or x, the variable x need not even be defined! The same holds for False and x.
Note that, unlike their counterparts in mathematical logic, these operators are no longer commutative in Python. Indeed, the following expressions are not equivalent:
1 or 'a' # produces 1
'a' or 1 # produces 'a'