0
logical and or evaluation in python
>>> 1 and 3 3 >>> 1 or 3 1 can someone explain the result?
2 Antworten
+ 2
it checks the first value if that's true it returns it so 1 or doesn't need to check a second value. 1 and requires a second value to be checked and then returns the last value checked.
+ 1
Everything except 0 (no value) - is True (has value):
# Logical and
0 and 0 -> 0
0 and 1 -> 0
1 and 0 -> 0
1 and 1 -> 1
# If the left operand is False, it returns 0, else it checks the right and returns it.
0 and 2 -> 0
2 and 0 -> 0
1 and 2 -> 2
2 and 1 -> 1
# Logical or
0 or 0 -> 0
0 or 1 -> 1
1 or 0 -> 1
1 or 1 -> 1
# If the left operand is True, it returns it, no matter what is on the right.
0 or 2 -> 2
2 or 0 -> 2
1 or 2 -> 1
2 or 1 -> 2