Check if a Variable is Between Two Numbers in Python

When I first started programming in Python, one of the most common tasks I encountered was verifying whether a variable falls within a specific range of values. This is a fundamental operation in many applications, from validating user input to controlling program flow based on numeric conditions.

In this article, I’ll share practical methods to check if a variable is between two numbers in Python. I’ll explain straightforward techniques using comparison operators, built-in functions, and more. Whether you’re a beginner or an experienced Python developer, these methods are easy to understand and implement.

Check if a Variable is Between Two Numbers in Python

In everyday Python programming, you often need to ensure that a variable lies within a certain range. For example, in a US-based financial application, you might want to verify if a customer’s credit score falls between 600 and 850 to determine loan eligibility.

Checking ranges helps enforce data integrity, control logic, and improve user experience. Python’s intuitive syntax makes these checks simple and readable.

Method 1: Use Comparison Operators in Python

The most direct way I use to check if a variable is between two numbers is by chaining comparison operators. Python allows expressions like this:

x = 75
if 50 < x < 100:
    print(f"{x} is between 50 and 100")
else:
    print(f"{x} is NOT between 50 and 100")

Output:

75 is between 50 and 100

You can see the output in the screenshot below.

Check if a Variable is Between Two Numbers in Python

This syntax reads naturally: check if x is greater than 50 and less than 100. It’s concise, efficient, and the preferred efficient way.

Method 2: Use Logical Operators and in Python

Another way I sometimes use, especially when conditions get more complex, is combining two comparisons with the and operator:

x = 120
if x > 50 and x < 150:
    print(f"{x} falls between 50 and 150")
else:
    print(f"{x} does NOT fall between 50 and 150")

Output:

120 falls between 50 and 150

You can see the output in the screenshot below.

Check if a Variable is Between Two Numbers Python

This method is equivalent to method 1 but more explicit. It’s helpful when you want to add additional conditions.

Method 3: Include Boundary Values with <= and >=

In many cases, you want to include the boundary numbers themselves. For example, checking if a temperature reading falls between 32°F and 100°F inclusive:

temperature = 32
if 32 <= temperature <= 100:
    print(f"Temperature {temperature}°F is within the safe range")
else:
    print(f"Temperature {temperature}°F is outside the safe range")

Output:

Temperature 32°F is within the safe range

You can see the output in the screenshot below.

Python Check if a Variable is Between Two Numbers

Using <= and >= includes the boundary values, which are often necessary in real-world scenarios.

Method 4: Use Python’s range() Function (Integer Checks Only)

For integer variables, I sometimes use the range() function to check if a value lies within a range:

score = 85
if score in range(50, 101):  # range is inclusive of 50 but exclusive of 101
    print(f"Score {score} is between 50 and 100")
else:
    print(f"Score {score} is outside the range")

Output:

Score 85 is between 50 and 100

You can see the output in the screenshot below.

Check if Python Variable is Between Two Numbers

Note: range() excludes the upper bound, so use range(start, end+1) if you want to include the upper limit.

Method 5: Create a Reusable Function to Check Range in Python

To keep my code clean and reusable, I often encapsulate the check in a function:

def is_between(value, low, high, inclusive=True):
    if inclusive:
        return low <= value <= high
    else:
        return low < value < high

# Example usage
age = 21
if is_between(age, 18, 65):
    print(f"Age {age} is within working age range")
else:
    print(f"Age {age} is outside working age range")

Output:

Age 21 is within working age range

This function lets you choose whether to include the boundaries, making it versatile for different use cases.

Practical Example: Validate User Input in a US-Based Application

Imagine you are building a Python application for a US bank that requires validating if a user’s income lies between $30,000 and $150,000 before processing a loan application.

def validate_income(income):
    if 30000 <= income <= 150000:
        print("Income is within the eligible range.")
    else:
        print("Income is outside the eligible range.")

# Example input
user_income = 45000
validate_income(user_income)

Output:

Income is within the eligible range.

This simple check helps ensure the application processes only valid data.

Tips for Checking Ranges in Python

  • Use chained comparisons (low < x < high) for clarity and efficiency.
  • Use <= and >= to include boundary values as needed.
  • For integers, range() can be handy, but remember it excludes the upper bound.
  • Encapsulate range checks in functions to improve code reuse.
  • Always validate input types before range checks to avoid errors.

Checking if a variable is between two numbers in Python is an essential skill I use daily. Python’s clean syntax and flexible options make it easy to write readable and efficient code for range validation.

Try these methods in your projects, and you’ll find Python makes these checks intuitive and powerful.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.