Beginner   Chapter 28 · 20 min read · Module 2: Operators and Expressions

Operator Precedence

Learning objectives
  • After this chapter you can predict the result of an expression with mixed operators.
  • You can list the main precedence levels from highest to lowest.
  • You can explain left-to-right and right-to-left associativity.
  • You can use parentheses to control and clarify evaluation order.
  • You can explain how and, or and not are ordered.

When an expression contains more than one operator, Python must decide which one to apply first. In school mathematics you learned that multiplication happens before addition. Python follows the same idea and extends it to every operator in the language.

The rules that decide the order are called operator precedence. When two operators have the same precedence, another rule called associativity decides whether to work from left to right or from right to left.

In this chapter you will learn the common precedence levels, see how Python evaluates mixed expressions, and learn why parentheses are the best friend of readable code.

What is precedence?

Operator precedence: the set of rules that decide which operator is applied first in an expression that has several operators.

In 2 + 3 * 4, the multiplication has higher precedence than the addition. Python first calculates 3 * 4 to get 12 and then adds 2 to get 14. If you wanted to add first, you must write (2 + 3) * 4, which gives 20.

Example 1: Multiplication before addition
print(2 + 3 * 4)
print((2 + 3) * 4)
print(10 - 4 / 2)
print(7 + 8 // 3 * 2)
14 20 8.0 11

In the last line, // and * have the same precedence, so they are handled from left to right: 8 // 3 is 2, then 2 * 2 is 4, and finally 7 + 4 is 11.

Precedence from high to low

The table below lists the operators you know so far, starting with the highest precedence. Operators in the same row have equal precedence.

LevelOperatorsNotes
1( )Parentheses always win
2**Power, right to left
3+x, -x, ~xUnary plus, minus, bitwise not
4*, /, //, %Left to right
5+, -Left to right
6<<, >>Shifts
7&, ^, |Bitwise, in this order
8==, !=, <, >, <=, >=, is, inComparisons
9notBoolean not
10andBoolean and
11orBoolean or, lowest

Associativity

When two operators from the same level appear next to each other, associativity decides the order. Most operators are left-associative, which means they group from the left. So 100 / 10 / 2 means (100 / 10) / 2, which is 5.0, not 20.0.

The power operator is the main exception. It is right-associative, so 2 3 2 means 2 (3 2), which is 2 9, or 512.

Example 2: Associativity
print(100 / 10 / 2)
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)
print(20 - 5 - 3)
5.0 512 64 12

Unary minus and power

The unary minus is lower than on its right side. This surprises many people: -2 2 means -(2 2), which is -4. If you want to square negative two, write (-2) 2, which is 4.

Comparisons and boolean operators

Arithmetic happens before comparisons, and comparisons happen before not, and and or. Among the boolean operators, not goes first, then and, then or. So True or False and False is True or (False and False), which is True.

Example 3: Mixed operators
x = 5
print(x + 1 > 5 and x * 2 < 20)
print(not x > 3 or x == 5)
print(True or False and False)
print((True or False) and False)
True True True False

Python also allows chained comparisons such as 1 < x < 10. This is evaluated as 1 < x and x < 10, and x is read only once.

Use parentheses for clarity

You do not need to memorise the whole table. Parentheses always work, they cost nothing and they tell the next reader what you meant. Even when the default order is correct, (a * b) + c can be easier to read than a * b + c in a long formula. A good habit is to rely on precedence for simple arithmetic and add parentheses for anything mixed with comparisons, boolean logic or bitwise operators.

Assignment is not part of this order. In x = 2 + 3, the right side is fully evaluated first and only then stored in x.

Evaluating step by step

To read a complicated expression, work like Python does. First find the parentheses and solve the innermost ones. Then look for **, then unary signs, then multiplication-type operators, then addition-type operators, then comparisons, and last the boolean operators. When operators tie, move in the direction given by associativity. Writing each intermediate result on paper is a very good way to learn this.

Remember that precedence decides only how operators group, not always the order in which sub-expressions are evaluated. Python still evaluates operands from left to right. In f() + g() * h() the function f is called first, then g, then h, although the multiplication is performed before the addition.

Python syntax

Syntax
a + b * c
Syntax
(a + b) * c
Syntax
a ** b ** c
Syntax
not a or b and c

Real-world applications

  • Writing mathematical formulas such as averages and interest correctly.
  • Reading other people's code that mixes comparisons and boolean logic.
  • Avoiding hidden bugs in conditions of if statements.
  • Understanding why a result is different from what you expected.

Common mistakes

Mistake: Writing a + b / 2 to get the average of two numbers.
Fix: Division goes first. Write (a + b) / 2.
Mistake: Expecting -3 ** 2 to be 9.
Fix: It is -9 because binds tighter than unary minus. Write (-3) 2.
Mistake: Mixing and and or without parentheses.
Fix: and goes before or. Add parentheses to show your intention.
Mistake: Assuming 2 3 2 is 64.
Fix: Power is right-associative, so the answer is 512.

Important tips

  • When in doubt, add parentheses.
  • Break long formulas into several short lines with named variables.
  • Test small pieces of an expression in the interpreter.
  • Do not rely on memory for rare operators; look at the table.

Key points summary

  • Higher precedence operators are applied first.
  • Parentheses override every other rule.
  • Most operators are left-associative; ** is right-associative.
  • not goes before and, and and goes before or.
  • Comparisons come after arithmetic.

Exam and interview points

  • ** is right-associative; others are left-associative.
  • and has higher precedence than or.
  • Unary minus has lower precedence than ** on its right.
  • Comparison operators can be chained.

Quick revision

  • () > ** > unary > * / // % > + - > comparisons > not > and > or.
  • Same level: left to right, except **.
  • -2 ** 2 is -4.
  • Use parentheses to make code clear.

Frequently asked questions

Do I need to memorise the whole table?

No. Remember the basics: parentheses, power, multiplication, addition, comparisons, not, and, or. Use parentheses for everything else.

Why is -2 ** 2 equal to -4?

The power operator has higher precedence than the unary minus, so Python computes 2 ** 2 first and then negates the result.

Does precedence change the order of evaluation of function calls?

No. Operands are still evaluated from left to right. Precedence only decides how the operators group the operands.

Where does the assignment operator fit?

It is not an expression operator. The right side is evaluated completely before the value is stored in the name.

Practise this chapter

22 practice questions Take the chapter quiz

Related chapters