Python Operator Precedence
Operations are prioritized from top to bottom, with higher priority operations at the top. Calculations proceed from left to right, so if an expression contains operators of the same precedence, the leftmost operator is executed first.
The exponentiation operator is an exception. When using two ** operators, the right one is executed before the left.
| Operators | Description |
|---|---|
() | Parentheses |
** | Exponentiation |
+x, -x, ~x | Unary plus, minus, and bitwise negation |
*, /, //, % | Multiplication, division, floor division, remainder |
+, - | Addition and subtraction |
<<, >> | Bitwise shifts |
& | Bitwise AND |
^ | Bitwise exclusive OR (XOR) |
| | Bitwise OR |
==, !=, >, >=, <, <=, is, is not, in, not in | Comparison, identity checks, membership tests |
not | Logical NOT |
and | Logical AND |
or | Logical OR |