TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
< (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).
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.
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 anintand afloat) 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 aDecimalto aFractionis not supported and will raise aTypeError. - 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 theord()function. - Sequences (
list,tuple): Evaluates elements lexicographically, item by item, starting from index0. 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 expressiona < breturnsTrueonly if all elements ofaexist inb, andbcontains at least one element not present ina(i.e.,ais a subset ofb, buta != 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.
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., anint and a str), the < operator will not perform implicit type coercion.
Master Python with Deep Grasping Methodology!Learn More





