Operator Precedence and Parentheses
If you had an expression such as 2 + 3 * 4 , is the addition done first or the multiplication? Our high school maths tells us that the multiplication should be done first. This means that the multiplication operator has higher precedence than the addition operator.
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
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.
Identify the operators: The expression contains addition and multiplication.
Apply precedence: Multiplication has higher precedence, so evaluate 3 * 4 first.
Continue with the remaining expression: The expression becomes 2 + 12.
Calculate 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
Assuming addition happens before multiplication because addition appears first.
Multiplication has higher precedence than addition.
Fix:
Evaluate 3 * 4 first, then add 2, or use parentheses when a different order is intended.Leaving the intended order unclear in a longer expression.
The expression can be harder to read and easier to misinterpret.
Fix:
Use parentheses to make the order of operations explicit.Recalculating a result that needs to be used more than once.
The result has not been stored for reuse, and the expression may be less readable.
Fix:
Store the expression result in a variable when reuse or readability matters.Storing every simple, one-time calculation automatically.
A variable is unnecessary when direct evaluation is clear and the calculation is used only once.
Fix:
Evaluate 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.
First expression: Multiplication has higher precedence, so calculate 2 * 3 first. The expression becomes 5 + 6 and produces 11.
Second expression: The parentheses require 5 + 2 to be evaluated first. The expression becomes 7 * 3 and produces 21.
Compare 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
- Expressions combine values and operators to calculate results, and Python evaluates them completely before proceeding.
- Multiplication has higher precedence than addition, so multiplication is evaluated first in an expression such as 2 + 3 * 4.
- Parentheses make the intended order explicit and can change the result by forcing a different operation to happen first.
- Evaluate a simple, one-time calculation directly in print; store a result in a variable when you need to reuse it or improve readability.
Key Takeaways
- 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.