Python

Check Number Between Two Numbers Python

This tutorial will teach you several ways to determine whether or not an integer value is inside the specified range. To add clarity, various examples are provided. Let’s start by defining the problem. We are attempting to determine whether a numeric value falls between two numbers or lies within a range or not, Therefore, we require a simple function/method or technique by using which can determine if a number lies between two numbers or a range. So, we will discuss different approaches to solving this problem in this post.

How to Check Whether a Number Exists Between Two Numbers?

In python, you can determine whether a number exists between two numbers or a specified range by using some functions, loops, conditions, and operators. In the following section, we will show how you can achieve this task using different approaches.

Using Comparison Operators

Example 1:

In Python programming, comparison operators can be used to determine whether one number is greater or less than another. Based on the outcome, we can perform different tasks using them. A few inbuilt comparison operators are used in the program below.

Code:

n1 = 100
n2 = 200
def checkNum(n):  
    if n1 <= n <= n2:
        print('The number {} lies between {} and {}'.format(n, n1, n2))
    else:
        print('The number {} is not between {} and {}'.format(n, n1, n2))

number = 110
checkNum(number)

Output:

We have created a function to determine whether or not the given number falls within the range. It uses the if statement and comparison operator syntax as follows:

If n1 <= n <= n2

The function is designed in such a way that if the specified number lies between the specified range, the statement “the number lies between the range” will be displayed as output. If the number does not lie between the range, the statement “number is not between range” will be displayed. The above program determines if the specified number is lying between 100 and 200, as we set the variables n1 and n2 as 100 and 200 respectively. Hence, the condition will look like this:

100 <= number <=200

We specified the number as 110 which satisfies the condition. Therefore, the program has returned the statement that “The number 110 lies between 100 and 200”. Now, let’s check this for another number.

Code:

n1 = 100
n2 = 200
def checkNum(n):
    if n1 <= n <= n2:
        print('The number {} lies between {} and {}'.format(n, n1, n2))
    else:
        print('The number {} is not between {} and {}'.format(n, n1, n2))

number = 250
checkNum(number)

Output:

As 250 does not exist within the range of 100 to 200, the function has returned a statement that “the number 250 is not between 100 and 200”.

You can modify the ranges in the above program just by changing the values of variables n1 and n2.

Example 2:

Now, we will create the same program as above. But this time we will take the numbers to set limits and the number that is required to be checked by the user instead of initializing values in the program.

Code:

n1 = int(input("Enter the lower limit: "))
n2 = int(input("Enter the higher limit: "))
def checkNum(n):
    if n1 <= n <= n2:
        print('The number {} lies between {} and {}'.format(n, n1, n2))
    else:
        print('The number {} is not between {} and {}'.format(n, n1, n2))
number = int(input("Enter the number to be checked: "))
checkNum(number)

Output:

To set the limits, we have taken inputs from the user using the input() function. The input() method in Python is used to take user input. The input() function changes any input the user supplies into a string. Integer values entered via the input() function are converted to strings. We applied int() types on the input() function so the input value will be cast into an int data type rather than a string. We first entered the lower limit ‘n1’ as 10 and the higher limit ‘n2’ as 20. We used the same condition which has been used in the program above.

Then, again we used the input() function to take the number (to be checked) from the user. We entered number 15. As 15 is greater than 10 and less than 20, it means it is satisfying our condition. Thus, the statement “The number 15 lies between 10 and 20” is returned by the function. Let us check this program with different input values.

Output:

This time we entered 125 in n1 and 375 in n2. The number to be checked is 124. The function has returned the statement that “the number 124 is not between 125 and 375”.

Using the ‘In’ Logical Operator with range() Function

Using the Python range() function, you can also determine whether a number is part of a created range and see if it falls between two other numbers in Python. You can supply one or two parameters to the range() function to generate a range of numbers between two values. After that, you can verify whether a number exists in the created range using the ‘in’ logical operator. To determine whether a value exists in a series or sequence (range, list, string, etc.), we can use the ‘in’ keyword. Here is a straightforward Python code that will determine whether a given number is between two other numbers.

Code:

h_limit = 2500

def checkinRange(num):
    if num in range(h_limit):
        print('The number {} lies between 0 and {}'.format(num, h_limit))
    else:
        print('The number {} lies between 0 and {}'.format(num, h_limit))
number = 1234
checkinRange(number)

Output:

First, we set/initialize the variable ‘h_limit’ as 2500. Then, we created a customized function to determine if the number lies between two numbers or not. To set/specify the range, we used the range() function and passed h_limit as an argument. As mentioned before the range function can take 2 parameters, one for the lower limit and the second for the higher limit. But in this case, we only supplied h_limit which is the higher limit. By default, the range() function set the lower limit to 0 if it is not specified. So, this program will check whether our number falls in the range of 1 to 2499. Note that, the range function() does not include the last number in the higher limit. We passed the number 1234 to the function. The statement ‘the number 1234 lies between 0 and 2500’ (the number 2500 will not be included) is returned by the function.

Now let us try this function by also specifying both lower and higher limits of the range() function.

Code:

l_limit = 1000
h_limit = 1500
def checkinRange(num):
    if num in range(h_limit):
        print('The number {} lies between {} and {}'.format(num,l_limit, h_limit))
    else:
        print('The number {} lies between {} and {}'.format(num,l_limit, h_limit))
number = 1300
checkinRange(number)

Output:

Now, the lower and upper limits for the range() function are specified as 1000 and 1500 respectively. Now, our function will check if the number 1300 (which we have specified) is between 1000 and 1499 or not. As the condition is met, the function has printed the statement that “the number 1300 lies between 1000 and 1500.

Note that we have used the range function. So, the last number i.e. 1500 will not be included/added to the range.

If a Number Exists in a List that is Created Between Two Numbers

In this example, we will first create a list between two numbers when we check if a specified number exists within the list or not. For creating the list of the specified range, we will again use the range() function to create our list.

Code:

l = list(range(5,20+1))
print(l)

Output:

We have created a list between numbers 5 and 20. Take note of the list() function’s usage. It guarantees that the result will be in list form. Additionally, take note of the usage of +1, which confirms that the last number i.e., 20 will be included in our list.

To quickly determine whether the number exists, one can use a loop to iterate over all the items. If the number is present in the list, this method returns “number exists in the list”; otherwise, it returns “number doesn’t exist in the list”.

Code:

n=11

if n in l:
    print("number {} exist in the list".format(n))
else:
    print("number {} doesn't exist in the list".format(n))

Output:

To determine whether the number is present in a list, the keyword “in” is used. An “if-else” statement is then used to display the statement.

Conclusion

In this tutorial, we teach you how to check whether a number falls or exists between two numbers or lies within a specified range. We used three approaches in this post to accomplish this task. First, we used the comparison operator to create a condition to check if the given number is between two numbers. Secondly, we used the range() function and the ‘in’ keyword. In the last we first created a list between two numbers then we checked if the number existed on our list.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.