Remember
Python groups operations using precedence. Parentheses make your intended grouping explicit.
Python groups operations using precedence. Parentheses make your intended grouping explicit.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 3
print(number + 2 * 3)
print((number + 2) * 3)Top-level scope
3Python groups operations using precedence. Parentheses make your intended grouping explicit.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 3
print(number + 2 * 3)
print((number + 2) * 3)
|
v
DISPLAYED RESULT
9
15
FINAL BINDINGS
number --> object 1 (int)
object 1: 3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 3
print(number + 2 * 3)
print((number + 2) * 3)Top-level scope
3The example has finished. Compare the final output with the values and references above.
9 15
What do you expect this program to print?
number = 3
print(number + 2 * 3)
print((number + 2) * 3)When Python evaluates an expression, the order in which operations occur can drastically affect the result. This is known as operator precedence. Just like in regular arithmetic, multiplication comes before addition—Python uses similar rules internally.
Imagine the expression 3 + 4 * 2. Should Python add 3 and 4 first, or multiply 4 and 2? Without knowing the precedence rules, you'd be guessing.
Python follows a strict hierarchy of operators. Misunderstanding this hierarchy can lead to incorrect results, confusing bugs, or unexpected behavior.
Here’s the standard operator precedence in Python, from highest to lowest:
| Precedence | Operators | Description |
|---|---|---|
| 1 | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +x, -x, ~x | Unary plus, minus, bitwise NOT |
| 4 | *, /, //, % | Multiplication, division, floor division, modulus |
| 5 | +, - | Addition, subtraction |
| 6 | <<, >> | Bitwise shift |
| 7 | & | Bitwise AND |
| 8 | ^ | Bitwise XOR |
| 9 | | | Bitwise OR |
| 10 | ==, !=, >, <, >=, <= | Comparisons |
| 11 | not | Logical NOT |
| 12 | and | Logical AND |
| 13 | or | Logical OR |
| 14 | =, +=, -=, *=, /= ... | Assignment operators |
Let’s see Python in action. Try predicting the output before running the code:
result = 10 + 5 * 2
print("Result:", result)
Result: 20
Multiplication (*) has higher precedence than addition (+), so 5 * 2 is evaluated first, giving 10. Then 10 + 10 results in 20.
When in doubt, use parentheses to make the order explicit and readable:
result = (10 + 5) * 2
print("Result:", result)
Result: 30
This time, 10 + 5 is evaluated first due to the parentheses, resulting in 15, which is then multiplied by 2.
value = 3 + 2 > 4 and not False
print("Value:", value)
Value: True
3 + 2 → 55 > 4 → Truenot False → TrueTrue and True → Trueresult = 100 / 10 ** 2
print("Result:", result)
Result: 1.0
10 ** 2 is evaluated first, giving 100. Then 100 / 100 gives 1.0.
When two operators have the same precedence, Python uses associativity to decide the order. Most operators are left-associative (evaluated left to right), except **, which is right-associative.
result = 2 ** 3 ** 2
print("Result:", result)
Result: 512
This is equivalent to 2 ** (3 ** 2) → 2 ** 9 → 512.
Connect what you noticed in the example with the idea below.
Python groups operations using precedence. Parentheses make your intended grouping explicit.
Python always evaluates arithmetic from left to right regardless of parentheses.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.