Skip to content
Tutorials Center
My learning

Topics

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

Python Which operator wins?

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.

Python Comparisons, not, and, or

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 < c means a < b and b < c, and b is evaluated once. The same applies to mixed chains such as a == b < c and x in y == z.
  • ** is special. -2 ** 2 is -4 because ** binds tighter than the minus on its left. On its right a unary operator is allowed: 2 ** -1 is 0.5. Chains run right to left: 2 ** 3 ** 2 is 2 ** 9.
  • Bitwise operators bind tighter than comparisons, unlike in some languages. x & 1 == 0 means (x & 1) == 0. Parenthesize anyway for readers who know C, where it is the other way round.
  • not is weaker than comparisons: not a == b means not (a == b). Write a != b when that is what you mean.
  • and binds tighter than or, so a or b and c is a or (b and c).
  • The conditional expression a if c else b is looser than or, so x or y if c else z is (x or y) if c else z.
  • Left-to-right evaluation order is not precedence. Operands are evaluated left to right (f() + g() calls f first) regardless of which operator runs first.
  • Use parentheses for clarity. They cost nothing at run time. Prefer (a and b) or c over relying on the table, and split long expressions into named steps.