Basic Operators in Python
Precedence decides which different operators come first. If operators have equal precedence, Python evaluates them from left to right.
Why Order Matters
When an expression contains several operators, Python must decide which operation to perform first. It does not simply evaluate every expression from left to right. Instead, it follows operator precedence: a set of rules that determines the order in which operations are performed.
What do you think happens?
What result should you expect from the expression 2 + 3 * 4?
Reveal answer
14, because 3 * 4 is evaluated first
Multiplication has higher precedence than addition. Python first computes 3 * 4 to get 12, then adds 2 to get 14.
Precedence Before Left-to-Right Evaluation
Python organizes operators into a precedence hierarchy. Operators with higher precedence are evaluated before operators with lower precedence. Multiplication and division have higher precedence than addition and subtraction, following the same convention commonly used in mathematics.
Precedence decides which different operators come first. If operators have equal precedence, Python evaluates them from left to right.
Tracing a Longer Expression
Step-by-step evaluation
Evaluate 10 - 2 * 3 + 8 / 4.
- 1Find the higher-precedence operations
Multiplication and division come before subtraction and addition. The expression contains 2 * 3 and 8 / 4.
- 2Evaluate multiplication
Compute 2 * 3 to produce 6.
- 3Evaluate division
Compute 8 / 4 to produce 2. Multiplication and division are handled from left to right when they share the same precedence.
- 4Evaluate the remaining operations
The expression is now 10 - 6 + 2. Subtraction and addition share the lower precedence level, so Python evaluates them from left to right: first 10 - 6, then 4 + 2.
6
Parentheses as Explicit Grouping
Parentheses override the default precedence rules. Python evaluates the expression inside parentheses as a single unit first, regardless of which operators it contains. This lets you specify the intended grouping directly instead of asking the reader to reconstruct it from precedence rules.
| Expression | First grouping | Result |
|---|---|---|
| 2 + 3 * 4 | 3 * 4 | 14 |
| (2 + 3) * 4 | 2 + 3 | 20 |
Parentheses can change both the grouping and the final result.
Mistakes Beginners Make
Common mistake Reading every expression strictly from left to right
What it looks likeTreating 2 + 3 * 4 as (2 + 3) * 4Why it failsMultiplication has higher precedence than addition, so Python evaluates 3 * 4 first.
Do this insteadEvaluate higher-precedence operations first, then continue with the remaining expression.
Common mistake Ignoring equal-precedence left-to-right evaluation
What it looks likeTreating 10 - 6 + 2 as 10 - (6 + 2)Why it failsSubtraction and addition have equal precedence, so Python evaluates them from left to right.
Do this insteadRead the remaining equal-precedence operations from left to right unless parentheses specify another grouping.
Common mistake Assuming parentheses are only decorative
What it looks likeComparing 2 + 3 * 4 with (2 + 3) * 4 as if they must have the same result
Why it failsParentheses override the default precedence rules and can change which operation happens first.
Do this insteadUse parentheses when you want a grouping different from the default or when the intended order should be explicit.
Practice and Takeaways
Predict the result of 10 - 2 * 3 + 8 / 4 without running it. Write the intermediate expression after evaluating the multiplication and division, then apply the remaining operations from left to right.
Hints
- Evaluate 2 * 3 and 8 / 4 before subtraction and addition.
- After replacing those subexpressions with their values, evaluate the remaining subtraction and addition from left to right.
Compare 5 + 3 * 2 and (5 + 3) * 2. Identify the first operation in each expression and predict both results.
Hints
- In the first expression, multiplication has higher precedence than addition.
- In the second expression, the parentheses make addition the first operation.
- 1Operator precedence determines which operations Python performs first. Multiplication and division are evaluated before addition and subtraction. Operators with equal precedence are evaluated from left to right. Parentheses override the default order, can change the result, and make the intended grouping easier to read.
At a glance
- Python uses precedence rules instead of evaluating every mixed expression strictly from left to right.
- Multiplication and division are evaluated before addition and subtraction.
- Operators with equal precedence are evaluated from left to right.
- Parentheses override default precedence and make grouping explicit.
- When an expression requires careful thought, parentheses can make the code clearer and easier to maintain.