print(45 and 2)
What is the mechanism behind the result of 2?
print(45 and 2)
What is the mechanism behind the result of 2?
Based on the docs: https://docs.python.org/3/reference/expressions.html#and
"The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned."
Which basically means that you will always get 2 long as you keep the 45 (45 != 0). If you try print(0 and 2) you will get 0.
The and
operator checks the first operand. If it is truthy, the second operand is returned. Else, the first operand is returned. 45 is a non-zero integer, so it is truthy. Therefore, the second operand (2) is returned. The or
operator works in a similar way.
https://realpython.com/python-and-operator/#using-pythons-and-operator-with-common-objects