Video summary
The video highlights a powerful yet often overlooked feature in Python known as comparison operator chaining, which allows multiple comparisons to be evaluated within a single statement without using logical operators like `and` or `or`. For instance, assigning values of 2 and 3 to variables x and y respectively enables the expression "1 < x < y < 4" to evaluate directly. This syntax mirrors intuitive mathematical notation by treating the chain as a series of binary comparisons: first checking if 1 is less than x, then verifying that x is less than y, and finally confirming that y is less than 4. If any individual comparison fails, such as changing the final operator to "greater than" where y equals 3 but needs to be greater than 4, the entire statement returns false immediately. The flexibility of this feature extends beyond simple inequality checks; users can mix various operators like less-than, greater-than, and their inclusive counterparts within an arbitrarily long chain. However, unlike some functional programming languages such as Scheme or Common Lisp where comparison chaining is a first-class citizen but restricted to using only the same operator type in one expression—preventing mixed operations—the Python approach permits combining different relational checks seamlessly. In contrast, languages like Perl 6 and Julia also support this syntax, while Lisps enforce strict uniformity across the chain of operators applied to lists or sequences. Despite its elegance and mathematical clarity, mainstream programming languages largely omit this feature due to historical implementation choices rather than technical impossibility. The transcript suggests that adding such functionality is actually quite easy to implement but has often been neglected because it was not considered a priority during initial language design phases. Critics argue against including it based on perceived low importance relative to other features and concerns about breaking backward compatibility if syntax were altered, though the speaker contends these reasons amount more to "laziness" than valid technical constraints. The discussion references a Software Engineering Stack Exchange thread that explores these philosophical and practical debates regarding why most major languages exclude this intuitive capability. Ultimately, the presenter champions comparison chaining as one of Python's best hidden features, ranking it alongside list comprehensions as essential tools for writing clean code. While other ecosystems like Lisp handle similar concepts with specific constraints to ensure consistency across operator types in a single expression, Python offers unique freedom by allowing mixed operators within chains while maintaining readability that aligns perfectly with human mathematical reasoning. The video concludes by encouraging viewers to explore further discussions on the topic and supports the channel through partnerships with ExpressVPN, emphasizing continuous learning as a core value for developers seeking to master such elegant language constructs.
Read the full video transcript
this is a hidden feature of python that
i recently came across
the chaining of comparison operators
that is not available in almost
any mainstream programming language i
think it's elegant and intuitive and
doesn't make any sense to me why it's
not available in most languages
so what is it say we assign the values 2
and 3 to x and y
and then look at a single statement that
includes several comparison operators
chained together
1 less than x less than y less than 4
in python this evaluates to true the way
python evaluates the statement is the
same way that we would
intuitively or mathematically look at
the statement
which is as a chain of binary comparison
operators
1 is less than x and x is less than y
and y is less than 4 which again
evaluates to true now you can use any
comparison operator
less than greater than less than or
equal to greater than or equal to and
mix and match them together in a single
arbitrarily long chain of comparison
operators
now if we change the original statement
to include a greater than operator is
the last comparison
then the entire statement returns false
because y which is equal to 3
is not greater than 4. and then finally
again we can flip the 4 and the y
to make the statement return true
because all the individual comparisons
are true 1 is less than x which is equal
to two
x is less than four and four is greater
than y
y being equal to three now this feature
is available in a few
other languages not many like pearl six
or reiku i think it's been her name too
not sure how to pronounce it
and julia and as shown here it's also
a first-class citizen in some functional
languages like scheme common lisp and
closure
with the added constraint that the
chaining of the operator includes only
the same operator so you can't mix and
match so shown here the greatest
language of all time which is lisp
the equals operator applied to a list of
numbers
three and three returns true three and
five returns false
all threes returns true all threes
except one of them being five returns
false
so again that's chaining the equality
comparison operator across the entire
list
and the same is true for the less than
operator applied to the entire list
below 3 less than 5 is true
and then a long sequence returning true
if it's in strictly increasing order
and false if it's not in strictly
increasing order
i put some links in the description one
of the more interesting ones is in the
software engineering stack exchange
which discusses from a
semi-philosophical perspective
why most mainstream languages do not
include this feature
you should check out some of the answers
on that page but to summarize some of
the discussion the reason to do it
is despite the initial intuition about
this feature being difficult to
implement
it's actually very easy to implement and
as i said it's a
mathematically intuitive and just
elegant statement which i think makes it
one of the
best hidden features at least to me of
python
in the discussion the reasons that come
up not to do it is
fundamentally just laziness in that its
importance versus other features is
quite low
and it doesn't seem to be the kind of
feature that pops up as an
intuitive first feature to implement
when the language is first born and
as with certain other features this can
potentially break backward compatibility
if this kind of chaining operators was
allowed previously
syntactically speaking meaning it was
allowed but it
didn't do the intuitive thing it can
certainly break in quite painful ways
backward compatibility
but still as i said in the previous
video list comprehensions i think is the
best feature python
and the chaining of comparison operators
i think
is the best hidden feature or not well
known feature
of python quick shout out to expressvpn
click their link in the description
it's the best way to support the podcast
i host in these videos that i make
if you enjoy these subscribe and
remember try to learn something new
every day
you