Concepts / Operator Precedence and Parentheses

Operator Precedence and Parentheses

Operator precedence is the priority that determines which operator Python applies 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 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?

  • 20, because addition happens first

  • 14, because multiplication happens first

  • 24, because Python combines all numbers at once

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.

2 + 3 * 4expression3 * 4122 + 12remaining operation14final resulthigher precedencereplace 3 * 4 with 12add
What happens next when Python evaluates 2 + 3 * 4, and how does each intermediate result lead to the final value?
Worked example

Tracing a mixed expression

Evaluate 2 + 3 * 4.

  1. 1
    Identify the operators

    The expression contains addition and multiplication.

  2. 2
    Apply precedence

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

  3. 3
    Continue with the remaining expression

    The expression becomes 2 + 12.

  4. 4
    Calculate the result

    The remaining addition produces 14.

Result

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.

2value+lower precedence3value*higher precedence4valueapply first
How does Python decide which operator to apply first when multiplication and addition appear in the same expression?

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.

2 + 3 * 4multiply first14final result(2 + 3) * 4add first20final result3 * 4, then + 22 + 3, then * 4
How does adding parentheses change the evaluation path and result of 2 + 3 * 4 versus (2 + 3) * 4?
Best practice

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.

2 + 3 * 4expression14calculated resulttotalstored resulttotallater useevaluatestorereuse
How does the value produced by an expression move into a variable so it can be used later?
ChoiceWhen it fitsPurpose
Evaluate directly in printA simple, one-time calculationShow the result immediately
Store in a variableThe result will be reused or a name improves readabilityKeep the result available for later use

Choosing between direct evaluation and storing an expression result

Common Evaluation Mistakes

  • Common mistake

    Assuming addition happens before multiplication because addition appears first.

    What it looks like
    Reading 2 + 3 * 4 as (2 + 3) * 4.
    Why it fails

    Multiplication has higher precedence than addition.

    Do this instead

    Evaluate 3 * 4 first, then add 2, or use parentheses when a different order is intended.

  • Common mistake

    Leaving the intended order unclear in a longer expression.

    What it looks like

    Using an expression with several operations and expecting the reader to remember the precedence rule.

    Why it fails

    The expression can be harder to read and easier to misinterpret.

    Do this instead

    Use parentheses to make the order of operations explicit.

  • Common mistake

    Recalculating a result that needs to be used more than once.

    What it looks like

    Evaluating the same multi-step expression separately every time its result is needed.

    Why it fails

    The result has not been stored for reuse, and the expression may be less readable.

    Do this instead

    Store the expression result in a variable when reuse or readability matters.

  • Common mistake

    Storing every simple, one-time calculation automatically.

    What it looks like

    Creating a variable for a result that is only needed immediately in a print statement.

    Why it fails

    A variable is unnecessary when direct evaluation is clear and the calculation is used only once.

    Do this instead

    Evaluate a simple, one-time expression directly in print.

Practice the Trace

EASY

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

Checking the practice expressions

Evaluate 5 + 2 * 3 and (5 + 2) * 3.

  1. 1
    First expression

    Multiplication has higher precedence, so calculate 2 * 3 first. The expression becomes 5 + 6 and produces 11.

  2. 2
    Second expression

    The parentheses require 5 + 2 to be evaluated first. The expression becomes 7 * 3 and produces 21.

  3. 3
    Compare the results

    The parentheses change the evaluation order, so the two expressions produce different results.

Result

5 + 2 * 3 produces 11; (5 + 2) * 3 produces 21.

Key Takeaways

  1. 1
    Expressions combine values and operators to calculate results, and Python evaluates them completely before proceeding.
  2. 2
    Multiplication has higher precedence than addition, so multiplication is evaluated first in an expression such as 2 + 3 * 4.
  3. 3
    Parentheses make the intended order explicit and can change the result by forcing a different operation to happen first.
  4. 4
    Evaluate a simple, one-time calculation directly in print; store a result in a variable when you need to reuse it or improve readability.

At a glance

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

Related concepts

More concepts starting with O