because
print(3 and 4)returns an int
Output:4
This explains ithttps://docs.python.org/3/library/stdtyp...and-or-not Wrote:Boolean Operations — and, or, not
These are the Boolean operations, ordered by ascending priority:
Notes:
Output:Operation Result Notes x or y if x is false, then y, else x (1) x and y if x is false, then x, else y (2) not x if x is false, then True, else False (3)
- This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
- This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
- not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.
tests = ((0, 0), (0, 1), (1, 0), (1, 1), (3, 4), (False, False), (False, True), (True, False), (True, True), ('', ''), ('', 'Something'), ('Something', ''), ('Something', 'Something')) for first, second in tests: if result := first and second: print(f'(True) ({first}, {second}) result = {result}') else: print(f'(False) ({first}, {second}) result = {result}')
Output:(False) (0, 0) result = 0
(False) (0, 1) result = 0
(False) (1, 0) result = 0
(True) (1, 1) result = 1
(True) (3, 4) result = 4
(False) (False, False) result = False
(False) (False, True) result = False
(False) (True, False) result = False
(True) (True, True) result = True
(False) (, ) result =
(False) (, Something) result =
(False) (Something, ) result =
(True) (Something, Something) result = Something)