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'm using 2.3 IDLE and what I've attempted so far is not working:
if number >= 10000 and number >= 30000: print ("you have to pay 5% taxes")
Another way to check if a number is between two numbers in Python is to use the Python range() function and check if the number is included in a created range. To create a range, you can pass two numbers to range(). Then you can use the in logical operator to check if a number is in the created range.
You can check if a number is present or not present in a Python range() object. To check if given number is in a range, use Python if statement with in keyword as shown below. number in range() expression returns a boolean value: True if number is present in the range(), False if number is not present in the range.
The idea is to multiply (x-low) and (x-high). If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0.
To check if a number is between two numbers: Use the && (and) operator to chain two conditions. In the first condition check that the number is greater than the lower range and in the second, that the number is lower than the higher range. If both conditions are met, the number is in the range.
if 10000 <= number <= 30000: pass
For details, see the docs.
>>> r = range(1, 4) >>> 1 in r True >>> 2 in r True >>> 3 in r True >>> 4 in r False >>> 5 in r False >>> 0 in r False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With