Skip to content

Python Operator Precedence

Updated 5 min read Aimerscrew

What does 2 + 3 * 4 give in Python: 20 or 14? The answer is 14, and the reason is Python operator precedence, the rules that decide which part of an expression runs first. In this lesson, you'll see the complete precedence table, learn a few surprising cases like -2 ** 2, and find out why good programmers use parentheses even when they don't strictly need them.

🌍 Real-life example

Following a recipe, you can't frost a cake before you bake it, even if "frost" appears earlier in the sentence. Some steps simply come first. Math works the same way: you may have learned PEMDAS or BODMAS at school (Parentheses/Brackets, Exponents/Orders, Multiplication and Division, Addition and Subtraction). Python follows the same idea, with a few extra levels for its other operators.

What is operator precedence in Python?

Precedence is the priority of an operator. When an expression contains several operators, Python runs the ones with higher precedence first. Multiplication has higher precedence than addition, so in 2 + 3 * 4, Python multiplies first:

print(2 + 3 * 4)
print((2 + 3) * 4)
14
20

Parentheses have the highest priority of all, so they let you choose the order yourself.

Python operator precedence table

Here are the operators you've learned in this chapter, from highest priority (runs first) to lowest (runs last):

PriorityOperatorDescription
1 (highest)( )Parentheses
2**Exponent
3+x, -xUnary plus and minus (a sign in front of one value)
4*, /, //, %Multiplication, division, floor division, modulo
5+, -Addition and subtraction
6==, !=, >, <, >=, <=, is, is not, in, not inComparison, identity and membership
7notLogical NOT
8andLogical AND
9orLogical OR
10 (lowest):=Walrus (assignment expression)

Python also has bitwise operators (like &, | and ^) that sit between arithmetic and comparisons, but you won't need them as a beginner. The plain assignment operators (=, += and friends) aren't in the table because they're statements, not operators inside an expression: they always happen last.

💡 Tip

A handy memory aid: math, then compare, then logic. Python finishes all the arithmetic, then does the comparisons, then combines the results with not, and, or.

How Python evaluates an expression step by step

Let's watch Python work through 10 + 2 * 3 ** 2 - 4. At each step, it finds the operator with the highest precedence and replaces that part with its result:

Animation: 10 + 2 * 3 ** 2 - 4 is evaluated step by step. First 3 ** 2 becomes 9, then 2 * 9 becomes 18, then 10 + 18 becomes 28, and finally 28 - 4 gives 24
Exponent first, then multiplication, then addition and subtraction from left to right.
print(10 + 2 * 3 ** 2 - 4)
24

What happens when operators have the same precedence?

When operators share a level, Python uses associativity, the direction it reads them in. Almost every operator is left-associative: it runs from left to right.

