⬅ Previous Topic
Python OperatorsNext Topic ⮕
Python Conditional Statements⬅ Previous Topic
Python OperatorsNext Topic ⮕
Python Conditional StatementsWhen 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
→ True
result = 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.
Understanding Python’s operator precedence isn’t just about memorizing a chart—it's about writing correct and readable code. With real-world programs, using parentheses often makes your intent clearer, reduces bugs, and helps others understand your logic. As you progress, you’ll start internalizing precedence rules naturally. But until then, don’t hesitate to clarify with brackets!
⬅ Previous Topic
Python OperatorsNext Topic ⮕
Python Conditional StatementsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.