Determine Whether Integer Is Between Two Other Integers?

前端 未结 11 862
猫巷女王i
猫巷女王i 2020-11-22 15:07

How do I determine whether a given integer is between two other integers (e.g. greater than/equal to 10000 and less than/equal to 30000)?

I\

相关标签:
11条回答
  • 2020-11-22 15:38

    There are two ways to compare three integers and check whether b is between a and c:

    if a < b < c:
        pass
    

    and

    if a < b and b < c:
        pass
    

    The first one looks like more readable, but the second one runs faster.

    Let's compare using dis.dis:

    >>> dis.dis('a < b and b < c')
      1           0 LOAD_NAME                0 (a)
                  2 LOAD_NAME                1 (b)
                  4 COMPARE_OP               0 (<)
                  6 JUMP_IF_FALSE_OR_POP    14
                  8 LOAD_NAME                1 (b)
                 10 LOAD_NAME                2 (c)
                 12 COMPARE_OP               0 (<)
            >>   14 RETURN_VALUE
    >>> dis.dis('a < b < c')
      1           0 LOAD_NAME                0 (a)
                  2 LOAD_NAME                1 (b)
                  4 DUP_TOP
                  6 ROT_THREE
                  8 COMPARE_OP               0 (<)
                 10 JUMP_IF_FALSE_OR_POP    18
                 12 LOAD_NAME                2 (c)
                 14 COMPARE_OP               0 (<)
                 16 RETURN_VALUE
            >>   18 ROT_TWO
                 20 POP_TOP
                 22 RETURN_VALUE
    >>>
    

    and using timeit:

    ~$ python3 -m timeit "1 < 2 and 2 < 3"
    10000000 loops, best of 3: 0.0366 usec per loop
    
    ~$ python3 -m timeit "1 < 2 < 3"
    10000000 loops, best of 3: 0.0396 usec per loop
    

    also, you may use range, as suggested before, however it is much more slower.

    0 讨论(0)
  • 2020-11-22 15:38

    You want the output to print the given statement if and only if the number falls between 10,000 and 30,000.

    Code should be;

    if number >= 10000 and number <= 30000:
        print("you have to pay 5% taxes")
    
    0 讨论(0)
  • 2020-11-22 15:39

    Suppose there are 3 non-negative integers: a, b, and c. Mathematically speaking, if we want to determine if c is between a and b, inclusively, one can use this formula:

    (c - a) * (b - c) >= 0

    or in Python:

    > print((c - a) * (b - c) >= 0)
    True
    
    0 讨论(0)
  • 2020-11-22 15:44

    Your code snippet,

    if number >= 10000 and number >= 30000:
        print ("you have to pay 5% taxes")
    

    actually checks if number is larger than both 10000 and 30000.

    Assuming you want to check that the number is in the range 10000 - 30000, you could use the Python interval comparison:

    if 10000 <= number <= 30000:
        print ("you have to pay 5% taxes")
    

    This Python feature is further described in the Python documentation.

    0 讨论(0)
  • 2020-11-22 15:47

    The trouble with comparisons is that they can be difficult to debug when you put a >= where there should be a <=

    #                             v---------- should be <
    if number >= 10000 and number >= 30000:
        print ("you have to pay 5% taxes")
    

    Python lets you just write what you mean in words

    if number in xrange(10000, 30001): # ok you have to remember 30000 + 1 here :)
    

    In Python3, you need to use range instead of xrange.

    edit: People seem to be more concerned with microbench marks and how cool chaining operations. My answer is about defensive (less attack surface for bugs) programming.

    As a result of a claim in the comments, I've added the micro benchmark here for Python3.5.2

    $ python3.5 -m timeit "5 in range(10000, 30000)"
    1000000 loops, best of 3: 0.266 usec per loop
    $ python3.5 -m timeit "10000 <= 5 < 30000"
    10000000 loops, best of 3: 0.0327 usec per loop
    

    If you are worried about performance, you could compute the range once

    $ python3.5 -m timeit -s "R=range(10000, 30000)" "5 in R"
    10000000 loops, best of 3: 0.0551 usec per loop
    
    0 讨论(0)
提交回复
热议问题