Operator Precedence
In Python 3, the / operator always returns a float, even when both operands are integers and the division is even.
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…
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
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.
- 1Evaluate 7 / 2
The / operator performs true division. In Python 3, its result is a float, so the result is 3.5.
- 2Evaluate 7 // 2
The // operator performs floor division. It rounds down to the nearest integer, so the result is 3.
- 3Compare 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.
- 1Without parentheses
The multiplication is performed before the addition, so 2 + 3 * 4 becomes 2 + 12 and then 14.
- 2With parentheses
The parentheses override the usual precedence, so 2 + 3 is evaluated first. The expression becomes 5 * 4 and then 20.
- 3Locate 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
Common mistake Reading every operator from left to right.
What it looks likeTreating 2 + 3 * 4 as if the addition happens before the multiplication.
Why it failsPython follows standard mathematical order of operations.
Do this insteadEvaluate the higher-precedence operation first, or use parentheses to make the intended order explicit.
Common mistake Expecting / to return an integer in Python 3.
What it looks likeExpecting 7 / 2 to produce an integer.
Why it failsIn Python 3, / always returns a float, even with integer operands.
Do this insteadUse // when you need floor division and an integer result.
Common mistake Assuming / and // are interchangeable.
What it looks likeReplacing 7 / 2 with 7 // 2 without considering the required result.
Why it failsTrue division and floor division perform different operations and produce different outputs.
Do this insteadChoose / for true division and // for floor division.
Common mistake Leaving a complicated intended order implicit.
What it looks likeWriting a mixed expression without parentheses when the desired grouping is not obvious.
Why it failsThe expression may follow Python's standard precedence rather than the grouping you intended.
Do this insteadAdd 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.
- 15 + 2 * 3
Multiplication comes before addition: 2 * 3 is 6, then 5 + 6 is 11.
- 2(5 + 2) * 3
Parentheses come first: 5 + 2 is 7, then 7 * 3 is 21.
- 39 / 2
Python 3 true division returns the float 4.5.
- 49 // 2
Floor division rounds down to the integer 4.
The results are 11, 21, 4.5, and 4, respectively.
Key Takeaways
- 1Python's five basic arithmetic operators are +, -, *, /, and **.
- 2Arithmetic expressions follow standard mathematical order of operations.
- 3Parentheses override precedence and make intended grouping explicit.
- 4In Python 3, / always returns a float, while // performs floor division and returns an integer by rounding down.
- 5Tracing intermediate expressions helps locate the exact operation that caused an unexpected result.
At a glance
- 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.