Concepts / Operator Precedence in Python

Operator Precedence in Python

Operator precedence describes which operator is applied first when an expression contains more than one operator.

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

The First Calculation

Consider the expression 2 + 3 * 4. Two operations appear in it: addition and multiplication. Which operation should Python perform first? Multiplication is performed before addition because the multiplication operator has higher precedence than the addition operator.

What do you think happens?

What result should Python produce for 2 + 3 * 4?

  • 20

  • 14

Reveal answer
Answer:

14

Multiplication has higher precedence than addition, so Python evaluates 3 * 4 first, producing 12. It then evaluates 2 + 12, producing 14.

Tracing the Evaluation

2left operand12result of 3 * 414result of 2 + 12+addition3left multiplication operand*multiplication4right multiplicationoperandmultiplymultiplyaddadd
What happens first when Python evaluates 2 + 3 * 4, and how does the intermediate result lead to 14?
Worked example

Following 2 + 3 * 4

Determine the result of 2 + 3 * 4 by applying operator precedence.

  1. 1
    Identify the operators

    The expression contains addition and multiplication.

  2. 2
    Apply precedence

    Multiplication has higher precedence than addition, so evaluate 3 * 4 first.

  3. 3
    Use the intermediate result

    The multiplication produces 12, leaving the addition 2 + 12.

  4. 4
    Finish the addition

    Evaluate 2 + 12 to obtain 14.

Result

The expression evaluates to 14.

Relative Operator Binding

Operator precedence describes which operator is applied first when an expression contains more than one operator. An operator with higher precedence binds more strongly than an operator with lower precedence. For the operators shown in this topic, multiplication has higher precedence than addition, so multiplication is evaluated before addition.

Multiplicationhigher precedenceAdditionlower precedenceevaluated before
Why is multiplication performed before addition, and how are the two operators ranked relative to each other?

Parentheses and Explicit Grouping

Although Python has precedence rules, parentheses are a better way to group operators and operands explicitly. Parentheses make the intended order easier to read and can change which operation is performed first.

2 + 3 * 4multiplication groupedfirst2 + (3 * 4)14(2 + 3) * 4addition grouped first5 * 420precedence groupsparentheses group
How does Python implicitly group 2 + 3 * 4 compared with (2 + 3) * 4?
ExpressionFirst operationResult
2 + 3 * 43 * 414
(2 + 3) * 42 + 320

Parentheses override the otherwise higher precedence of multiplication by explicitly grouping the addition first.

Best practice

When an expression could be misread, use parentheses to show the intended grouping. This improves readability and makes the order of evaluation explicit instead of requiring the reader to recall the precedence rules.

Mistakes with Mixed Operators

  • Common mistake

    Evaluating addition before multiplication in 2 + 3 * 4.

    What it looks like
    Treating the expression as (2 + 3) * 4.
    Why it fails

    Multiplication has higher precedence than addition.

    Do this instead

    Evaluate 3 * 4 first, then add 2, giving 14.

  • Common mistake

    Assuming parentheses are unnecessary because Python already has precedence rules.

    What it looks like

    Writing a mixed expression without showing the intended grouping.

    Why it fails

    The expression may be difficult for another reader to interpret, even when Python can evaluate it.

    Do this instead

    Use parentheses when they make the intended order clearer.

  • Common mistake

    Confusing precedence with the final numerical result.

    What it looks like

    Expecting 2 + 3 * 4 and (2 + 3) * 4 to mean the same thing.

    Why it fails

    The parentheses change which operation is performed first.

    Do this instead

    Check the grouping before calculating the result.

Check Your Understanding

EASY

For each expression, identify the operation performed first and then determine the result: 2 + 3 * 4 and (2 + 3) * 4.

Hints
  • For the first expression, compare the precedence of multiplication and addition.
  • For the second expression, inspect the operation inside the parentheses first.
  1. 1
    Operator precedence determines which operation is evaluated first in an expression. Multiplication has higher precedence than addition, so 2 + 3 * 4 is evaluated as 2 + (3 * 4), producing 14. Parentheses explicitly group an expression and can change the result, as in (2 + 3) * 4, which groups the addition first.

Key Takeaways

  • Operator precedence determines the order in which competing operators are evaluated.
  • Multiplication has higher precedence than addition.
  • The expression 2 + 3 * 4 is evaluated as 2 + (3 * 4), giving 14.
  • Parentheses explicitly control grouping and can change the result.
  • Using parentheses can make an expression more readable and its intended order clearer.

Related concepts

More concepts starting with O