Concepts / Working with Numbers and Arithmetic

Working with Numbers and Arithmetic

Operator precedence is a set of rules that determines which operations Python performs first when multiple operators appear in the same expression.

  • Programming

Why Reading Left to Right Fails

When several arithmetic operators appear in one expression, Python does not simply calculate from left to right. It follows operator precedence: a set of rules that determines which operations happen first. This follows the same broad convention used in school mathematics.

What do you think happens?

What result should you expect from 2 + 3 * 4?

  • 20
  • 14
Reveal answer

Answer: 14

Python evaluates 3 * 4 first because multiplication has higher precedence than addition. That produces 12, and then Python evaluates 2 + 12, producing 14.

higher precedence firstreplace 3 * 4 with 12evaluate2 + 3 * 4four values and operators3 * 4122 + 121414final value
What operation does Python perform first, and how does the result flow through the remaining expression?

Precedence Levels

Python organizes operators into a precedence hierarchy. Operators with higher precedence bind more strongly and are evaluated before operators with lower precedence. In the arithmetic examples covered here, multiplication and division have higher precedence than addition and subtraction.

evaluated beforeAddition andsubtractionlower precedenceMultiplication anddivisionhigher precedence
How are the arithmetic operators ranked, and which operators take priority over others?

A useful first question is: which operators have the highest precedence in this expression? Evaluate those operations before moving to lower-precedence operations.

Tracing a Compound Expression

To predict a compound expression, identify the higher-precedence operations first. Then evaluate equal-precedence operations from left to right. Continue until the expression has become one value.

Evaluating 10 - 2 * 3 + 8 / 4

Determine the result without treating the expression as a simple left-to-right calculation.

Identify priorities: Multiplication and division have higher precedence than subtraction and addition.

Evaluate multiplication: Evaluate 2 * 3 first, giving 6.

Evaluate division: Evaluate 8 / 4, giving 2. Multiplication and division are handled before subtraction and addition.

Evaluate remaining operations: The expression is now 10 - 6 + 2. Subtraction and addition have equal precedence, so Python evaluates them from left to right: 10 - 6 gives 4, then 4 + 2 gives 6.

The final result is 6.

evaluate 2 * 3 and 8 / 4evaluate 10 - 6evaluate 4 + 210 - 2 * 3 + 8 / 4original expression10 - 6 + 2multiplication and divisionevaluated4 + 210 - 6 evaluated6final value
How can you trace each intermediate value to determine the final result without running the code?

Parentheses as Intent

Parentheses override the default precedence rules. Python treats the expression inside parentheses as a single unit and evaluates it first, regardless of which operators appear inside. This lets you specify the calculation you intend instead of relying only on the reader to remember the precedence hierarchy.

evaluateevaluate2 + 3 * 4multiplication first(2 + 3) * 4addition first14default result20explicit result
How does adding parentheses change the grouping and evaluation order of an expression?

Do not add parentheses mechanically to every expression. A simple expression such as 5 + 3 * 2 is clear because multiplication is conventionally evaluated first. Add parentheses when an expression is complex, mixes operators in a way that could slow the reader down, or needs an order different from the default.

Mistakes to Avoid

  • Assuming Python evaluates every expression strictly from left to right.

    Multiplication has higher precedence than addition, so Python evaluates 3 * 4 before adding 2.

    Fix: Look for higher-precedence operations first.

  • Ignoring left-to-right evaluation when operators share a precedence level.

    Subtraction and addition have equal precedence, so Python evaluates them from left to right.

    Fix: After handling higher-precedence operations, process equal-precedence operations from left to right.

  • Adding parentheses without checking what grouping they create.

    The parentheses deliberately move addition ahead of multiplication, changing the result.

    Fix: Use parentheses to express the intended order, then trace the grouped expression.

  • Relying on implicit precedence in a complicated expression when the intended grouping is not immediately clear.

    Even if Python can evaluate the expression, another programmer may need to spend extra effort determining the intended calculation.

    Fix: Use parentheses when they make a complex calculation easier to understand and maintain.

Practice and Review

MEDIUM

Predict the result of 6 + 4 * 2 - 3. First identify the multiplication, then evaluate the remaining addition and subtraction from left to right. Afterward, consider how parentheses could express a different intended grouping.

Hints
  • Multiplication has higher precedence than addition and subtraction.
  • After evaluating 4 * 2, the remaining addition and subtraction have equal precedence.
  • Evaluate the remaining operations from left to right.
  1. Operator precedence determines which operations Python performs first in a multi-operator expression. Multiplication and division take priority over addition and subtraction in the arithmetic examples covered here. Equal-precedence operators are evaluated from left to right. Parentheses override the default order and make the intended grouping explicit, which is especially valuable when an expression is complex.

Key Takeaways

  • Python uses operator precedence instead of evaluating every arithmetic expression strictly from left to right.
  • Multiplication and division are evaluated before addition and subtraction in the examples covered here.
  • Operators with equal precedence are evaluated from left to right.
  • Parentheses override default precedence and show which operation should happen first.
  • Clear parentheses can make complex arithmetic expressions easier to read and maintain.