Operator Precedence in Complex Programs
From highest to lowest precedence, it means Parentheses, Exponentiation, Multiplication and Division, and Addition and Subtraction.
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 Left-to-Right Fails
When an expression contains several operators, Python does not simply evaluate it from left to right. Instead, Python follows mathematical convention: operators have different precedence levels, and higher-precedence operators are evaluated first. This makes expressions predictable even when operators appear in different positions.
What do you think happens?
What result should you expect from 2 + 3 * 4?
Reveal answer
14, because multiplication is evaluated before addition
Python follows mathematical convention rather than evaluating every expression strictly from left to right. The multiplication is completed before the addition.
PEMDAS Levels
PEMDAS is a memory aid for Python's precedence order. From highest to lowest precedence, it means Parentheses, Exponentiation, Multiplication and Division, and Addition and Subtraction. An operator from a higher level is evaluated before an operator from a lower level, regardless of where the higher-precedence operator appears in the expression.
| Precedence level | Operator group | Evaluation rule |
|---|---|---|
| Highest | Parentheses | Evaluate first |
| Next | Exponentiation | Evaluate before multiplication, division, addition, and subtraction |
| Next | Multiplication and division | Evaluate before addition and subtraction |
| Lowest | Addition and subtraction | Evaluate after the higher levels |
PEMDAS ranks the main operator groups from highest to lowest precedence.
Tracing a Mixed Expression
Evaluating 2 + 3 * 4
Determine the result of 2 + 3 * 4 by applying Python's precedence rules.
- 1Find the highest-precedence operator
The expression contains addition and multiplication. Multiplication has higher precedence than addition.
- 2Evaluate multiplication
Evaluate 3 * 4 first, producing 12.
- 3Evaluate addition
The expression is now 2 + 12. Evaluate the addition to produce 14.
14
The important habit is to locate the highest-precedence operation before calculating. Do not begin with the operator that appears farthest to the left. First apply the precedence hierarchy, then use left-to-right evaluation only when the remaining operators share the same precedence level.
Parentheses as Intent
Parentheses have the highest precedence in the PEMDAS hierarchy. The expression inside parentheses is evaluated first, so parentheses can force Python to use an evaluation order different from the default precedence order.
Changing the order with parentheses
Compare 2 + 3 * 4 with (2 + 3) * 4.
- 1Without parentheses
Multiplication has higher precedence, so 3 * 4 is evaluated first. The result is 2 + 12, which equals 14.
- 2With parentheses
The parentheses force 2 + 3 to be evaluated first. The result is 5 * 4, which equals 20.
Adding parentheses changes both the evaluation sequence and the result in this comparison.
Same-Level Operators
Precedence does not decide everything. Multiplication and division share one precedence level, and addition and subtraction share another. When operators are at the same level, Python evaluates them from left to right, in the order they appear.
| Operators in the expression | How Python chooses the next operation |
|---|---|
| Multiplication and division | Evaluate from left to right |
| Addition and subtraction | Evaluate from left to right |
| Different precedence levels | Evaluate the higher-precedence group first |
Use precedence first; use left-to-right order when the competing operators share a precedence level.
Mistakes with Mixed Operators
Common mistake Assuming Python evaluates every expression from left to right
What it looks likeTreating 2 + 3 * 4 as (2 + 3) * 4Why it failsMultiplication has higher precedence than addition.
Do this insteadEvaluate the multiplication first, then evaluate the addition.
Common mistake Using left-to-right order before checking precedence
What it looks likeChoosing the leftmost addition before a multiplication that appears later
Why it failsThe position of an operator does not override its precedence level.
Do this insteadIdentify the highest-precedence operator group before applying left-to-right order.
Common mistake Ignoring parentheses that were added intentionally
What it looks likeReading (2 + 3) * 4 as though it were 2 + 3 * 4Why it failsParentheses force their contents to be evaluated first.
Do this insteadResolve the parenthesized expression before considering the remaining operators.
Common mistake Adding unnecessary mental complexity to a readable expression
What it looks likeExpecting a reader to apply PEMDAS to a complex expression without helpful grouping
Why it failsAn expression can be technically correct but harder for another person to understand.
Do this insteadUse parentheses when they clarify the intended grouping, even when they do not change the result.
Practice the Sequence
Predict the result of 2 + 3 * 4, then compare it with the result of (2 + 3) * 4. For each expression, write down which operation is evaluated first and explain why.
Hints
- Check whether parentheses are present.
- If there are no parentheses, compare the precedence of multiplication and addition.
- Use left-to-right evaluation only if the operators being compared share the same precedence level.
Consider the expression minute * 100 / 60. Explain why adding parentheses as (minute * 100) / 60 can improve readability, even though the evaluation order remains the same.
Hints
- Multiplication and division share a precedence level.
- Operators at the same level are evaluated from left to right.
- Ask what grouping the parentheses make visible to a reader.
Key Takeaways
- 1Python follows mathematical convention for operator precedence rather than evaluating every expression strictly from left to right.
- 2PEMDAS places parentheses first, followed by exponentiation, multiplication and division, and addition and subtraction.
- 3Operators at the same precedence level are evaluated from left to right.
- 4Parentheses can change the result by forcing a new order, or improve readability without changing the result.
- 5When reading a complex expression, identify precedence first, then apply left-to-right evaluation where it is appropriate.
At a glance
- Python uses precedence rules instead of evaluating all operators from left to right.
- PEMDAS provides the order: Parentheses, Exponentiation, Multiplication and Division, Addition and Subtraction.
- Same-level operators are evaluated from left to right.
- Parentheses control evaluation order and communicate the programmer's intent.