Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The < (less than) operator is a strict relational operator that evaluates whether the left operand is strictly smaller in mathematical value, precedes in lexicographical order, or is a strict proper subset of the right operand. It evaluates the expression and returns a boolean value (True or False).
operand_a < operand_b

Underlying Implementation

When the < operator is invoked, Python internally delegates the operation to the __lt__() special (dunder) method of the left operand. However, Python’s data model enforces a specific method resolution order regarding operator overloading and subclasses. If the right operand is an instance of a strict subclass of the left operand’s type, Python prioritizes the subclass. It will call the right operand’s __gt__() (greater than) method before attempting to call the left operand’s __lt__() method.

# Standard method resolution for a < b
a.__lt__(b)


# Resolution if type(b) is a strict subclass of type(a)
b.__gt__(a)
If the initially called method does not support the type of the other operand, it returns the NotImplemented singleton. Python’s interpreter then falls back to calling the reflected method on the other operand (e.g., falling back to b.__gt__(a) if a.__lt__(b) returns NotImplemented). If both methods return NotImplemented, a TypeError is raised.

Evaluation Mechanics by Data Type

The behavior of the < operator is strictly defined by the data types of the operands:
  • Numeric Types (int, float, Decimal, Fraction): Evaluates the exact mathematical value. Python compares mixed numeric types (such as an int and a float) by evaluating their exact mathematical values to avoid precision loss. It does not coerce them to a common type (which could result in precision loss for very large integers). While most cross-type numeric comparisons are natively handled, comparing a Decimal to a Fraction is not supported and will raise a TypeError.
  • Strings (str): Performs a strict lexicographical comparison based on the Unicode code point values of the characters. Python compares the strings character by character from left to right using the integer values returned by the ord() function.
  • Sequences (list, tuple): Evaluates elements lexicographically, item by item, starting from index 0. The comparison terminates at the first unequal pair of elements. If all evaluated elements are equal but one sequence is shorter, the shorter sequence is evaluated as strictly less than the longer sequence.
  • Sets (set, frozenset): Evaluates as a strict proper subset test. The expression a < b returns True only if all elements of a exist in b, and b contains at least one element not present in a (i.e., a is a subset of b, but a != b).

Operator Chaining

Python allows the < operator to be chained with other relational operators. The interpreter evaluates chained comparisons using implicit logical AND operations with short-circuit evaluation semantics.

# Chained syntax
a < b < c


# Logically equivalent (but operationally different)
(a < b) and (b < c)
In the chained evaluation a < b < c, the middle operand b is evaluated exactly once. This is operationally distinct from the explicit and expression, which evaluates b twice if a < b is True. In both cases, if a < b evaluates to False, the b < c expression is never evaluated due to short-circuiting.

Type Compatibility

Unlike some dynamically typed languages, Python is strongly typed regarding comparisons. If the operands are of incompatible types (e.g., an int and a str), the < operator will not perform implicit type coercion.

# This will raise a TypeError: '<' not supported between instances of 'int' and 'str'
5 < "10"
Master Python with Deep Grasping Methodology!Learn More