Concepts / Operator Precedence and Parentheses

Operator Precedence and Parentheses

If you had an expression such as 2 + 3 * 4 , is the addition done first or the multiplication? Our high school maths tells us that the multiplication should be done first. This means that the multiplication operator has higher precedence than the addition operator.

  • Programming

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.

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

Tracing a mixed expression

Evaluate 2 + 3 * 4.

Identify the operators: The expression contains addition and multiplication.

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

Continue with the remaining expression: The expression becomes 2 + 12.

Calculate the result: The remaining addition produces 14.

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.

apply first2value+lower precedence3value*higher precedence4value
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.

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

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.

evaluatestorereuse2 + 3 * 4expression14calculated resulttotalstored resulttotallater use
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

  • Assuming addition happens before multiplication because addition appears first.

    Multiplication has higher precedence than addition.

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

  • Leaving the intended order unclear in a longer expression.

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

    Fix: Use parentheses to make the order of operations explicit.

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

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

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

  • Storing every simple, one-time calculation automatically.

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

    Fix: 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.

Checking the practice expressions

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

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

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

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

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

Key Takeaways

  1. Expressions combine values and operators to calculate results, and Python evaluates them completely before proceeding.
  2. Multiplication has higher precedence than addition, so multiplication is evaluated first in an expression such as 2 + 3 * 4.
  3. Parentheses make the intended order explicit and can change the result by forcing a different operation to happen first.
  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.

Key Takeaways

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