all Python 3Arithmetic & operatorsterm
Operator precedence
precedence
The order in which an expression is evaluated: ** > unary minus > * / // % > + -. Parentheses raise the precedence. Remember PEMDAS / BODMAS.
Syntax
( ) → ** → -x → * / // % → + -
The table does not stop at arithmetic: comparisons sit below it and not/and/or sit lower still, so a + b > c and d parses as ((a + b) > c) and d. Exponentiation binds tighter than a unary minus on its left but not on its right, which is why 2 ** -1 is legal and yields 0.5. There is no need to memorise the whole table — in a mixed expression parentheses are cheaper than debugging.
Examples
print(2 + 3 * 4) # → 14 (сначала умножение)
print((2 + 3) * 4) # → 20 (скобки первыми)
print(2 ** 3 ** 2) # → 512 (правоассоциативность: 3**2=9, 2**9)
print(-2 ** 2) # → -4 (сначала степень, потом минус)
print(10 - 3 + 2) # → 9 (слева направо)
print(10 % 3 + 1) # → 2 (% раньше +)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy