The Python less than or equal to (left<=right
) operator returns True
when its left
operand does not exceed the right
operand. When the left
operand is greater than the right
operand, the <=
operator returns False
. For example, 2<=3
and 2<=2
evaluate to True
, but 3<=2
and evaluates to False
.
Examples
Let’s explore a couple of examples regarding the less than or equal to operator.
Is 3 less than or equal to 2?
>>> 3 <= 2 False
What about 2 less than or equal to 3?
>>> 2 <= 3 True
And 2 less than or equal to itself?
>>> 2 <= 2 True
Can you compare collections such as lists?
>>> [1, 2] <= [99] True >>> [1, 2] <= [0] False >>> [1, 2] <= [1, 2, 3] True >>> [1, 2] <= [1, 1, 3] False >>> [1, 2] <= [1, 2] True
Yes!
The list “less than or equal to” operator iterates over the lists and checks pairwise if the i-th element of the left operand is at most the i-th element of the right operand (see detailed discussion below).
Can you use the less than or equal to operator on custom objects? Yes!
Python Less Than or Equal to Magic Method
To use the “less than or equal to” operator on custom objects, you need to define the __le__()
dunder method that takes two arguments: self
and other
. You can then use attributes of the custom objects to determine if one is less than or equal to the other.
In the following code, you check if a Person is less than or equal to the other Person by using the age
attribute as a decision criterion:
class Person: def __init__(self, age): self.age = age def __le__(self, other): return self.age <= other.age alice = Person(10) bob = Person(12) print(alice <= bob) # True print(bob <= alice) # False print(bob <= bob) # True
Because Alice is 10 years old and Bob is 12 years old, the result of alice <= bob
is True
and bob <= alice
is False
. If you compare Bob with himself, the result is True
as defined in the __le__
magic method.
Python Less Than or Equal to If Statement
The Python less than or equal to <
= operator can be used in an if statement as an expression to determine whether to execute the if branch or not. For example, the condition x<=3
checks if the value of variable x
is less than or equal to 3, and if it is, the if branch is entered.
The following code asks the user to input their age using the input()
function. It then checks if the user input, when converted to an integer using int()
, is smaller than or equal to 17. If so, it enters the if branch. If not, it enters the else branch.
x = int(input('your age: ')) if x <= 17: print('you cannot vote') else: print('you can vote')
Here’s an example execution of this code where the if branch is not entered:
your age: 18 you can vote
Here’s an example execution where the if branch is entered:
your age: 13 you cannot vote
Python Less Than or Equal to Chaining
You can chain together two “less than or equal to” operators. For example, the expression 5 <= x <= 18
would check whether variable x
is between 5 and 18, both interval boundaries are included. Formally, the expression x <= y <= z
is just a shorthand expression for (x <= y) and (y <= z)
.
Here’s a minimal example that checks if variable x
is between 5 and 18.
x = 8 # Is x between 5 and 18? if 5 <= x <= 18: print('yes') # Output: yes
The code enters the if branch because the if condition is fulfilled.
Python Less Than or Equal For Loop
There’s no “less than or equal to” condition to be used in for
loops. If you want to iterate over all elements in a given iterable that are less than or equal to an element y
, create a filtered list with list comprehension such as [x for x in iterable if x<=y]
. You can then iterate over the generated list.
Here’s an example where you iterate in a for loop over all list elements that are less than or equal to y=5
:
elements = [1, 3, 5, 7, 9] y = 5 for element in [x for x in elements if x<=y]: print(element)
The output is:
1 3 5
Python Less Than or Equal to Float
Never test floating-point numbers for equality. The reason is that floating-point numbers are inherently imprecise and two floats that should be equal from a mathematical point of view, may not actually be. Instead, use the Decimal data type instead of float that is more precise.
x = 1.92 - 1.52 if 0.40 <= x: print("Yes!") else: print("No!")
Surprisingly to many coders, the output is "No!"
. See here for a more detailed discussion.
Here’s the solution with the Decimal
type:
from decimal import Decimal x = Decimal('1.92') - Decimal('1.52') if Decimal('0.40') <= x: print("Yes!") else: print("No!")
Now, the output is "Yes!"
as it should be!
Python Less Than or Equal to Lists
The list “less than or equal to” operator iterates over the lists and checks pairwise if the i-th element of the left operand is smaller than or equal to the i-th element of the right operand.
>>> [1, 2] <= [99] True >>> [1, 2] <= [0] False >>> [1, 2] <= [1, 2, 3] True >>> [1, 2] <= [1, 1, 3] False >>> [1, 2] <= [1, 2] True
[1, 2] <= [99]
. Python first checks1 <= 99
which isTrue
, so it immediately returnsTrue
.[1, 2] <
. Python first checks=
[0]1 <= 0
which isFalse
.[1, 2] <
. Python first compares 1 and 1—a tie! So, it moves on to the second elements 2 and 2—tie again! So, it moves to the third elements as a tie-breaker. But only the second list has a third element so it is considered greater than the first and the result of the operation is=
[1, 2, 3]True
.[1, 2] <
. Python compares elements 1 and 1—a tie! But then it compares the second elements 2 and 1 and determines that the first is not less than the second, so the result is=
[1, 1, 3]False
.[1, 2] <= [1, 2]
. The lists contain the same elements, so pairwise comparison results inTrue
.
The same method also applies to strings and other sequence types in Python such as tuples.
Is Everything Less Than or Equal to None?
You cannot use the less than or equal to operator with None
as one of its operands. Python 3 expects that both operands implement the comparable interface, but the None
type does not. That’s why Python raises a TypeError
if you try to compare variables with None
.
>>> 21 <= None Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> 21 <= None TypeError: '<=' not supported between instances of 'int' and 'NoneType'
Comparison Operators
Comparison operators are applied to comparable objects and they return a Boolean value (True
or False
).
Operator | Name | Description | Example |
---|---|---|---|
> | Greater Than | Returns True if the left operand is greater than the right operand | 3 > 2 == True |
< | Less Than | Returns True if the left operand is smaller than the right operand | 3 < 2 == False |
== | Equal To | Returns True if the left operand is the same as the right operand | (3 == 2) == False |
!= | Not Equal To | Returns True if the left operand is not the same as the right operand | (3 != 2) == True |
>= | Greater Than or Equal To | Returns True if the left operand is greater than or equal to the right operand | (3 >= 3) == True |
<= | Less Than or Equal To | Returns True if the left operand is less than or equal to the right operand | (3 <= 2) == False |
Programmer Humor – Blockchain
