This tutorial provides you with multiple methods to check if an integer number lies in the given range or not. It includes several examples to bring clarity.
Let’s first define the problem. We want to verify whether an integer value lies between two other numbers, for example, 1000 and 7000:
So, we need a simple method that can tell us about any numeric value if it belongs to a given range. Hence, in this post, we’ll describe three ways of solving this problem. You can choose which of these suits you the best.
Two of these methods work in Python 3, and the third one is specific to Python 2.7.
Python | Check Integer in Range or Between Two Numbers
Let’s now open up all three ways to check if the integer number is in range or not.
Using Python comparison operator
In Python programming, we can use comparison operators to check whether a value is higher or less than the other. And then, we can take some action based on the result.
The below tutorial defines the built-in comparison operators available in Python. Check it out.
let’s now check out a sample code to see how to use the comparison operators.
""" Desc: Python program to check if the integer number is in between a range """ # Given range X = 1000 Y = 7000 def checkRange(num): # using comaparision operator if X <= num <= Y: print('The number {} is in range ({}, {})'.format(num, X, Y)) else: print('The number {} is not in range ({}, {})'.format(num, X, Y)) # Test Input List testInput = [X-1, X, X+1, Y+1, Y, Y-1] for eachItem in testInput: checkRange(eachItem)
We’ve written a function to check whether the input number is in the given range or not. It is using the following comparison operator syntax:
if X <= num <= Y:
Also, we’ve prepared the test data in a list accordingly. We wish to test all +ve and edge cases as well. Hence, we made the test input list, as shown below:
# Test Input List testInput = [X-1, X, X+1, Y+1, Y, Y-1]
This list helps us run some regular test cases, upper and lower boundary tests. So, when we run the above program, it gets us the following result:
The number 999 is not in range (1000, 7000) The number 1000 is in range (1000, 7000) The number 1001 is in range (1000, 7000) The number 7001 is not in range (1000, 7000) The number 7000 is in range (1000, 7000) # We've included upper range as well The number 6999 is in range (1000, 7000)
Python range() to check integer in between two numbers
We can also use the Python range function that does this job for us. It can quite easily identify if the integer lies between two numbers or not.
Please check the following example:
""" Desc: Python range to check if the integer is in between two numbers """ # Given range X = 1000 Y = 7000 def checkRange(num): # using comaparision operator if num in range(X, Y): print('The number {} is in range ({}, {})'.format(num, X, Y)) else: print('The number {} is not in range ({}, {})'.format(num, X, Y)) # Test Input testInput = [X-1, X, X+1, Y+1, Y, Y-1] for eachItem in testInput: checkRange(eachItem)
Here, we’ve called the range() function which includes the lower range (X) but discards the edge value, i.e., Y.
Hence, when you execute the above code, it results in the below output:
The number 999 is not in range (1000, 7000) The number 1000 is in range (1000, 7000) The number 1001 is in range (1000, 7000) The number 7001 is not in range (1000, 7000) The number 7000 is not in range (1000, 7000) # Python range doesn't include upper range value The number 6999 is in range (1000, 7000)
Python xrange() to check integer in between two numbers
This method (xrange()) would only work in Python 2.7 or below. But since Python 2.7 is still in use, so we are giving an example of the same.
Please see the below coding snippet using the Python xrange function:
""" Desc: Python xrange to check if the integer is in between two numbers """ # Given range X = 1000 Y = 7000 def checkRange(num): # using comaparision operator if num in xrange(X, Y): print('The number {} is in range ({}, {})'.format(num, X, Y)) else: print('The number {} is not in range ({}, {})'.format(num, X, Y)) # Test Input testInput = [X-1, X, X+1, Y+1, Y, Y-1] for eachItem in testInput: checkRange(eachItem)
Here is the output that will you get after running the above program.
The number 999 is not in range (1000, 7000) The number 1000 is in range (1000, 7000) The number 1001 is in range (1000, 7000) The number 7001 is not in range (1000, 7000) The number 7000 is not in range (1000, 7000) The number 6999 is in range (1000, 7000)
The output of xrange() is almost identical to what range() gave us.
We hope that after wrapping up this tutorial, you should know how to check if an integer lies in between two numbers or not. However, you may practice more with examples to gain confidence.
Also, to learn Python from scratch to depth, do read our step-by-step Python tutorial.