Concepts / Parentheses and Grouping in Expressions

Parentheses and Grouping in Expressions

For this expression, the valid grouping is (5 - 3) - 1, because the leftmost operation is performed first.

  • 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 Ambiguity Trap

An expression such as 5 - 3 - 1 can make people hesitate. Should the computer subtract 3 from 5 first, or should it group the final subtraction in some other way? The answer is determined by a precise rule: when operators have equal precedence, they are evaluated from left to right. This rule prevents a chain of operators from becoming ambiguous.

What do you think happens?

What is the result of 5 - 3 - 1?

  • 1

  • 3

  • The expression is ambiguous

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

Precedence determines which operations receive attention first. When two or more operators in an expression have the same precedence level, left-to-right evaluation breaks the tie. The computer processes the equal-precedence operators in the order they appear, beginning with the leftmost operator.

In 5 - 3 - 1, both operators are subtraction operators, so they have equal precedence. The first subtraction is the one between 5 and 3. Once that operation is completed, the remaining expression is 2 - 1. The second subtraction is then evaluated.

5left value-subtraction; equalprecedence3middle value-subtraction; equalprecedence1right value
Which operators in 5 - 3 - 1 share the same precedence and determine the evaluation order?

Tracing the Leftmost Operation

Worked example

Evaluating 5 - 3 - 1

Determine the result of 5 - 3 - 1 using the equal-precedence rule.

  1. 1
    First operation

    Both subtraction operators have equal precedence. The leftmost subtraction is evaluated first: 5 - 3 = 2.

  2. 2
    Rewrite the expression

    Replace the completed operation with its result. The expression becomes 2 - 1.

  3. 3
    Second operation

    Evaluate the remaining subtraction: 2 - 1 = 1.

Result

5 - 3 - 1 equals 1.

5 - 322 - 1remaining expression1final resultthenthen
What happens first, second, and third when evaluating 5 - 3 - 1?

Grouping Changes the Result

Left-to-right evaluation is especially important for subtraction and division because these operators are not associative. For a non-associative operator, changing the grouping can change the result. The grouping (5 - 3) - 1 produces 1, while the grouping 5 - (3 - 1) produces 3.

Parentheses make the intended grouping explicit. A parenthesized sub-expression is evaluated first, regardless of where that sub-expression appears in the larger expression. Therefore, parentheses can override the default left-to-right order.

5 - 3 - 1left-to-right grouping5 - (3 - 1)parentheses grouping(5 - 3) - 1result: 13resultgroups asevaluates to
How does adding parentheses change the grouping and final result of 5 - 3 - 1?
GroupingFirst operationFinal result
(5 - 3) - 15 - 3 = 22 - 1 = 1
5 - (3 - 1)3 - 1 = 25 - 2 = 3

The same numbers and subtraction operators produce different results when their grouping changes.

(5 - 3) - 1first: 5 - 35 - (3 - 1)first: 3 - 11final result3final resultevaluates toevaluates to
What is the difference between grouping (5 - 3) - 1 and 5 - (3 - 1), and why do they produce different results?

Parentheses as Explicit Instructions

Worked example

Changing 10 - 5 + 2

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

  1. 1
    Without parentheses

    The subtraction and addition operators have equal precedence, so evaluate from left to right: 10 - 5 = 5, followed by 5 + 2 = 7.

  2. 2
    With parentheses

    The parentheses force 5 + 2 to be evaluated first. That gives 7, followed by 10 - 7 = 3.

Result

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

Best practice

Use parentheses when you need a grouping different from the default left-to-right order. They do more than change a result: they make the intended order visible to anyone reading the expression. This is particularly important when subtraction or division is involved, because regrouping these operations can change the result.

Common Evaluation Mistakes

  • Common mistake

    Reading equal-precedence operators from right to left

    What it looks like

    Treating 5 - 3 - 1 as though the grouping were 5 - (3 - 1)

    Why it fails

    Equal-precedence operators are evaluated from left to right. The right-hand subtraction is not performed first unless parentheses explicitly place it there.

    Do this instead

    Use the grouping (5 - 3) - 1 and compute 5 - 3 before subtracting 1.

  • Common mistake

    Assuming that subtraction can be regrouped without changing the answer

    What it looks like

    Assuming (5 - 3) - 1 and 5 - (3 - 1) must produce the same result

    Why it fails

    Subtraction is not associative, so its grouping affects the result.

    Do this instead

    Check the parentheses and evaluate the explicitly grouped sub-expression first.

  • Common mistake

    Ignoring parentheses when tracing an expression

    What it looks like

    Evaluating 10 - (5 + 2) from left to right as if it were 10 - 5 + 2

    Why it fails

    Parentheses override the default left-to-right evaluation order.

    Do this instead

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

Check Your Reasoning

EASY

Compare the two expressions 10 - 5 + 2 and 10 - (5 + 2). Determine the result of each one, identify which operation happens first in each expression, and explain why the results differ.

Hints
  • In the expression without parentheses, the operators have equal precedence.
  • In the expression with parentheses, evaluate the parenthesized sub-expression first.
  • Write the expression again after completing the first operation.

The Evaluation Rule

  1. 1
    Operators with equal precedence are evaluated from left to right.
  2. 2
    The expression 5 - 3 - 1 is grouped as (5 - 3) - 1 and equals 1.
  3. 3
    Subtraction and division are not associative, so changing their grouping can change the result.
  4. 4
    Parentheses force a sub-expression to be evaluated first and override the default left-to-right order.
  5. 5
    Tracing an expression one operation at a time makes its grouping and final result clear.

Key Takeaways

  • Equal-precedence operators are evaluated from left to right.
  • 5 - 3 - 1 equals 1 because 5 - 3 is evaluated before the remaining subtraction.
  • Subtraction and division require careful grouping because they are not associative.
  • Parentheses explicitly control which sub-expression is evaluated first.
  • Writing each intermediate expression helps prevent grouping mistakes.

Related concepts

More concepts starting with P