Concepts / Writing and Evaluating Expressions

Writing and Evaluating Expressions

Python follows mathematical convention for operator precedence, not left-to-right evaluation

  • Programming

Why Order Matters

When an expression contains several operators, Python must decide which operation to perform first. It does not simply evaluate the expression from left to right. Instead, Python follows mathematical convention: operators with higher precedence are evaluated before operators with lower precedence. This makes expressions predictable and gives the same expression a consistent meaning for every reader and program.

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 the expression is evaluated from left to right
Reveal answer

Answer: 14, because multiplication happens before addition

Python follows operator precedence rather than simple left-to-right evaluation. The multiplication 3 * 4 is evaluated first, producing 12, and then 2 is added.

Reading Precedence from an Expression

PEMDAS is a mnemonic for Python's precedence order: Parentheses, Exponentiation, Multiplication and Division, Addition and Subtraction. Read it as a ranking from higher precedence to lower precedence. Parenthesized expressions are handled first. Exponentiation comes next. Multiplication and division follow, and addition and subtraction come after them. An operator's position in the written expression does not override this ranking.

2left value+addition3left factor*multiplication first4right factor
Which part of 2 + 3 * 4 does Python evaluate first?
Precedence levelOperatorsEvaluation rule
HighestParenthesesEvaluate the expression inside parentheses first
NextExponentiationEvaluate before multiplication, division, addition, or subtraction
NextMultiplication and divisionEvaluate before addition and subtraction; equal-level operators go left to right
LowestAddition and subtractionEvaluate after higher-precedence operators; equal-level operators go left to right

PEMDAS ranks the arithmetic operators from higher to lower precedence.

Tracing Each Evaluation Step

Evaluating 2 + 3 * 4

Determine the result of the expression 2 + 3 * 4.

Find the higher-precedence operation: Multiplication has higher precedence than addition, so evaluate 3 * 4 before adding 2.

Evaluate multiplication: The expression becomes 2 + 12.

Evaluate addition: Now evaluate 2 + 12.

14

evaluate multiplicationevaluate addition2 + 3 * 42 + 1214
How does the expression change after Python evaluates each operator according to precedence?

After Python completes an operation, its result takes the place of that operation in the remaining expression. This provides a useful way to evaluate an expression by hand: identify the highest-precedence operation that remains, replace it with its result, and continue. If two remaining operators have the same precedence, evaluate the one farther to the left first.

Equal-precedence operations

Determine how Python handles an expression containing multiplication and division.

Compare the operators: Multiplication and division share the same precedence level.

Use left-to-right order: Because the operators have equal precedence, Python evaluates them in the order they appear from left to right.

Equal-precedence operators are evaluated left to right.

Parentheses as Control

Parentheses have the highest precedence in the PEMDAS ranking. They force Python to evaluate the expression inside them before considering operators outside them. This lets you choose a grouping instead of relying on the default precedence rules.

Changing the grouping

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

Evaluate the unparenthesized expression: In 2 + 3 * 4, multiplication has higher precedence, so 3 * 4 is evaluated first. The result is 14.

Evaluate the parenthesized expression: In (2 + 3) * 4, the parentheses force 2 + 3 to be evaluated first. The result is then multiplied by 4.

The first expression produces 14, while the parenthesized expression produces 20.

multiplication firstparentheses first2 + 3 * 4(2 + 3) * 41420
How does adding parentheses change which subexpression Python evaluates first and the final result?
higher precedence thanhigher precedence thanhigher precedence thanParenthesesExponentiationMultiplication anddivisionAddition andsubtraction
Which operators have priority over others, and where do parentheses fit in the evaluation order?

Expression Comparisons

resultresult2 + 3 * 4multiplication first(2 + 3) * 4addition first1420
What is different when the same numbers and operators are grouped in different ways?
ExpressionFirst operationResult
2 + 3 * 43 * 414
(2 + 3) * 42 + 320
minute * 100 / 60minute * 100, followed by divisionSame evaluation as the parenthesized form
(minute * 100) / 60minute * 100, followed by divisionSame result, with clearer grouping

Mistakes Beginners Make

  • Assuming every expression is evaluated from left to right

    Multiplication has higher precedence than addition.

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

  • Forgetting that multiplication and division share one precedence level

    Neither operator automatically outranks the other.

    Fix: When multiplication and division appear together, evaluate them from left to right.

  • Treating addition as higher precedence than subtraction

    Addition and subtraction share the same precedence level.

    Fix: Evaluate addition and subtraction from left to right when they appear at the same level.

  • Using parentheses only when they change the result

    An expression can be mathematically correct but still harder for another reader to understand.

    Fix: Add parentheses when they make the intended grouping immediately clear.

When evaluating an unfamiliar expression, first mark every parenthesized part. Then identify exponentiation, followed by multiplication and division, followed by addition and subtraction. At each level, move from left to right. When writing an expression for other people to read, use parentheses when the intended grouping might require mental effort to recognize.

Practice the Trace

MEDIUM

For each expression, identify which operation Python evaluates first and determine the final result: 2 + 3 * 4; (2 + 3) * 4; and (minute * 100) / 60. Then explain whether the parentheses change the result, clarify the grouping, or do both.

Hints
  • Start with the PEMDAS hierarchy.
  • For operators at the same precedence level, use left-to-right order.
  • Compare the two expressions containing 2, 3, and 4 separately from the minute expression.
  1. Python does not generally evaluate mixed arithmetic expressions from left to right. It follows PEMDAS: parentheses, exponentiation, multiplication and division, then addition and subtraction. Operators at the same precedence level are evaluated from left to right. Parentheses can force a different order, as in (2 + 3) * 4, and they can also clarify an order that would otherwise be harder to read, as in (minute * 100) / 60.

Key Takeaways

  • Python uses operator precedence rather than simple left-to-right evaluation.
  • PEMDAS ranks parentheses, exponentiation, multiplication and division, then addition and subtraction.
  • Operators at the same precedence level are evaluated from left to right.
  • Parentheses can change an expression's result by forcing a grouping.
  • Parentheses can also improve readability without changing the result.