r/ProgrammerTIL • u/cdrini • Sep 12 '17
Python [Python] TIL that you can chain comparisons
For example:
x = 3
0 < x < 10 # -> TrueWikipedia: https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Comparison_operators
Python Docs: https://docs.python.org/3/reference/expressions.html#comparisons
Edit: formatting
69
Upvotes
View all comments
Show parent comment
2
u/sim642 Sep 12 '17
The only reason Python has comparison chaining is to cater to newbies who try to use a familiar syntax and expect it to work in Python, because Python also tries to be really intuitive etc. Notice how no other programing language has this syntax and there really hasn't been the need to introduce it there either. The only actual usage of the syntax is for math-like monotone comparison chains but the Python syntax is much more loose than that, swaying from actual intuitive, practical and unambiguous semantics.