Use two or more relational operators in one senten

2019-01-27 04:56发布

How do two or more relational operators in a single sentence work? For example:

5 < 5 <= 3 > 10

2条回答
Rolldiameter
2楼-- · 2019-01-27 05:33

https://docs.python.org/2/reference/expressions.html#not-in

It's evaluated in order, so your expression expands to

5 < 5 and 5 <= 3 and 3 > 10

which evaluates to False

查看更多
Viruses.
3楼-- · 2019-01-27 05:40

Python supports double-ended comparisons. For example,

3 < x <= 7

is a check for 3 < x and x <= 7 (with x being evaluated just once).

By extension,

5 < 5 <= 3 > 10

means (5 < 5) and (5 <= 3) and (3 > 10), all of which are False, so the whole expression evaluates to False.

查看更多
登录 后发表回答