Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Whether Integer Is Between Two Other Integers?

Tags:

python

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") 
like image 552
Average kid Avatar asked Nov 29 '12 15:11

Average kid


People also ask

How do you check if a number is in between two numbers Python?

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.

How do you check if an integer is in a range Python?

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.

How do you know if a number is within a 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.

How do you code if a number is in between two values?

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.


2 Answers

if 10000 <= number <= 30000:     pass 

For details, see the docs.

like image 64
Paolo Moretti Avatar answered Oct 14 '22 06:10

Paolo Moretti


>>> 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 
like image 33
Bohdan Avatar answered Oct 14 '22 04:10

Bohdan