DEV Community

qing
qing

Posted on

TIL: You can chain comparison operators in Python like math (2026)

TIL: You can chain comparison operators in Python like math

When writing conditional statements in Python, it's common to see verbose expressions like x > 1 and x < 10. However, Python provides a more concise and readable way to write these comparisons.

In mathematics, we're used to writing inequalities like 1 < x < 10. Python allows you to do the same, making your code more readable and intuitive. You can chain comparison operators together, like this: 1 < x < 10.

This expression is equivalent to the more verbose x > 1 and x < 10, but it's much easier to read and understand. The chained comparison is also more efficient, as it only evaluates `x


Follow me on Dev.to for daily Python tips and quick guides!


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*

Top comments (0)