Operator Precedence and Parentheses
Operator precedence is the priority that determines which operator Python applies 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 Decision
Consider the expression 2 + 3 * 4. Two operators appear, so Python must determine which operation happens first. Multiplication has higher precedence than addition, so Python evaluates 3 * 4 before adding 2. The expression therefore produces 14.
What do you think happens?
What result should Python produce for 2 + 3 * 4?
Reveal answer
14, because multiplication happens first
The multiplication operator has higher precedence than the addition operator. Python first calculates 3 * 4, then adds 2.
Following the Evaluation Path
An expression combines values and operators to calculate a result. Python evaluates the expression completely before proceeding. For 2 + 3 * 4, the multiplication is the first meaningful step. That intermediate result then becomes part of the remaining addition.
Tracing a mixed expression
Evaluate 2 + 3 * 4.
- 1Identify the operators
The expression contains addition and multiplication.
- 2Apply precedence
Multiplication has higher precedence, so evaluate 3 * 4 first.
- 3Continue with the remaining expression
The expression becomes 2 + 12.
- 4Calculate the result
The remaining addition produces 14.
14
Reading Operator Precedence
Operator precedence is the priority that determines which operator Python applies first when an expression contains more than one operator. In the source example, multiplication has higher precedence than addition. This is why 2 + 3 * 4 is evaluated as 2 + (3 * 4), rather than (2 + 3) * 4.
Making Order Explicit
Parentheses explicitly group part of an expression. They can change the evaluation path and make the intended order easier to see. Without parentheses, 2 + 3 * 4 evaluates multiplication first and produces 14. With parentheses around the addition, (2 + 3) * 4 evaluates the addition first and produces 20.
Use parentheses to make the order of operations explicit and to avoid mistakes. Even when precedence would produce the intended result, visible grouping can make an expression easier to understand.
Keeping Results for Later
An expression can be evaluated directly when its result is needed for a simple, one-time calculation. When the result must be reused or when a name would make the code more readable, store the expression result in a variable. The variable then represents the value produced by the completed expression.
| Choice | When it fits | Purpose |
|---|---|---|
| Evaluate directly in print | A simple, one-time calculation | Show the result immediately |
| Store in a variable | The result will be reused or a name improves readability | Keep the result available for later use |
Choosing between direct evaluation and storing an expression result
Common Evaluation Mistakes
Common mistake Assuming addition happens before multiplication because addition appears first.
What it looks likeReading 2 + 3 * 4 as (2 + 3) * 4.Why it failsMultiplication has higher precedence than addition.
Do this insteadEvaluate 3 * 4 first, then add 2, or use parentheses when a different order is intended.
Common mistake Leaving the intended order unclear in a longer expression.
What it looks likeUsing an expression with several operations and expecting the reader to remember the precedence rule.
Why it failsThe expression can be harder to read and easier to misinterpret.
Do this insteadUse parentheses to make the order of operations explicit.
Common mistake Recalculating a result that needs to be used more than once.
What it looks likeEvaluating the same multi-step expression separately every time its result is needed.
Why it failsThe result has not been stored for reuse, and the expression may be less readable.
Do this insteadStore the expression result in a variable when reuse or readability matters.
Common mistake Storing every simple, one-time calculation automatically.
What it looks likeCreating a variable for a result that is only needed immediately in a print statement.
Why it failsA variable is unnecessary when direct evaluation is clear and the calculation is used only once.
Do this insteadEvaluate a simple, one-time expression directly in print.
Practice the Trace
For each expression, write the first operation Python should evaluate, then calculate the final result. Also decide whether parentheses would make the intended order clearer: 5 + 2 * 3 and (5 + 2) * 3.
Hints
- Compare the precedence of multiplication and addition.
- For the parenthesized expression, evaluate the grouped addition first.
- Use a variable when you imagine that a result will be reused later.
Checking the practice expressions
Evaluate 5 + 2 * 3 and (5 + 2) * 3.
- 1First expression
Multiplication has higher precedence, so calculate 2 * 3 first. The expression becomes 5 + 6 and produces 11.
- 2Second expression
The parentheses require 5 + 2 to be evaluated first. The expression becomes 7 * 3 and produces 21.
- 3Compare the results
The parentheses change the evaluation order, so the two expressions produce different results.
5 + 2 * 3 produces 11; (5 + 2) * 3 produces 21.
Key Takeaways
- 1Expressions combine values and operators to calculate results, and Python evaluates them completely before proceeding.
- 2Multiplication has higher precedence than addition, so multiplication is evaluated first in an expression such as 2 + 3 * 4.
- 3Parentheses make the intended order explicit and can change the result by forcing a different operation to happen first.
- 4Evaluate a simple, one-time calculation directly in print; store a result in a variable when you need to reuse it or improve readability.
At a glance
- Python evaluates an expression according to operator precedence.
- Multiplication is evaluated before addition in expressions such as 2 + 3 * 4.
- Parentheses can change the evaluation order and make it explicit.
- Store expression results in variables when reuse or readability matters; evaluate directly in print for simple, one-time calculations.