Concepts / Debugging Expressions

Debugging Expressions

When both appear in an expression, the operation that appears farther left is performed first.

  • Programming

Why Evaluation Order Matters

When an expression contains several operations, Python must determine which calculation happens first. It follows the mathematical convention called PEMDAS: Parentheses, Exponentiation, Multiplication and Division, then Addition and Subtraction. The higher-priority level is handled before a lower-priority level, even when the lower-priority operator appears earlier in the expression.

Tracing PEMDAS Step by Step

PEMDAS gives the evaluation hierarchy from highest to lowest priority. Parentheses are handled first. Exponentiation comes next. Multiplication and division share the next level, so they are handled from left to right. Addition and subtraction share the final level and are also handled from left to right.

Parentheseshighest priorityExponentiationnext priorityMultiplication anddivisionleft to rightAddition andsubtractionleft to rightthenthenthen
What happens next when an expression contains parentheses, exponents, multiplication, division, addition, and subtraction together?
Worked example

A complete precedence trace

Evaluate 2 + 3 × (4 raised to the second power − 10) ÷ 7.

  1. 1
    1. Parentheses

    Work inside the parentheses. The exponent is evaluated before the subtraction: 4 raised to the second power becomes 16, and 16 − 10 becomes 6.

  2. 2
    2. Multiplication and division

    The expression is now 2 + 3 × 6 ÷ 7. Multiplication and division share a precedence level, so evaluate them from left to right: 3 × 6 becomes 18, then 18 ÷ 7 remains.

  3. 3
    3. Addition

    Addition is evaluated after the higher-precedence operations, so the remaining expression is 2 + 18 ÷ 7, with the division completed before the addition.

Result

The expression evaluates by completing the parenthesized calculation first, then the multiplication and division from left to right, and finally the addition.

Resolving Equal-Priority Operations

Multiplication and division are not separate priority levels. They have the same precedence. When both appear in an expression, the operation that appears farther left is performed first. Addition and subtraction follow the same rule: they share a precedence level and are evaluated from left to right.

24 ÷ 3 × 2same precedence level8 × 224 ÷ 3 first168 × 2leftmost operationremaining operation
When multiplication and division appear at the same precedence level, which operation happens first and how does the result change the remaining expression?
Worked example

The left-to-right tie-breaker

Evaluate 24 ÷ 3 × 2.

  1. 1
    Find the shared level

    Division and multiplication have the same precedence.

  2. 2
    Choose the first operation

    Division appears to the left of multiplication, so calculate 24 ÷ 3 first.

  3. 3
    Continue with the remainder

    The expression becomes 8 × 2.

Result

16

Controlling the Grouping

Parentheses override the default precedence rules. Any expression inside parentheses is evaluated before operations outside those parentheses. This lets you deliberately change the result and makes the intended grouping visible to other readers.

2 + 3 × 4multiplication first(2 + 3) × 4addition first142 + 12205 × 4evaluateevaluate
How does adding parentheses change the order in which parts of an expression are evaluated?
ExpressionFirst calculationEvaluation pathResult
2 + 3 × 43 × 42 + 1214
(2 + 3) × 42 + 35 × 420

Parentheses change the grouping when they place a lower-precedence operation inside them.

Best practice

Use parentheses when you need a different evaluation order and when they make your intent easier to read. They can improve clarity even when they do not change the result. For example, (minute × 100) ÷ 60 is clearer than minute × 100 ÷ 60, even though both expressions evaluate in the same order.

Misconceptions to Correct

  • Common mistake

    Evaluating every expression strictly from left to right

    What it looks like

    In 2 + 3 × 4, calculating 2 + 3 before the multiplication ignores the higher precedence of multiplication.

    Why it fails

    Precedence determines which level is handled first. Left-to-right order is only used to break ties within one precedence level.

    Do this instead

    Identify parentheses, exponentiation, multiplication or division, and addition or subtraction in that order.

  • Common mistake

    Treating multiplication as higher priority than division

    What it looks like

    In 24 ÷ 3 × 2, choosing multiplication first because the letter M appears before D in PEMDAS.

    Why it fails

    Multiplication and division share the same precedence.

    Do this instead

    Evaluate whichever of the two appears first from left to right.

  • Common mistake

    Treating addition as higher priority than subtraction

    What it looks like

    Assuming addition must be completed before subtraction whenever both appear.

    Why it fails

    Addition and subtraction share the same lower precedence level.

    Do this instead

    Evaluate addition and subtraction from left to right.

  • Common mistake

    Avoiding parentheses because the default order is enough

    What it looks like

    Writing a longer expression without grouping even when the intended calculation is not obvious.

    Why it fails

    Parentheses can override precedence and improve readability.

    Do this instead

    Add parentheses when they communicate the intended grouping or make the expression easier to understand.

Practice the Trace

EASY

Determine the evaluation order for 6 + 2 × (3 + 1). Do not calculate everything at once. Write the first operation, simplify the expression, and then identify the next operation.

Hints
  • Start inside the parentheses.
  • After the parentheses are simplified, multiplication has priority over addition.
  • Use left-to-right order only when two remaining operations share a precedence level.

What do you think happens?

Which operation should be performed first in 6 + 2 × (3 + 1)?

  • 6 + 2

  • 2 × 3

  • 3 + 1

  • The expression must be evaluated entirely from left to right

Reveal answer
Answer:

3 + 1

The operation is inside parentheses, and parentheses have the highest precedence. After that group is simplified, the remaining multiplication is handled before the addition.

Key Takeaways

  1. 1
    PEMDAS orders evaluation as Parentheses, Exponentiation, Multiplication and Division, then Addition and Subtraction.
  2. 2
    Multiplication and division share a precedence level and are evaluated from left to right.
  3. 3
    Addition and subtraction also share a precedence level and are evaluated from left to right.
  4. 4
    Precedence comes before left-to-right order; left-to-right is the tie-breaker for equal-precedence operators.
  5. 5
    Parentheses can override the default order and make the intended grouping clearer.

At a glance

  • Use PEMDAS to identify the highest-priority operation before calculating.
  • Treat multiplication and division as equal in precedence, resolving them from left to right.
  • Treat addition and subtraction as equal in precedence, also resolving them from left to right.
  • Use parentheses to change evaluation order or make the intended calculation easier to read.
  • Do not confuse the precedence hierarchy with a rule that always evaluates every operation from left to right.

Related concepts

More concepts starting with D