Concepts / Operator Precedence in Python

Operator Precedence in Python

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

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

Following 2 + 3 * 4

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

Identify the operators: The expression contains addition and multiplication.

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

Use the intermediate result: The multiplication produces 12, leaving the addition 2 + 12.

Finish the addition: Evaluate 2 + 12 to obtain 14.

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.

evaluated beforeMultiplicationhigher precedenceAdditionlower precedence
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.

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

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

  • Evaluating addition before multiplication in 2 + 3 * 4.

    Multiplication has higher precedence than addition.

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

  • Assuming parentheses are unnecessary because Python already has precedence rules.

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

    Fix: Use parentheses when they make the intended order clearer.

  • Confusing precedence with the final numerical result.

    The parentheses change which operation is performed first.

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