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.
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
- Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
- Then *, /, // and %, left to right; then + and −, left to right.
- / always gives a float; // rounds down; % takes the sign of the divisor.
- 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.
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?
Reveal 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.
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
Evaluating the expression step by step
Evaluate 5 - 3 - 1 using the left-to-right rule.
- 1Step 1
Both subtraction operators have equal precedence. Start with the leftmost subtraction, 5 - 3, which produces 2.
- 2Step 2
Replace the completed part of the expression with its result. The remaining expression is 2 - 1.
- 3Step 3
Evaluate the remaining subtraction: 2 - 1 produces 1.
5 - 3 - 1 equals 1.
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.
| Grouping | First operation | Result |
|---|---|---|
| (5 - 3) - 1 | 5 - 3 | 1 |
| 5 - (3 - 1) | 3 - 1 | 3 |
Subtraction produces different results when its grouping changes.
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.
Controlling the grouping
Compare 10 - 5 + 2 with 10 - (5 + 2).
- 1Expression without parentheses
The operators have equal precedence, so evaluate from left to right: 10 - 5 produces 5, and then 5 + 2 produces 7.
- 2Expression with parentheses
The parentheses force 5 + 2 to be evaluated first, producing 7. Then evaluate 10 - 7, which produces 3.
10 - 5 + 2 equals 7, while 10 - (5 + 2) equals 3.
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 likeTreating the expression as 5 - (3 - 1) gives 3.
Why it failsThe subtraction operators have equal precedence, so the tie is resolved from left to right.
Do this insteadEvaluate 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 likeComparing (5 - 3) - 1 with 5 - (3 - 1) as if they must have the same result.
Why it failsSubtraction is not associative, so different groupings can produce different results.
Do this insteadUse the left-to-right rule unless parentheses explicitly request a different grouping.
Common mistake Ignoring parentheses when determining the first operation.
What it looks likeEvaluating 10 - (5 + 2) as though it were 10 - 5 + 2.Why it failsParentheses force their enclosed sub-expression to be evaluated first.
Do this insteadEvaluate 5 + 2 first, then subtract that result from 10.
Check Your Reasoning
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.
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
- 1When operators have equal precedence, evaluate them from left to right.
- 2The expression 5 - 3 - 1 equals 1 because 5 - 3 is evaluated before the remaining subtraction.
- 3Subtraction and division are not associative, so changing their grouping can change the result.
- 4Parentheses override the normal left-to-right grouping and force a chosen sub-expression to be evaluated first.
- 5Use 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.