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 working with numerical values in Python, we often find ourselves needing to check if a value falls within a certain range. Typically, we'd use the and operator to combine two separate comparison operations, like so:

x = 5
if x > 1 and x < 10:
    print("x is between 1 and 10")
Enter fullscreen mode Exit fullscreen mode

However, Python provides a more concise and readable way to achieve this using chained comparison operators. You can chain comparison operators together, just like you would in mathematical notation:


python
x = 5
if 1 < x < 10:
    print("x is between

---

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

---

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

Top comments (0)