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 100You can see the output in the screenshot below.

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 150You can see the output in the screenshot below.

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 rangeYou can see the output in the screenshot below.

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 100You can see the output in the screenshot below.

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 rangeThis 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:
- Difference Between Class and Instance Variables in Python
- Insert a Python Variable into a String
- Write a Variable to a File in Python
- Check if a Variable Contains an Integer in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.