Use two or more relational operators in one sentence in python

孤者浪人 提交于 2019-11-26 22:01:43

问题


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

5 < 5 <= 3 > 10

回答1:


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.




回答2:


https://docs.python.org/2/reference/expressions.html#comparisons

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

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

which evaluates to False



来源:https://stackoverflow.com/questions/22776897/use-two-or-more-relational-operators-in-one-sentence-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!