Concepts / Operator Precedence

Operator Precedence

In Python 3, the / operator always returns a float, even when both operands are integers and the division is even.

  • Programming
Interactive lab

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

  1. Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
  2. Then *, /, // and %, left to right; then + and −, left to right.
  3. / always gives a float; // rounds down; % takes the sign of the divisor.
  4. 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.

Educational simulation

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?

  • 2 + 3

  • 3 * 4

  • 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.

2 + 3 * 4 ** 2mixed expression4 ** 2163 * 16482 + 4850firstthenfinally
What happens first, and in what order are the operators in an expression evaluated?
python
Output
50

Five Arithmetic Operators

OperatorOperation
+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.

7 and 2same operands7 / 23.57 // 23///
How do / and // transform the same operands into different outputs?
Worked example

Comparing the two division operators

Predict the results of 7 / 2 and 7 // 2 in Python 3.

  1. 1
    Evaluate 7 / 2

    The / operator performs true division. In Python 3, its result is a float, so the result is 3.5.

  2. 2
    Evaluate 7 // 2

    The // operator performs floor division. It rounds down to the nearest integer, so the result is 3.

  3. 3
    Compare the results

    The same operands produce different results because the operators specify different kinds of division.

Result

7 / 2 produces 3.5, while 7 // 2 produces 3.

python
Output
3.5
3

Tracing 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.

2 + 3 * 4 ** 2initial expression2 + 3 * 16exponentiation complete2 + 48multiplication complete50final resultevaluate **evaluate *evaluate +
How does the intermediate result change after each operation, and where does the result diverge from what I expected?
Worked example

Making the intended order explicit

Compare 2 + 3 * 4 with (2 + 3) * 4.

  1. 1
    Without parentheses

    The multiplication is performed before the addition, so 2 + 3 * 4 becomes 2 + 12 and then 14.

  2. 2
    With parentheses

    The parentheses override the usual precedence, so 2 + 3 is evaluated first. The expression becomes 5 * 4 and then 20.

  3. 3
    Locate the divergence

    The results diverge at the first operation: the unparenthesized expression multiplies first, while the parenthesized expression adds first.

Result

2 + 3 * 4 produces 14, while (2 + 3) * 4 produces 20.

python
Output
14
20

Common Mistakes

  • Common mistake

    Reading every operator from left to right.

    What it looks like

    Treating 2 + 3 * 4 as if the addition happens before the multiplication.

    Why it fails

    Python follows standard mathematical order of operations.

    Do this instead

    Evaluate 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 like

    Expecting 7 / 2 to produce an integer.

    Why it fails

    In Python 3, / always returns a float, even with integer operands.

    Do this instead

    Use // when you need floor division and an integer result.

  • Common mistake

    Assuming / and // are interchangeable.

    What it looks like

    Replacing 7 / 2 with 7 // 2 without considering the required result.

    Why it fails

    True division and floor division perform different operations and produce different outputs.

    Do this instead

    Choose / for true division and // for floor division.

  • Common mistake

    Leaving a complicated intended order implicit.

    What it looks like

    Writing a mixed expression without parentheses when the desired grouping is not obvious.

    Why it fails

    The expression may follow Python's standard precedence rather than the grouping you intended.

    Do this instead

    Add parentheses to override precedence and make the calculation easier to read and debug.

Practice Trace

EASY

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.
Worked example

Practice check

Evaluate 5 + 2 * 3, (5 + 2) * 3, 9 / 2, and 9 // 2.

  1. 1
    5 + 2 * 3

    Multiplication comes before addition: 2 * 3 is 6, then 5 + 6 is 11.

  2. 2
    (5 + 2) * 3

    Parentheses come first: 5 + 2 is 7, then 7 * 3 is 21.

  3. 3
    9 / 2

    Python 3 true division returns the float 4.5.

  4. 4
    9 // 2

    Floor division rounds down to the integer 4.

Result

The results are 11, 21, 4.5, and 4, respectively.

Key Takeaways

  1. 1
    Python's five basic arithmetic operators are +, -, *, /, and **.
  2. 2
    Arithmetic expressions follow standard mathematical order of operations.
  3. 3
    Parentheses override precedence and make intended grouping explicit.
  4. 4
    In Python 3, / always returns a float, while // performs floor division and returns an integer by rounding down.
  5. 5
    Tracing 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.

Related concepts

More concepts starting with O