Writing and Evaluating Expressions
Python follows mathematical convention for operator precedence, not left-to-right evaluation
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?
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.
| Precedence level | Operators | Evaluation rule |
|---|---|---|
| Highest | Parentheses | Evaluate the expression inside parentheses first |
| Next | Exponentiation | Evaluate before multiplication, division, addition, or subtraction |
| Next | Multiplication and division | Evaluate before addition and subtraction; equal-level operators go left to right |
| Lowest | Addition and subtraction | Evaluate 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
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.
Expression Comparisons
| Expression | First operation | Result |
|---|---|---|
| 2 + 3 * 4 | 3 * 4 | 14 |
| (2 + 3) * 4 | 2 + 3 | 20 |
| minute * 100 / 60 | minute * 100, followed by division | Same evaluation as the parenthesized form |
| (minute * 100) / 60 | minute * 100, followed by division | Same 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
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.
- 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.