Comparison Operator Priority in Python vs. C / C ++

In C / C ++, comparison operators such as < > take precedence over == . This code evaluates to true or 1 :

 if(3<4 == 2<3) { //3<4 == 2<3 will evaluate to true ... } 

But in Python, this seems wrong:

 3<4 == 2<3 #this will evaluate to False in Python. 

In Python, does each comparison operator have the same priority?

+5
source share
2 answers

In Python, comparison operators not only have the same priority, they are processed specially (they are more likely chains than groups). From the documentation :

Formally, if a, b, c, ..., y, z are expressions, and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z equivalent to a op1 b and b op2 c and ... and y opN z , except that each expression is evaluated no more than once.

In your case, the expression

 3<4 == 2<3 

equivalently

 3 < 4 and 4 == 2 and 2 < 3 

what is False because of the second sentence.

+5
source

Short answer: yes, all comparisons have the same priority

Long answer: you can see the documentation: Priority in Python

+3
source

Source: https://habr.com/ru/post/1390212/


All Articles