Python Less Than

Rate this post

The Python less than (left<right) operator returns True when its left operand is smaller than its right operand. When the left operand is greater than or equal to the right operand, the < operator returns False. For example, 2<3 evaluates to True, but 3<2 and 2<2 both evaluate to False.

Examples

Let’s explore a couple of examples regarding the less than (or smaller than) operator.

Is 3 less than 2?

>>> 3 < 2
False

What about 2 less than 3?

>>> 2 < 3
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

Yes!

The list “less than” operator iterates over the lists and checks pairwise if the i-th element of the left operand is less than the i-th element of the right operand. You can find a detailed discussion on the less than operator with list operands below in this article.

Can you use the less than operator on custom objects? Yes!

Python Less Than on Custom Objects

To use the “less than” operator on custom objects, you need to define the __lt__() 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 the other.

In the following code, you check if a Person is less than the other Person by using the age attribute as a decision criterion:

class Person:
    def __init__(self, age):
        self.age = age

    def __lt__(self, other):
        return self.age < other.age



alice = Person(10)
bob = Person(12)

print(alice < bob)
# True

print(bob < alice)
# False

Because Alice is 10 years old and Bob is 12 years old, the result of alice < bob is True and bob < alice is False.

Python Less Than If Statement

The Python less than < operator can be used in an if statement as an expression to determine whether to execute the if branch or not. For example, the less than if condition x<3 checks if the value of variable x is less than 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 18. If so, it enters the if branch. If not, it enters the else branch.

x = int(input('your age: '))

if x < 18:
    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 But Greater Than

Python has a “less than but greater than” operator by chaining together two “less than” operators. For example, the expression 5 < x < 18 would check whether variable x is less than 18 but greater than 5. 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 less than 18 but greater than 2:

x = 8

# Is x less than 18 but greater than 2?
if 2 < x < 18:
    print('yes')

# Output: yes

The code enters the if branch because the if condition is fulfilled.

Python Less Than Lists

The list “less than” operator iterates over the lists and checks pairwise if the i-th element of the left operand is smaller than 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] < [99]. Python first checks 1 < 99 which is True, so it immediately returns True.
  • [1, 2] < [0]. Python first checks 1 < 0 which is False.
  • [1, 2] < [1, 2, 3]. 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 True.
  • [1, 2] < [1, 1, 3]. 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 False.

The same method also applies to strings and other sequence types in Python such as tuples.

Is Everything Less Than None?

You cannot use the less than 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).

OperatorNameDescriptionExample
>Greater ThanReturns True if the left operand is greater than the right operand3 > 2 == True
<Less ThanReturns True if the left operand is smaller than the right operand3 < 2 == False
==Equal ToReturns True if the left operand is the same as the right operand(3 == 2) == False
!=Not Equal ToReturns True if the left operand is not the same as the right operand(3 != 2) == True
>=Greater Than or Equal ToReturns True if the left operand is greater than or equal to the right operand(3 >= 3) == True
<=Less Than or Equal ToReturns True if the left operand is less than or equal to the right operand(3 <= 2) == False