Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator precedence: not and comparisons

Tags:

python

I'm trying to understand python better and the lack of parentheses can be a bit confusing for some reason.

how is (not a < b < c) evaluated? Is it (not a) < b < c? or not (a < b < c)?

According to the reference manual, does not have a lower or higher precedence than < ? I'm assuming operators with higher precedence evaluate before those with lower, right? I feel like I need someone to break out the sock puppets right now.

According to the Python 2.4 reference manual not and comparisons have a different precedence. Then in the Python 2.7 reference manual not and comparisons have the same precedence.

If i'm not mistaken not a < b < c will have varying results depending on the version of python. Would someone please share how this statement is evaluated?

I'm sticking with not (a < b < c)

like image 697
Jake Avatar asked Oct 11 '11 04:10

Jake


1 Answers

What you're seeing in the 2.7 manual is all relational operators, including not in and is not, at the same precedence; boolean not is still one level lower in precedence and as such the relational comparison happens first.

like image 158
Ignacio Vazquez-Abrams Avatar answered Sep 29 '22 05:09

Ignacio Vazquez-Abrams