print(100 / 10 / 2)    # (100 / 10) / 2
print(20 - 5 - 3)      # (20 - 5) - 3
print(12 // 5 * 5)     # (12 // 5) * 5
5.0
12
10

Why is ** right-associative?

The exponent operator is the exception: it runs from right to left, just like stacked exponents in math.

print(2 ** 3 ** 2)      # 2 ** (3 ** 2) = 2 ** 9
print((2 ** 3) ** 2)    # 8 ** 2
512
64

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

This one surprises almost everyone. ** has higher precedence than the minus sign in front of a number, so Python squares first and applies the minus afterwards:

print(-2 ** 2)      # -(2 ** 2)
print((-2) ** 2)

temperature = -3
print(temperature ** 2)
-4
4
9

Notice the last example: when the negative number is stored in a variable, the minus sign is part of the value, so squaring gives 9 as you'd expect. The trap only appears when you type the minus sign directly in front of the number.

In what order do not, and and or run?

Among the logical operators, not runs first, then and, then or. This can change the meaning of a condition completely:

is_vip = True
has_ticket = False
door_open = False

print(is_vip or has_ticket and door_open)     # is_vip or (has_ticket and door_open)
print((is_vip or has_ticket) and door_open)
True
False

Same three values, different answers. In the first line, and runs first, so a VIP gets in even though the door is closed. Also remember that comparisons run before logic, so age >= 18 and age < 65 needs no extra brackets.

print(not 5 > 3)       # not (5 > 3)
print(1 + 1 == 2 and 3 > 2)
False
True

Real-world example: price with discount and tax

A jacket costs $80. It's 25% off, and then 8% sales tax is added. Here's a version without enough parentheses, and the fixed version:

price = 80
discount = 0.25
tax = 0.08

wrong = price - price * discount * 1 + tax
right = (price - price * discount) * (1 + tax)

print("Wrong:", wrong)
print("Right:", round(right, 2))
Wrong: 60.08
Right: 64.8

The wrong version is just the right formula with the parentheses removed. Multiplication runs first and then + tax just adds 8 cents to the discounted price. The parentheses make Python apply the discount first and multiply the whole discounted price by 1.08.

When should you use parentheses?

Use them whenever a reader might have to stop and think. Parentheses never slow your program down, and they make your intent obvious:

hours = 3
minutes = 45

total_minutes = hours * 60 + minutes          # correct, but you must know the rules
total_minutes = (hours * 60) + minutes        # same result, clearer
print(total_minutes)
225

📝 Note

Professional programmers don't memorize every row of the precedence table. They remember the basics (math before comparisons before logic, ** before * before +) and add parentheses for everything else.

Common mistakes with operator precedence

1. Calculating an average without parentheses

score1 = 80
score2 = 90
print(score1 + score2 / 2)
print((score1 + score2) / 2)
125.0
85.0

Without parentheses, only score2 is divided by 2.

2. Expecting -2 ** 2 to be 4

Write (-2) ** 2 when you want to square a negative number.

3. Mixing and and or without parentheses

a or b and c means a or (b and c). If you meant (a or b) and c, say so with parentheses.

4. Forgetting that ** reads right to left

2 ** 3 ** 2 is 512, not 64.

Practice exercise

🏋️ Try it yourself

Without running it, predict what each line prints. Then check your answers in Python.

print(5 + 4 * 2)
print(20 / 4 * 2)
print(3 ** 2 ** 2)
print(-3 ** 2)
print(not 4 > 2 or 1 == 1)
Show solution
print(5 + 4 * 2)            # 4 * 2 first, then + 5
print(20 / 4 * 2)           # left to right: (20 / 4) * 2
print(3 ** 2 ** 2)          # right to left: 3 ** 4
print(-3 ** 2)              # -(3 ** 2)
print(not 4 > 2 or 1 == 1)  # (not True) or True
13
10.0
81
-9
True

What's next?

Congratulations, you've finished the Operators chapter! You can now calculate, update, compare and combine values in Python. Next, dive into one of the most used data types in any program, text, in Python strings.

Key takeaways

  • Operator precedence decides which operator Python runs first in an expression with several operators.
  • The order is: parentheses, **, unary minus, * / // %, + -, comparisons, not, and, or.
  • Operators on the same level run left to right, except **, which runs right to left: 2 ** 3 ** 2 is 512.
  • -2 ** 2 is -4 because ** runs before the minus sign; write (-2) ** 2 to get 4.
  • not runs before and, which runs before or, so a or b and c means a or (b and c).
  • Use parentheses whenever the order is not obvious; they make code clearer and never slow it down.

Frequently asked questions

What is the order of operations in Python?

Python follows the same idea as PEMDAS or BODMAS: parentheses first, then exponents (**), then multiplication and division (*, /, //, %), then addition and subtraction. After the math come comparisons, then not, and, and or.

Why does -2 ** 2 give -4 in Python?

The ** operator has higher precedence than the unary minus, so Python calculates 2 ** 2 first and then makes it negative. Use (-2) ** 2 to get 4.

Is ** left or right associative in Python?

It is right-associative, so 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2), which is 512. Almost all other operators are left-associative.

Which has higher precedence in Python, and or or?

and has higher precedence than or, and not is higher than both. So not a or b and c is evaluated as (not a) or (b and c).

Do parentheses slow down Python code?

No. Parentheses only change the order of evaluation, and extra ones cost nothing at run time. Use them freely to make expressions easier to read.