0

why print(2<10<9) evaluates to False

print(2<10<9) print(True<9) """ Output: False True """ """ Less operator associativity is from left to right. As per this rule, 2<10 is evaluated first and should give True then the result of above should have been evaluated second i.e True <9 but actually 10<9 is evaluated second why ? """

10/6/2019 9:06:32 AM

Zahid Khan

4 Answers

New Answer

+3

2 < 10 < 9 is non associative operation and is evaluated as 2 < 10 and 10 < 9 lhs is True and rhs is False but due to "and" (logical and) the overall result is False

+4

10<9 is evaluated second because thats the fixed order of precedence(i.e.,preference) for the < operators.

+4

Actually it is non associative. This is a Python feature called chained comparisons https://docs.python.org/3/reference/expressions.html#comparisons

0

Thanks everyone i got it