Why does `(not True == False)` work but not `(True == not False)`?



In Python, why would this work,
(not True == False)

but not this?
(True == not False)


The reason for this is mainly due to the order of operations in Python.

The not operator has a lower precedence than the comparison operators such as ==, !=, <, >, <= and >=.

As a result, in this expression, the == operator will be evaluated first, before the not is evaluated. Because of this, it will try to compare True == not first, which would not work and would cause an error.


# True == False is evaluated first.
# Then the result will be reversed with 'not'.
not True == False # True

# This would not work.
# It will try to evaluate 'True == not' first, which is not valid.
True == not False