Python Operator Precedence

Let’s look at operator precedence

  1. Notice

    Python groups operations using precedence. Parentheses make your intended grouping explicit.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3
print(number + 2 * 3)
print((number + 2) * 3)
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

Python groups operations using precedence. Parentheses make your intended grouping explicit.

Picture operator precedence

Python groups operations using precedence. Parentheses make your intended grouping explicit.

Image brief

Alt text
A diagram for Python Operator Precedence. Instructions: number = 3 print(number + 2 * 3) print((number + 2) * 3). Displayed result: 9 15. The final example bindings are number --> object 1 (int). object 1: 3
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Operator Precedence". Teaching focus: Python groups operations using precedence. Parentheses make your intended grouping explicit. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 3 print(number + 2 * 3) print((number + 2) * 3) | v DISPLAYED RESULT 9 15 FINAL BINDINGS number --> object 1 (int) object 1: 3 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Operator Precedence. Instructions: number = 3 print(number + 2 * 3) print((number + 2) * 3). Displayed result: 9 15. The final example bindings are number --> object 1 (int). object 1: 3

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3
print(number + 2 * 3)
print((number + 2) * 3)
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
9
15

What do you expect this program to print?

What does this example display?

number = 3
print(number + 2 * 3)
print((number + 2) * 3)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

9 15

Follow the printed values in order. The output is: 9 15

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

Python groups operations using precedence. Parentheses make your intended grouping explicit.

Python groups operations using precedence. Parentheses make your intended grouping explicit.

Operator Precedence

When Python evaluates an expression, the order in which operations occur can drastically affect the result. This is known as operator precedence. Just like in regular arithmetic, multiplication comes before addition—Python uses similar rules internally.

Why Operator Precedence Matters

Imagine the expression 3 + 4 * 2. Should Python add 3 and 4 first, or multiply 4 and 2? Without knowing the precedence rules, you'd be guessing.

Python follows a strict hierarchy of operators. Misunderstanding this hierarchy can lead to incorrect results, confusing bugs, or unexpected behavior.

Python Operator Precedence Table

Here’s the standard operator precedence in Python, from highest to lowest:

PrecedenceOperatorsDescription
1()Parentheses
2**Exponentiation
3+x, -x, ~xUnary plus, minus, bitwise NOT
4*, /, //, %Multiplication, division, floor division, modulus
5+, -Addition, subtraction
6<<, >>Bitwise shift
7&Bitwise AND
8^Bitwise XOR
9|Bitwise OR
10==, !=, >, <, >=, <=Comparisons
11notLogical NOT
12andLogical AND
13orLogical OR
14=, +=, -=, *=, /= ...Assignment operators

How Python Evaluates Expressions

Let’s see Python in action. Try predicting the output before running the code:

result = 10 + 5 * 2
print("Result:", result)
Result: 20

Explanation

Multiplication (*) has higher precedence than addition (+), so 5 * 2 is evaluated first, giving 10. Then 10 + 10 results in 20.

Using Parentheses to Control Evaluation

When in doubt, use parentheses to make the order explicit and readable:

result = (10 + 5) * 2
print("Result:", result)
Result: 30

Explanation

This time, 10 + 5 is evaluated first due to the parentheses, resulting in 15, which is then multiplied by 2.

Examples

Example 1: Logical vs Arithmetic

value = 3 + 2 > 4 and not False
print("Value:", value)
Value: True

Explanation

  • 3 + 2 → 5
  • 5 > 4 → True
  • not False → True
  • True and True → True

Example 2: Division and Exponentiation

result = 100 / 10 ** 2
print("Result:", result)
Result: 1.0

Explanation

10 ** 2 is evaluated first, giving 100. Then 100 / 100 gives 1.0.

Operator Associativity

When two operators have the same precedence, Python uses associativity to decide the order. Most operators are left-associative (evaluated left to right), except **, which is right-associative.

result = 2 ** 3 ** 2
print("Result:", result)
Result: 512

This is equivalent to 2 ** (3 ** 2) → 2 ** 9 → 512.

Things to Keep in Mind

  • Always use parentheses if the expression feels ambiguous.
  • For readability and debugging, simplify complex expressions step by step.
  • Be especially cautious when mixing comparison, logical, and arithmetic operators.

What to remember

Connect what you noticed in the example with the idea below.

Remember

Python groups operations using precedence. Parentheses make your intended grouping explicit.

Watch out for this idea

Python always evaluates arithmetic from left to right regardless of parentheses.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Conditional Statements →