Why is 'True == not False' a syntax error in Python?
It has to do with operator precedence in Python (the interpreter thinks you're comparing True to not, since ==
has a higher precedence than not
). You need some parentheses to clarify the order of operations:
True == (not False)
In general, you can't use not
on the right side of a comparison without parentheses. However, I can't think of a situation in which you'd ever need to use a not
on the right side of a comparison.
It's just a matter of operator precedence. Try:
>>> True == (not False)
True
Have a look in this table of operator precedences, you'll find that ==
binds tigher than not
, and thus True == not False
is parsed as (True == not) False
which is clearly an error.