Operator Precedence
Python provides five basic arithmetic operators: +, -, *, /, and **
When an Expression Surprises You
An arithmetic expression can contain several operations at once. Python does not simply evaluate every operator from left to right. Instead, it follows the standard mathematical order of operations. Understanding that order lets you predict the result, while parentheses let you make a different order explicit. Python provides five basic arithmetic operators: + for addition, - for subtraction, * for multiplication, / for division, and ** for exponentiation.
What do you think happens?
What value does Python calculate first in the expression 2 + 3 * 4 ** 2?
Reveal answer
Answer: 4 ** 2
The expression follows standard mathematical order of operations. Exponentiation is evaluated before multiplication, and multiplication is evaluated before addition.
Evaluation Order
For a mixed arithmetic expression, trace the operations in precedence order rather than reading only from left to right. In the generated example 2 + 3 * 4 ** 2, Python first evaluates 4 ** 2, then multiplies 3 by that result, and finally adds 2. Parentheses override the usual precedence, so adding parentheses can change which operation happens first and can make your intent easier to read.
50Five Arithmetic Operators
| Operator | Operation |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| ** | Exponentiation |
The five basic arithmetic operators identified in the source material.
These five operators form the basic arithmetic toolkit in Python. The operator symbol tells Python which calculation to perform, while precedence determines the order when multiple operators appear in one expression. The division operators require special attention because / and // do not produce the same kind of result.
Division Results
In Python 3, the / operator always returns a float, even when both operands are integers and the division is even. The // operator performs floor division and returns an integer by rounding down. Therefore, replacing / with // changes both the operation and the form of the result.
Comparing the two division operators
Predict the results of 7 / 2 and 7 // 2 in Python 3.
Evaluate 7 / 2: The / operator performs true division. In Python 3, its result is a float, so the result is 3.5.
Evaluate 7 // 2: The // operator performs floor division. It rounds down to the nearest integer, so the result is 3.
Compare the results: The same operands produce different results because the operators specify different kinds of division.
7 / 2 produces 3.5, while 7 // 2 produces 3.
3.5
3Tracing Unexpected Results
When an expression gives an unexpected result, rewrite it as a sequence of smaller operations. First identify the operation with the highest precedence, calculate its intermediate result, and substitute that result back into the expression. Continue until only the final operation remains. This process exposes the exact point where your expectation and Python's evaluation order diverge.
Making the intended order explicit
Compare 2 + 3 * 4 with (2 + 3) * 4.
Without parentheses: The multiplication is performed before the addition, so 2 + 3 * 4 becomes 2 + 12 and then 14.
With parentheses: The parentheses override the usual precedence, so 2 + 3 is evaluated first. The expression becomes 5 * 4 and then 20.
Locate the divergence: The results diverge at the first operation: the unparenthesized expression multiplies first, while the parenthesized expression adds first.
2 + 3 * 4 produces 14, while (2 + 3) * 4 produces 20.
14
20Common Mistakes
Reading every operator from left to right.
Python follows standard mathematical order of operations.
Fix:
Evaluate the higher-precedence operation first, or use parentheses to make the intended order explicit.Expecting / to return an integer in Python 3.
In Python 3, / always returns a float, even with integer operands.
Fix:
Use // when you need floor division and an integer result.Assuming / and // are interchangeable.
True division and floor division perform different operations and produce different outputs.
Fix:
Choose / for true division and // for floor division.Leaving a complicated intended order implicit.
The expression may follow Python's standard precedence rather than the grouping you intended.
Fix:
Add parentheses to override precedence and make the calculation easier to read and debug.
Practice Trace
Trace each expression before evaluating it. Write the intermediate expression after the first operation, then give the final result: 1) 5 + 2 * 3, 2) (5 + 2) * 3, 3) 9 / 2, and 4) 9 // 2.
Hints
- For the first expression, evaluate multiplication before addition.
- For the second expression, evaluate the parenthesized addition first.
- In Python 3, / produces a float.
- The // operator rounds down to the nearest integer.
Practice check
Evaluate 5 + 2 * 3, (5 + 2) * 3, 9 / 2, and 9 // 2.
5 + 2 * 3: Multiplication comes before addition: 2 * 3 is 6, then 5 + 6 is 11.
(5 + 2) * 3: Parentheses come first: 5 + 2 is 7, then 7 * 3 is 21.
9 / 2: Python 3 true division returns the float 4.5.
9 // 2: Floor division rounds down to the integer 4.
The results are 11, 21, 4.5, and 4, respectively.
Key Takeaways
- Python's five basic arithmetic operators are +, -, *, /, and **.
- Arithmetic expressions follow standard mathematical order of operations.
- Parentheses override precedence and make intended grouping explicit.
- In Python 3, / always returns a float, while // performs floor division and returns an integer by rounding down.
- Tracing intermediate expressions helps locate the exact operation that caused an unexpected result.
Key Takeaways
- Use +, -, *, /, and ** for Python's five basic arithmetic operations.
- Follow standard mathematical precedence when evaluating mixed expressions.
- Use parentheses to override precedence and clarify intent.
- In Python 3, / returns a float, whereas // performs floor division and returns an integer by rounding down.
- Trace intermediate results to debug arithmetic expressions.