Use two or more relational operators in one sentence in python

后端 未结 2 1257
南方客
南方客 2020-12-07 00:13

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

5 < 5 <= 3 > 10
2条回答
  •  抹茶落季
    2020-12-07 00:46

    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.

提交回复
热议问题