Skip to main content

Python Numbers: How to Check If a Number Is Less Than Another

Comparing numerical values is a fundamental operation in programming, used for sorting algorithms, filtering data, and controlling program flow. In Python, the less than operator (<) is used to determine if one value is strictly smaller than another.

This guide explains how to use the < operator, how it handles different numeric types (like integers and floats), and how to distinguish between strict inequality (<) and inclusive inequality (<=).

Method 1: Using the Less Than Operator (<)

The < operator compares two values. It returns the boolean True if the value on the left is strictly smaller than the value on the right, and False otherwise.

x = 5
y = 10

# ✅ Basic Comparison
result = x < y
print(f"Is {x} less than {y}? {result}")

# Using in an if-statement
if x < y:
print(f"{x} is smaller.")
else:
print(f"{x} is not smaller.")

Output:

Is 5 less than 10? True
5 is smaller.
note

The comparison is strict. This means 10 < 10 evaluates to False because 10 is not smaller than itself; it is equal.

Method 2: Comparing Different Numeric Types

Python allows you to compare integers (int) and floating-point numbers (float) directly without manual conversion. The interpreter automatically handles the type promotion behind the scenes.

integer_val = 5
float_val = 5.5

# ✅ Comparing int and float
if integer_val < float_val:
print(f"{integer_val} is less than {float_val}")

# Comparing seemingly equal values
int_ten = 10
float_ten = 10.0

print(f"Is {int_ten} < {float_ten}? {int_ten < float_ten}")

Output:

5 is less than 5.5
Is 10 < 10.0? False
tip

While Python handles int vs float comparison well, be cautious when comparing two floats (e.g., 0.1 + 0.2 < 0.3) due to standard floating-point precision issues.

Method 3: Handling Equality (Less Than or Equal To)

A common logic error occurs when a developer wants to perform an action if a number is smaller or the same size, but uses the strict < operator.

The Logic Gap

If you need to define a threshold where the limit itself is included (e.g., "Age must be 18 or lower"), use the Less Than or Equal To operator (<=).

limit = 100
current_value = 100

# ⛔️ Incorrect: Using strict inequality when equality is allowed
# This logic fails to catch the limit itself
if current_value < limit:
print("Value is within limit (Strict).")
else:
print("Limit reached or exceeded (Strict).")

# ✅ Correct: Using inclusive inequality
if current_value <= limit:
print("Value is within limit (Inclusive).")

Output:

Limit reached or exceeded (Strict).
Value is within limit (Inclusive).

Advanced: Chained Comparisons

Python supports a unique and readable syntax called chained comparisons. You can check multiple conditions in a single expression, which is mathematically intuitive.

x = 5

# ✅ Check if x is between 0 and 10
if 0 < x < 10:
print("x is strictly between 0 and 10")

# This is equivalent to:
if 0 < x and x < 10:
print("x is strictly between 0 and 10 (Explicit)")

Output:

x is strictly between 0 and 10
x is strictly between 0 and 10 (Explicit)
warning

Ensure the logic flows correctly. 10 < x < 5 is valid syntax but will always return False because a number cannot be greater than 10 and less than 5 simultaneously.

Conclusion

To check if a number is less than another in Python:

  1. Use < for strict comparison (e.g., a < b). It returns False if numbers are equal.
  2. Use <= if you want to include equality (e.g., a <= b).
  3. Mix Types: You can safely compare integers and floats.
  4. Chain Operators: Use syntax like a < x < b to check ranges concisely.