Python Reference ยท Numbers and Binary Data
Python operator precedence
Python operator precedence and associativity from highest to lowest: the full table, chained comparisons, ** and unary minus, not/and/or, and tips for parentheses.
Operator precedence decides which operation runs first when an expression mixes several operators. In 2 + 3 * 4 the multiplication binds tighter, so the result is 14, not 20. When two operators have the same precedence, associativity decides: most operators group from left to right, but ** groups from right to left.
You do not need to memorize the whole table. Learn the main layers (arithmetic, then comparisons, then not, and, or) and use parentheses whenever an expression might make a reader pause.
The table
From the tightest binding (evaluated first) to the loosest:
| Level | Operators | Notes |
|---|---|---|
| 1 | (...), [...], {...}, f(...), x[i], x.attr |
Grouping, literals, calls, subscripts, attribute access |
| 2 | await x |
Only inside async functions |
| 3 | ** |
Right-associative; binds tighter than a unary operator on its left |
| 4 | +x, -x, ~x |
Unary plus, minus, bitwise NOT |
| 5 | *, @, /, //, % |
Multiplication, matrix multiplication, division |
| 6 | +, - |
Addition and subtraction |
| 7 | <<, >> |
Shifts |
| 8 | & |
Bitwise AND |
| 9 | ^ |
Bitwise XOR |
| 10 | | |
Bitwise OR |
| 11 | in, not in, is, is not, <, <=, >, >=, !=, == |
Comparisons; they chain |
| 12 | not x |
Boolean NOT |
| 13 | and |
Boolean AND |
| 14 | or |
Boolean OR |
| 15 | x if c else y |
Conditional expression |
| 16 | lambda |
Lambda expression |
| 17 | := |
Assignment expression |
Assignment (=, +=, ...) is a statement, not an operator, and sits outside the table.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Usage notes
- Comparisons chain.
a < b < cmeansa < b and b < c, andbis evaluated once. The same applies to mixed chains such asa == b < candx in y == z. **is special.-2 ** 2is-4because**binds tighter than the minus on its left. On its right a unary operator is allowed:2 ** -1is0.5. Chains run right to left:2 ** 3 ** 2is2 ** 9.- Bitwise operators bind tighter than comparisons, unlike in some languages.
x & 1 == 0means(x & 1) == 0. Parenthesize anyway for readers who know C, where it is the other way round. notis weaker than comparisons:not a == bmeansnot (a == b). Writea != bwhen that is what you mean.andbinds tighter thanor, soa or b and cisa or (b and c).- The conditional expression
a if c else bis looser thanor, sox or y if c else zis(x or y) if c else z. - Left-to-right evaluation order is not precedence. Operands are evaluated left to right (
f() + g()callsffirst) regardless of which operator runs first. - Use parentheses for clarity. They cost nothing at run time. Prefer
(a and b) or cover relying on the table, and split long expressions into named steps.