How to Determine Whether a Given Integer is Between two Other Integers in Python
Created
Modified
Using Comparisons
Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:
See the following example:
#!/usr/bin/python3
i = 20
if 10 < i < 40:
print(True)
True
Using range
For example,
#!/usr/bin/python3
i = 10
r = range(10, 30)
if i in range(10, 30):
print(True)
print(30 in r)
print(20 in r)
True False True