Concepts / Operator Precedence and PEMDAS

Operator Precedence and PEMDAS

Left-to-right evaluation breaks a precedence tie. It does not mean that every operation in every expression is always performed from left to right; it applies here when the operators being compared have equal precedence.

  • 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…

The Tie in the Middle

An expression can contain several operators with equal precedence. When that happens, precedence alone does not decide which operator comes first. The tie is broken by evaluating from left to right. This rule is especially important for subtraction and division, because changing their grouping can change the result.

What do you think happens?

What result should you expect from 5 - 3 - 1?

  • 1

  • 3

Reveal answer
Answer:

1

The subtraction operators have equal precedence, so evaluation proceeds from left to right: first 5 - 3 produces 2, then 2 - 1 produces 1.

Reading Equal-Precedence Operators

Operator precedence describes which operations are considered first when an expression contains different operators. When two or more operators share the same precedence level, the left-to-right rule decides the order. The computer processes the operator that appears farther left before the equal-precedence operator to its right.

5 - 3 - 1two equal-precedenceoperators5 - 3leftmost subtraction2 - 1remaining expression1final resultevaluate leftmost operatorreplace 5 - 3 with 2evaluate remaining subtraction
What happens next when an expression contains two subtraction operators with the same precedence?
Key point

Left-to-right evaluation breaks a precedence tie. It does not mean that every operation in every expression is always performed from left to right; it applies here when the operators being compared have equal precedence.

Tracing 5 - 3 - 1

Worked example

Evaluating the expression step by step

Evaluate 5 - 3 - 1 using the left-to-right rule.

  1. 1
    Step 1

    Both subtraction operators have equal precedence. Start with the leftmost subtraction, 5 - 3, which produces 2.

  2. 2
    Step 2

    Replace the completed part of the expression with its result. The remaining expression is 2 - 1.

  3. 3
    Step 3

    Evaluate the remaining subtraction: 2 - 1 produces 1.

Result

5 - 3 - 1 equals 1.

5 - 3 - 1initial expression2 - 15 - 3 = 212 - 1 = 1evaluate 5 - 3evaluate 2 - 1
How does the value change after evaluating 5 - 3 and then subtracting 1, instead of evaluating 3 - 1 first?

Why Grouping Changes the Answer

Subtraction is not associative. That means changing the grouping can change the result. The left-to-right grouping of 5 - 3 - 1 is (5 - 3) - 1, which equals 1. A different grouping, 5 - (3 - 1), equals 3. These are different expressions because the parentheses tell the computer to evaluate a different sub-expression first.

GroupingFirst operationResult
(5 - 3) - 15 - 31
5 - (3 - 1)3 - 13

Subtraction produces different results when its grouping changes.

Key point

Addition and multiplication are associative in the source examples: (a + b) + c equals a + (b + c), and (a * b) * c equals a * (b * c). Subtraction and division are not associative, so their grouping requires particular care.

Parentheses as an Override

Parentheses explicitly group a sub-expression and force that sub-expression to be evaluated first, even when it appears later in the expression. Use them when the intended grouping differs from the normal left-to-right order or when you want the grouping to be unmistakable.

10 - 5 + 2left-to-right grouping10 - (5 + 2)parenthesized grouping710 - 5 = 5; 5 + 2 = 735 + 2 = 7; 10 - 7 = 3evaluate left to rightevaluate parentheses first
How does adding parentheses change the order of operations and the final result?
Worked example

Controlling the grouping

Compare 10 - 5 + 2 with 10 - (5 + 2).

  1. 1
    Expression without parentheses

    The operators have equal precedence, so evaluate from left to right: 10 - 5 produces 5, and then 5 + 2 produces 7.

  2. 2
    Expression with parentheses

    The parentheses force 5 + 2 to be evaluated first, producing 7. Then evaluate 10 - 7, which produces 3.

Result

10 - 5 + 2 equals 7, while 10 - (5 + 2) equals 3.

Best practice

Add parentheses when you need a grouping other than the default left-to-right grouping. This makes the intended evaluation order explicit and helps prevent calculation errors.

Mistakes with Equal Operators

  • Common mistake

    Evaluating the rightmost subtraction first in 5 - 3 - 1.

    What it looks like

    Treating the expression as 5 - (3 - 1) gives 3.

    Why it fails

    The subtraction operators have equal precedence, so the tie is resolved from left to right.

    Do this instead

    Evaluate 5 - 3 first, then evaluate the remaining expression 2 - 1.

  • Common mistake

    Assuming that equal-precedence operators can be grouped in either direction.

    What it looks like

    Comparing (5 - 3) - 1 with 5 - (3 - 1) as if they must have the same result.

    Why it fails

    Subtraction is not associative, so different groupings can produce different results.

    Do this instead

    Use the left-to-right rule unless parentheses explicitly request a different grouping.

  • Common mistake

    Ignoring parentheses when determining the first operation.

    What it looks like
    Evaluating 10 - (5 + 2) as though it were 10 - 5 + 2.
    Why it fails

    Parentheses force their enclosed sub-expression to be evaluated first.

    Do this instead

    Evaluate 5 + 2 first, then subtract that result from 10.

Check Your Reasoning

EASY

Evaluate 10 - 5 + 2 without parentheses. Then evaluate 10 - (5 + 2). State which operation is performed first in each expression and explain why the results differ.

Hints
  • For the expression without parentheses, look for equal-precedence operators and apply the left-to-right rule.
  • For the expression with parentheses, evaluate the enclosed sub-expression first.
EASY

Explain why 5 - 3 - 1 produces 1 rather than 3. Write the two intermediate expressions created during left-to-right evaluation.

Hints
  • Begin with the leftmost subtraction.
  • After evaluating 5 - 3, write the remaining expression before calculating the final result.

The Rule to Remember

  1. 1
    When operators have equal precedence, evaluate them from left to right.
  2. 2
    The expression 5 - 3 - 1 equals 1 because 5 - 3 is evaluated before the remaining subtraction.
  3. 3
    Subtraction and division are not associative, so changing their grouping can change the result.
  4. 4
    Parentheses override the normal left-to-right grouping and force a chosen sub-expression to be evaluated first.
  5. 5
    Use parentheses when you need to control or clarify the evaluation order.

Key Takeaways

  • Equal-precedence operators are evaluated from left to right.
  • 5 - 3 - 1 is evaluated as (5 - 3) - 1, producing 1.
  • Subtraction and division require careful grouping because they are not associative.
  • Parentheses force a sub-expression to be evaluated first and can change the final result.

More concepts starting with O