Chained Comparison Operators
·Jun 19, 2022·
1 min read
Lets understand how Chained Comparison Operator Works in Python
Considering 1 < 6 > 3 as an example and executing this in python.
print(1 < 6 > 3)
Here python breaks statement by adding Logical And Operator and then code looks like as follows.
print(1 < 6 and 6 > 3)
Final output is True. Which is 100% correct.
// log statement console.log(1 < 6 > 3) // output false
Here the output is false. Which is 200% wrong.
Lets understand how this was executed.
JS executes this from Left to Right and this becomes ( ( 1 < 6) > 3 ) In the first step (1 < 6) is executed and result will be true which can be considered as 1. In the second step we have ( 1 > 3 ) which is false. That is the reason we have final output as false.