Concepts / Operator Precedence in Complex Programs

Operator Precedence in Complex Programs

From highest to lowest precedence, it means Parentheses, Exponentiation, Multiplication and Division, and Addition and Subtraction.

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

Why Left-to-Right Fails

When an expression contains several operators, Python does not simply evaluate it from left to right. Instead, Python follows mathematical convention: operators have different precedence levels, and higher-precedence operators are evaluated first. This makes expressions predictable even when operators appear in different positions.

What do you think happens?

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

  • 20, because 2 + 3 is evaluated first

  • 14, because multiplication is evaluated before addition

Reveal answer
Answer:

14, because multiplication is evaluated before addition

Python follows mathematical convention rather than evaluating every expression strictly from left to right. The multiplication is completed before the addition.

(2 + 3) * 4202 + (3 * 4)14
What is the difference between evaluating an expression strictly left to right and following Python's precedence rules?

PEMDAS Levels

PEMDAS is a memory aid for Python's precedence order. From highest to lowest precedence, it means Parentheses, Exponentiation, Multiplication and Division, and Addition and Subtraction. An operator from a higher level is evaluated before an operator from a lower level, regardless of where the higher-precedence operator appears in the expression.

ParentheseshighestExponentiationMultiplication andDivisionAddition andSubtractionlowestbeforebeforebefore
Which operator group is evaluated first when several different operator types appear in one expression?
Precedence levelOperator groupEvaluation rule
HighestParenthesesEvaluate first
NextExponentiationEvaluate before multiplication, division, addition, and subtraction
NextMultiplication and divisionEvaluate before addition and subtraction
LowestAddition and subtractionEvaluate after the higher levels

PEMDAS ranks the main operator groups from highest to lowest precedence.

Tracing a Mixed Expression

Worked example

Evaluating 2 + 3 * 4

Determine the result of 2 + 3 * 4 by applying Python's precedence rules.

  1. 1
    Find the highest-precedence operator

    The expression contains addition and multiplication. Multiplication has higher precedence than addition.

  2. 2
    Evaluate multiplication

    Evaluate 3 * 4 first, producing 12.

  3. 3
    Evaluate addition

    The expression is now 2 + 12. Evaluate the addition to produce 14.

Result

14

2 + 3 * 4mixed operators3 * 4122 + 1214final resultmultiplication firstreplace productadd
What happens next when Python evaluates an expression containing multiplication and addition?

The important habit is to locate the highest-precedence operation before calculating. Do not begin with the operator that appears farthest to the left. First apply the precedence hierarchy, then use left-to-right evaluation only when the remaining operators share the same precedence level.

Parentheses as Intent

Parentheses have the highest precedence in the PEMDAS hierarchy. The expression inside parentheses is evaluated first, so parentheses can force Python to use an evaluation order different from the default precedence order.

Worked example

Changing the order with parentheses

Compare 2 + 3 * 4 with (2 + 3) * 4.

  1. 1
    Without parentheses

    Multiplication has higher precedence, so 3 * 4 is evaluated first. The result is 2 + 12, which equals 14.

  2. 2
    With parentheses

    The parentheses force 2 + 3 to be evaluated first. The result is 5 * 4, which equals 20.

Result

Adding parentheses changes both the evaluation sequence and the result in this comparison.

2 + 3 * 414(2 + 3) * 4203 * 4first2 + 3firstmultiplication firstparentheses first
How does the result and evaluation sequence change when parentheses are added to the same expression?

Same-Level Operators

Precedence does not decide everything. Multiplication and division share one precedence level, and addition and subtraction share another. When operators are at the same level, Python evaluates them from left to right, in the order they appear.

Operators in the expressionHow Python chooses the next operation
Multiplication and divisionEvaluate from left to right
Addition and subtractionEvaluate from left to right
Different precedence levelsEvaluate the higher-precedence group first

Use precedence first; use left-to-right order when the competing operators share a precedence level.

Mistakes with Mixed Operators

  • Common mistake

    Assuming Python evaluates every expression from left to right

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

    Multiplication has higher precedence than addition.

    Do this instead

    Evaluate the multiplication first, then evaluate the addition.

  • Common mistake

    Using left-to-right order before checking precedence

    What it looks like

    Choosing the leftmost addition before a multiplication that appears later

    Why it fails

    The position of an operator does not override its precedence level.

    Do this instead

    Identify the highest-precedence operator group before applying left-to-right order.

  • Common mistake

    Ignoring parentheses that were added intentionally

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

    Parentheses force their contents to be evaluated first.

    Do this instead

    Resolve the parenthesized expression before considering the remaining operators.

  • Common mistake

    Adding unnecessary mental complexity to a readable expression

    What it looks like

    Expecting a reader to apply PEMDAS to a complex expression without helpful grouping

    Why it fails

    An expression can be technically correct but harder for another person to understand.

    Do this instead

    Use parentheses when they clarify the intended grouping, even when they do not change the result.

Practice the Sequence

EASY

Predict the result of 2 + 3 * 4, then compare it with the result of (2 + 3) * 4. For each expression, write down which operation is evaluated first and explain why.

Hints
  • Check whether parentheses are present.
  • If there are no parentheses, compare the precedence of multiplication and addition.
  • Use left-to-right evaluation only if the operators being compared share the same precedence level.
MEDIUM

Consider the expression minute * 100 / 60. Explain why adding parentheses as (minute * 100) / 60 can improve readability, even though the evaluation order remains the same.

Hints
  • Multiplication and division share a precedence level.
  • Operators at the same level are evaluated from left to right.
  • Ask what grouping the parentheses make visible to a reader.

Key Takeaways

  1. 1
    Python follows mathematical convention for operator precedence rather than evaluating every expression strictly from left to right.
  2. 2
    PEMDAS places parentheses first, followed by exponentiation, multiplication and division, and addition and subtraction.
  3. 3
    Operators at the same precedence level are evaluated from left to right.
  4. 4
    Parentheses can change the result by forcing a new order, or improve readability without changing the result.
  5. 5
    When reading a complex expression, identify precedence first, then apply left-to-right evaluation where it is appropriate.

At a glance

  • Python uses precedence rules instead of evaluating all operators from left to right.
  • PEMDAS provides the order: Parentheses, Exponentiation, Multiplication and Division, Addition and Subtraction.
  • Same-level operators are evaluated from left to right.
  • Parentheses control evaluation order and communicate the programmer's intent.

Related concepts

More concepts starting with O