Mathematical Operators in Python
When Python evaluates a mathematical expression, it does not simply perform every operation from left to right.
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…
Why Evaluation Order Matters
When Python evaluates a mathematical expression, it does not simply perform every operation from left to right. It follows an order of operations so that an expression has one consistent meaning. The key rule is to check precedence first: higher-priority operations are evaluated before lower-priority operations. Left-to-right order matters when the operations being considered share the same precedence level.
What do you think happens?
What result should you expect from the expression 2 + 3 * 4?
Reveal answer
14
Multiplication has higher precedence than addition, so 3 * 4 is evaluated first. The expression then becomes 2 + 12, which produces 14.
The PEMDAS Hierarchy
PEMDAS gives the precedence order from highest to lowest: Parentheses, Exponentiation, Multiplication and Division, then Addition and Subtraction. An operation at a higher level is evaluated before an operation at a lower level, even when the lower-level operation appears earlier in the expression.
| Precedence level | Operations | Tie-breaking direction |
|---|---|---|
| 1 | Parentheses | The expression inside is evaluated first |
| 2 | Exponentiation | Evaluated before multiplication, division, addition, or subtraction |
| 3 | Multiplication and division | Left to right |
| 4 | Addition and subtraction | Left to right |
PEMDAS lists the priority levels. Left-to-right evaluation is used when operations share a level.
Resolving Equal-Priority Operations
Multiplication and division share one precedence level. Python evaluates them from left to right, choosing whichever of those operations appears first. Addition and subtraction follow the same rule at their own level. Left-to-right is therefore a tie-breaker, not the main rule for the entire expression.
Multiplication and division together
Evaluate 24 / 6 * 2.
- 1Identify the precedence level
Division and multiplication have the same precedence, so neither automatically jumps ahead of the other.
- 2Choose the leftmost operation
Division appears before multiplication, so evaluate 24 / 6 first. The intermediate value is 4.
- 3Continue with the remaining operation
The expression is now 4 * 2, which evaluates to 8.
24 / 6 * 2 evaluates to 8.
Tracing a Complete Expression
To evaluate a longer expression, repeatedly find the highest-precedence operation that remains. Resolve parentheses first, then exponentiation, then multiplication or division from left to right, and finally addition or subtraction from left to right. Each resolved operation changes the expression into a simpler one.
Applying several precedence levels
Evaluate 5 + 2 ** 2 * 3.
- 1Evaluate exponentiation
Exponentiation has higher precedence than multiplication and addition. Evaluate 2 ** 2 to get 4.
- 2Rewrite the expression
The expression becomes 5 + 4 * 3.
- 3Evaluate multiplication
Multiplication has higher precedence than addition. Evaluate 4 * 3 to get 12.
- 4Evaluate addition
The expression becomes 5 + 12, which evaluates to 17.
5 + 2 ** 2 * 3 evaluates to 17.
Parentheses as Control and Clarity
Parentheses have the highest precedence and cause the expression inside them to be evaluated first. They can change the result by grouping operations differently. They can also make the intended grouping obvious even when the result would be unchanged without them.
Changing the grouping
Compare 2 + 3 * 4 with (2 + 3) * 4.
- 1Evaluate the expression without added parentheses
Multiplication takes precedence, so 3 * 4 is evaluated first. The result is 2 + 12, or 14.
- 2Evaluate the parenthesized expression
The parentheses require 2 + 3 to be evaluated first. The result is 5 * 4, or 20.
- 3Compare the outcomes
Adding parentheses changed the grouping and therefore changed the result.
2 + 3 * 4 evaluates to 14, while (2 + 3) * 4 evaluates to 20.
Mistakes Beginners Make
Common mistake Evaluating every expression strictly from left to right
What it looks likeTreating 2 + 3 * 4 as though 2 + 3 happens before multiplication
Why it failsPrecedence must be checked before left-to-right order. Multiplication has a higher precedence than addition.
Do this insteadEvaluate 3 * 4 first, then add 2, producing 14.
Common mistake Assuming multiplication always happens before division
What it looks likeTreating 24 / 6 * 2 as though 6 * 2 must happen first
Why it failsMultiplication and division share the same precedence.
Do this insteadEvaluate the leftmost operation at that precedence level: 24 / 6 first, then multiply the intermediate result by 2.
Common mistake Applying left-to-right order across different precedence levels
What it looks likeUsing left-to-right order to evaluate addition before exponentiation or multiplication
Why it failsLeft-to-right is the tie-breaker for operations at the same precedence level, not a replacement for PEMDAS.
Do this insteadUse PEMDAS first, then use left-to-right within the multiplication and division level or within the addition and subtraction level.
Common mistake Avoiding parentheses because the default result seems obvious
What it looks likeWriting a longer expression without showing which calculation is intended as a group
Why it failsThe expression may be interpreted according to precedence rules in a way that does not communicate the writer's intent clearly.
Do this insteadAdd parentheses to override the default order or to make the intended grouping readable.
Practice and Review
Predict the result of 10 - 2 * 3 + 4. Identify the precedence level you use first, show the intermediate expression after that operation, and then explain how the remaining addition and subtraction are evaluated.
Hints
- Multiplication has higher precedence than addition and subtraction.
- After multiplication is resolved, addition and subtraction share a level and are evaluated from left to right.
- Do not evaluate the subtraction before resolving the multiplication.
Rewrite 6 + 4 * 5 with parentheses so that addition is evaluated before multiplication. Then predict how the result differs from the unparenthesized expression.
Hints
- Place parentheses around the addition.
- Evaluate the original expression using PEMDAS before comparing it with the parenthesized version.
- 1Python follows PEMDAS: parentheses, exponentiation, multiplication and division, then addition and subtraction. Higher-precedence operations are evaluated before lower-precedence operations. Multiplication and division share a level, as do addition and subtraction, so operations within either level are evaluated from left to right. Parentheses can override the default order and improve readability. When evaluating an expression, check precedence first and use left-to-right only to resolve ties.
Key Takeaways
- PEMDAS determines the main evaluation hierarchy: parentheses, exponentiation, multiplication and division, then addition and subtraction.
- Operations at the same precedence level are evaluated from left to right.
- Left-to-right order is a tie-breaker, not a rule that overrides precedence.
- Parentheses can change an expression's result by changing its grouping.
- Parentheses can also improve readability when they make the intended calculation explicit.