Writing and Evaluating Expressions
Parenthesized expressions are handled first.
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
- Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
- Then *, /, // and %, left to right; then + and −, left to right.
- / always gives a float; // rounds down; % takes the sign of the divisor.
- 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.
Loading the simulation…
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
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.
- 1Find the higher-precedence operation
Multiplication has higher precedence than addition, so evaluate 3 * 4 before adding 2.
- 2Evaluate multiplication
The expression becomes 2 + 12.
- 3Evaluate 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.
- 1Compare the operators
Multiplication and division share the same precedence level.
- 2Use 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.
- 1Evaluate the unparenthesized expression
In 2 + 3 * 4, multiplication has higher precedence, so 3 * 4 is evaluated first. The result is 14.
- 2Evaluate 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 |
Parentheses can either change the result or clarify an evaluation order that was already implied.
Mistakes Beginners Make
Common mistake Assuming every expression is evaluated from left to right
What it looks likeTreating 2 + 3 * 4 as (2 + 3) * 4Why it failsMultiplication has higher precedence than addition.
Do this insteadEvaluate 3 * 4 first, then add 2.
Common mistake Forgetting that multiplication and division share one precedence level
Why it failsNeither operator automatically outranks the other.
Do this insteadWhen multiplication and division appear together, evaluate them from left to right.
Common mistake Treating addition as higher precedence than subtraction
Why it failsAddition and subtraction share the same precedence level.
Do this insteadEvaluate addition and subtraction from left to right when they appear at the same level.
Common mistake Using parentheses only when they change the result
Why it failsAn expression can be mathematically correct but still harder for another reader to understand.
Do this insteadAdd 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.
- 1Python 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.