The __lt__() method is used to check whether one object is less than another. It is primarily used to compare objects of the same or closely related types.

This is the method that actually gets called in the background when we use the less than( < ) operator. So when we use the operator as  x < y , Python in the background calls x.__lt__(y).

The method belongs to a set of magic methods known as comparison methods.  The methods are used to compare the value of two related objects.
Most builtin builtin types have the __lt__() method implemented by default, this  makes it possible to use the operator on  objects such as integers,  lists, tuples, dicts e.tc

ExampleEdit & Run
print(int(10).__lt__(5)) #Same as print(10 < 5)
print(float(5.0).__lt__(5.5)) #Same as print(1 < 5)
print([1, 2, 3].__lt__([5, 4, 3])) #Same as print([1, 2, 3] < [5, 4, 3])
print("abc".__lt__("xyz")) #same as print("abc" < "xyz")
copy
Output:
False True True True [Finished in 0.010422057006508112s]

The __lt__() method can also be used to compare objects of different but closely related types for example integers and floating point values. However, most of the types are not comparable and using the method with such objects will  result in a TypeError exception or a NotImplemented error. 

ExampleEdit & Run
#with closely related types
print(float(5.2).__lt__(7))

#With incompatible types
print([1, 2, 3].__lt__((1, 2, 3)))
print("1, 2, 3" < [1, 2, 3])
copy
Output:
True NotImplemented Traceback (most recent call last):   File "<string>", line 6, in <module> TypeError: '<' not supported between instances of 'str' and 'list' [Finished in 0.010705366265028715s]

Defining the __lt__() method for custom objects

We can define the __lt__() methods in our classes in order to establish the behavior of the operator "<" when used in comparison operations involving objects of the class. It defines what it means for an object of the class to be “less than” another object of the same class. This is an essential feature especially for ordering and sorting objects. 

We define the method in our classes using the following syntax:

Syntax:
class MyClass:
    def __lt__(self, other):
        #statements
copy
ExampleEdit & Run
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __lt__(self, other):
        if isinstance(other, Point):
             return (other.x, other.y) < (self.x, self.y)
        raise TypeError("Both objects should be instances of Point")

p1 = Point(3, 4)
p2 = Point(1, 2)
p3 = Point(3, 2)

print(p1 < p2)
print(p1 < p3)
print(p2 < p3)

#Raises an Error
print(p1 < (1, 2))
copy
Output:
True True False Traceback (most recent call last):   File "<string>", line 19, in <module>   File "<string>", line 8, in __lt__ TypeError: Both objects should be instances of Point [Finished in 0.00960647314786911s]