Operator Precedence in Python
Operator precedence describes which operator is applied first when an expression contains more than one operator.
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 First Calculation
Consider the expression 2 + 3 * 4. Two operations appear in it: addition and multiplication. Which operation should Python perform first? Multiplication is performed before addition because the multiplication operator has higher precedence than the addition operator.
What do you think happens?
What result should Python produce for 2 + 3 * 4?
Reveal answer
14
Multiplication has higher precedence than addition, so Python evaluates 3 * 4 first, producing 12. It then evaluates 2 + 12, producing 14.
Tracing the Evaluation
Following 2 + 3 * 4
Determine the result of 2 + 3 * 4 by applying operator precedence.
- 1Identify the operators
The expression contains addition and multiplication.
- 2Apply precedence
Multiplication has higher precedence than addition, so evaluate 3 * 4 first.
- 3Use the intermediate result
The multiplication produces 12, leaving the addition 2 + 12.
- 4Finish the addition
Evaluate 2 + 12 to obtain 14.
The expression evaluates to 14.
Relative Operator Binding
Operator precedence describes which operator is applied first when an expression contains more than one operator. An operator with higher precedence binds more strongly than an operator with lower precedence. For the operators shown in this topic, multiplication has higher precedence than addition, so multiplication is evaluated before addition.
Parentheses and Explicit Grouping
Although Python has precedence rules, parentheses are a better way to group operators and operands explicitly. Parentheses make the intended order easier to read and can change which operation is performed first.
| Expression | First operation | Result |
|---|---|---|
| 2 + 3 * 4 | 3 * 4 | 14 |
| (2 + 3) * 4 | 2 + 3 | 20 |
Parentheses override the otherwise higher precedence of multiplication by explicitly grouping the addition first.
When an expression could be misread, use parentheses to show the intended grouping. This improves readability and makes the order of evaluation explicit instead of requiring the reader to recall the precedence rules.
Mistakes with Mixed Operators
Common mistake Evaluating addition before multiplication in 2 + 3 * 4.
What it looks likeTreating the expression as (2 + 3) * 4.Why it failsMultiplication has higher precedence than addition.
Do this insteadEvaluate 3 * 4 first, then add 2, giving 14.
Common mistake Assuming parentheses are unnecessary because Python already has precedence rules.
What it looks likeWriting a mixed expression without showing the intended grouping.
Why it failsThe expression may be difficult for another reader to interpret, even when Python can evaluate it.
Do this insteadUse parentheses when they make the intended order clearer.
Common mistake Confusing precedence with the final numerical result.
What it looks likeExpecting 2 + 3 * 4 and (2 + 3) * 4 to mean the same thing.
Why it failsThe parentheses change which operation is performed first.
Do this insteadCheck the grouping before calculating the result.
Check Your Understanding
For each expression, identify the operation performed first and then determine the result: 2 + 3 * 4 and (2 + 3) * 4.
Hints
- For the first expression, compare the precedence of multiplication and addition.
- For the second expression, inspect the operation inside the parentheses first.
- 1Operator precedence determines which operation is evaluated first in an expression. Multiplication has higher precedence than addition, so 2 + 3 * 4 is evaluated as 2 + (3 * 4), producing 14. Parentheses explicitly group an expression and can change the result, as in (2 + 3) * 4, which groups the addition first.
Key Takeaways
- Operator precedence determines the order in which competing operators are evaluated.
- Multiplication has higher precedence than addition.
- The expression 2 + 3 * 4 is evaluated as 2 + (3 * 4), giving 14.
- Parentheses explicitly control grouping and can change the result.
- Using parentheses can make an expression more readable and its intended order clearer.