I find (boolean type) and (integer) will become a integer in Python3. What conversion happens in this line of code?
flag=True
print(flag and 2)
Output:2
I find (boolean type) and (integer) will become a integer in Python3. What conversion happens in this line of code?
flag=True
print(flag and 2)
Output:2
The value of the and
operator (and all other Boolean operators) will always be the last value evaluated. Operator and
stops evaluation when it evaluates to False
so the operand that's evaluated to False
will become the value of the evaluation. Conversely, if it evaluates to True
, then the operand that's evaluated to True
will become the value of the evaluation.
>>> False and 2
False
>>> True and 3
3
>>> 1 and False and 3
False
>>> 1 and 0 and 3
0