Simplify Chained Comparison

前端 未结 3 1308
你的背包
你的背包 2020-11-27 02:28

I have an integer value x, and I need to check if it is between a start and end values, so I write the following statements:



        
3条回答
  •  日久生厌
    2020-11-27 03:24

    It can be rewritten as:

    start <= x <= end:
    

    Or:

    r = range(start, end + 1) # (!) if integers
    if x in r:
        ....
    

提交回复
热议问题