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")
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*
Top comments (0)