PYTHON

Less Than or Equal To in Python: Syntax, Usage, and Examples

The <= operator in Python checks whether the value on the left is less than or equal to the value on the right. If the condition is true, it returns True; otherwise, it returns False.

How to Use the Less Than or Equal To Operator in Python

The syntax for using the less than or equal to operator is straightforward:

value1 <= value2

Here, Python evaluates whether value1 is less than, or equal to, value2. If either condition is satisfied, the result is True.

Example:

print(5 <= 10)   # True
print(7 <= 7)    # True
print(9 <= 4)    # False

The operator works with numbers, strings (based on lexicographical order), and even with variables holding comparable values.

When to Use the Less Than or Equal To Operator in Python

The <= operator is useful in a variety of programming tasks. Here are some common use cases:

1. Conditional Statements

You can use <= in if statements to control program logic based on a condition.

user_age = 17
if user_age <= 18:
    print("You qualify for a student discount.")

This checks if user_age is less than or equal to 18.

2. Loop Conditions

Use <= to keep a loop running until a certain threshold is reached.

count = 1
while count <= 5:
    print(count)
    count += 1

This loop runs while count is less than or equal to 5.

3. Range Validation

It’s useful when checking if a value falls within a specified range.

score = 85
if 80 <= score <= 90:
    print("You're within the B grade range.")

This checks if the score is between 80 and 90, inclusive.

Examples of Less Than or Equal To in Python

Let’s look at a few more practical examples to understand how <= works in real-world scenarios.

Budget Comparison

budget = 100
cart_total = 95

if cart_total <= budget:
    print("Purchase is within budget.")
else:
    print("Over budget!")

Text Comparison

word1 = "apple"
word2 = "banana"

print(word1 <= word2)  # True, because "apple" comes before "banana"

In Python, strings are compared based on alphabetical order using their Unicode values.

Function Conditions

You can return results from a function based on less than or equal comparisons.

def check_speed(speed):
    if speed <= 60:
        return "Safe"
    else:
        return "Too fast!"

print(check_speed(55))  # Output: Safe

Grade Feedback Example

grade = 72

if grade <= 50:
    print("Failing grade.")
elif grade <= 70:
    print("Average grade.")
else:
    print("Good job!")

This shows how you can use multiple <= conditions to break values into categories.

Learn More About the Less Than or Equal To Operator

Chained Comparisons

Python allows chained comparisons, which can be more expressive and readable than using and operators.

temperature = 18
if 15 <= temperature <= 25:
    print("Temperature is within the comfortable range.")

This reads almost like natural language and is common in Python for checking ranges.

<= vs <

It’s easy to confuse <= (less than or equal to) with < (less than). The key difference is inclusion:

  • x < y: True only if x is strictly less than y
  • x <= y: True if x is less than or equal to y

Example:

x = 10
print(x < 10)   # False
print(x <= 10)  # True

Comparison with Variables

The <= operator works with any two values that can be compared, including variables.

a = 12
b = 20
c = 12

print(a <= b)  # True
print(a <= c)  # True
print(b <= a)  # False

Using <= in Sorting Logic

In some applications, like leaderboard logic or sorting algorithms, <= is useful for deciding insertion points or comparison behavior.

def insert_if_allowed(new_score, high_score):
    if new_score <= high_score:
        return "Score too low to enter leaderboard."
    else:
        return "New high score!"

print(insert_if_allowed(85, 90))

Type Errors

Using <= on non-comparable types will raise a TypeError.

# This will raise a TypeError:
# print([1, 2, 3] <= 5)

Always make sure you're comparing compatible data types—like integers to integers or strings to strings.

The <= operator in Python is essential for writing conditional logic, validating input ranges, and building comparisons into functions, loops, and sorting logic. Whether you’re checking if a user’s input fits within boundaries, looping over a set number of items, or categorizing data, using <= helps keep your code clean and expressive.

Keep experimenting with different data types and conditions, and you'll quickly find how versatile this operator can be.

Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH