+ 4
Why this evaluation is false?
nums = [1, 2, 3] print(not 5 and not 4 in nums) It gives me false, althoug true and true operation is in print.
3 Answers
+ 4
The order of evaluation for these operators:
1. in
2. not
3. and
so the print can be expressed like this:
print((not 5) and (not (4 in nums)))
(not 5) is False because the boolean value of any nonzero integer is True
bool(5) = True
therefore (not 5) = (not True) = False
While the second part of the expression evaluates to True, it doesn't matter because
(False and True) = False
+ 5
not 5 => false
+ 5
Correct line is:
print(not 5 in nums and not 4 in nums)
-> True