Concepts / Understanding Operator Precedence

Understanding Operator Precedence

A useful first question is: which operators have the highest precedence in this expression? Evaluate those operations before moving to the lower-precedence operations.

  • Programming
Interactive lab

Try it: Order of Operations

How Python decides which operator in an expression runs first — precedence, left-to-right order and right-to-left **, plus what /, // and % really return.

How it works

  1. Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
  2. Then *, /, // and %, left to right; then + and −, left to right.
  3. / always gives a float; // rounds down; % takes the sign of the divisor.
  4. Each step replaces one operation with its value until one number is left.

Default run (7 steps): Expression: 2 + 3 * (7 - 4) ** 2 // 5. Python evaluates left to right, but higher-precedence operators bind first. … Result: 7 (an int).

Simplified: Numbers (at most 6 digits) and arithmetic operators only; parsed and evaluated by the lab's own evaluator (no eval), checked against real Python results.

Educational simulation

Loading the simulation…

Why Order Matters

When an expression contains several operators, Python must decide which operation to perform first. It does not simply evaluate every expression from left to right. Instead, it follows operator precedence: a set of rules that determines the order in which operations are performed.

In the real world

Consider the expression 2 + 3 * 4. A strict left-to-right reading would add 2 and 3 first, producing 5, and then multiply by 4, producing 20. Python instead follows the familiar mathematical convention that multiplication comes before addition. It evaluates 3 * 4 first, producing 12, and then adds 2, producing 14.

Multiplicationhigher precedenceAdditionlower precedenceDivisionhigher precedenceSubtractionlower precedencebeforebefore
What operation does Python perform first, second, and last when an expression contains operators with different precedence levels?

Reading the Precedence Hierarchy

Python organizes operators into a precedence hierarchy. Operators with higher precedence bind more strongly and are evaluated before operators with lower precedence. Multiplication and division have higher precedence than addition and subtraction, so they are handled first when these operators appear together.

Definition

Operator precedence is the set of rules that determines which operations Python performs first when multiple operators appear in the same expression.

Key point

A useful first question is: which operators have the highest precedence in this expression? Evaluate those operations before moving to the lower-precedence operations.

Tracing a Mixed Expression

Worked example

Evaluating 10 - 2 * 3 + 8 / 4

Determine the result by applying precedence rather than evaluating strictly from left to right.

  1. 1
    Identify the operators

    The expression contains subtraction, multiplication, addition, and division.

  2. 2
    Find the higher-precedence operations

    Multiplication and division have higher precedence than addition and subtraction, so evaluate 2 * 3 and 8 / 4 first.

  3. 3
    Evaluate the higher-precedence operations

    The multiplication produces 6, and the division produces 2.

  4. 4
    Continue with the remaining expression

    The expression is now 10 - 6 + 2. Subtraction and addition have equal precedence, so Python evaluates them from left to right: 10 - 6 produces 4, and then 4 + 2 produces 6.

Result

The result is 6.

10 - 2 * 3 + 8 / 4original expression10 - 6 + 2multiplication and divisionfirst4 + 210 - 66final resultevaluate higher precedenceleft to rightadd
How does the mixed-operator expression break into intermediate steps as Python evaluates it?

What do you think happens?

Predict the result of 8 + 2 * 5 before checking the explanation.

  • 50

  • 18

  • 40

Reveal answer
Answer:

18

Multiplication has higher precedence than addition, so 2 * 5 is evaluated first, producing 10. Then 8 is added, producing 18.

Equal-Precedence Operators

Precedence alone does not decide everything. If the operators that remain have equal precedence, Python evaluates them from left to right. In the source expression 10 - 6 + 2, subtraction and addition have equal precedence, so Python performs 10 - 6 first and then adds 2 to the result.

Worked example

Left-to-right evaluation at one precedence level

Determine the result of 18 - 5 + 3.

  1. 1
    Compare the operators

    Subtraction and addition have equal precedence.

  2. 2
    Apply the direction rule

    Because the operators have equal precedence, evaluate from left to right.

  3. 3
    Calculate

    First calculate 18 - 5, producing 13. Then calculate 13 + 3.

Result

The result is 16.

Key point

The complete reasoning pattern is: first group operations by precedence, then evaluate equal-precedence operations from left to right.

Parentheses as an Override

Parentheses override the default precedence rules. They explicitly show which operation should happen first, even when that operation would normally have lower precedence. Parentheses therefore affect both the calculation and the readability of an expression.

Before
2 + 3 * 4

Multiplication happens before addition, producing 14.

After
(2 + 3) * 4

The parentheses make addition happen first, producing 20.

2 + 3 * 4default grouping(2 + 3) * 4explicit grouping14multiplication first20addition firstevaluateevaluate
How does adding parentheses change the order of operations and the final result?
Best practice

Use parentheses when the intended grouping might not be immediately obvious. Even when you understand the default precedence, explicit grouping can make the expression easier for another reader to interpret and maintain.

Mistakes in Evaluation

  • Common mistake

    Evaluating every expression strictly from left to right

    What it looks like
    Reading 2 + 3 * 4 as (2 + 3) * 4
    Why it fails

    Multiplication has higher precedence than addition.

    Do this instead

    Evaluate 3 * 4 first, then add 2.

  • Common mistake

    Stopping after applying precedence and forgetting the left-to-right rule

    What it looks like
    Treating 10 - 6 + 2 as 10 - (6 + 2)
    Why it fails

    Subtraction and addition have equal precedence, so they are evaluated from left to right.

    Do this instead

    Calculate 10 - 6 first, then add 2.

  • Common mistake

    Assuming parentheses are only for changing a result

    What it looks like

    Leaving a complicated expression ungrouped even when its intent is important

    Why it fails

    Parentheses also make the intended operation order explicit and improve readability.

    Do this instead

    Add parentheses when they clarify which operations belong together.

Practice the Trace

MEDIUM

Without running code, determine the result of 12 + 4 * 2 - 3. Write the intermediate expression after evaluating multiplication, then apply the remaining addition and subtraction from left to right.

Hints
  • Multiplication has higher precedence than addition and subtraction.
  • After multiplication, addition and subtraction have equal precedence.
  • Evaluate the remaining addition and subtraction from left to right.
Worked example

Practice solution

Evaluate 12 + 4 * 2 - 3.

  1. 1
    Evaluate multiplication

    4 * 2 produces 8, so the expression becomes 12 + 8 - 3.

  2. 2
    Evaluate equal-precedence operators

    Addition and subtraction have equal precedence, so move from left to right: 12 + 8 produces 20, and 20 - 3 produces 17.

Result

The result is 17.

Working with Intent

To predict an expression's result, do not begin by scanning blindly from left to right. Identify the operators with higher precedence, evaluate them first, and then handle equal-precedence operators from left to right. When the intended grouping should be especially clear, use parentheses to override the default order or to communicate that order directly to readers.

  • Operator precedence determines which operations Python performs first.
  • Multiplication and division are evaluated before addition and subtraction.
  • Operators with equal precedence are evaluated from left to right.
  • Parentheses override default precedence and make grouping explicit.
  • Clear grouping improves the readability and maintainability of expressions.

Key Takeaways

  • Python uses precedence rules instead of evaluating every mixed expression strictly from left to right.
  • Higher-precedence operations such as multiplication and division are performed before lower-precedence addition and subtraction.
  • When operators have equal precedence, Python evaluates them from left to right.
  • Parentheses override the default order and make intended grouping visible.
  • Tracing an expression step by step helps you predict results and avoid evaluation mistakes.

More concepts starting with U