Python Operators Precedence
Operators precedence determines the order in which different operators are evaluated in an expression. Understanding operator precedence is crucial for writing correct and predictable code.
Operator Precedence Order
Here’s a summary of Python operator precedence from highest to lowest:
- Parentheses
()
- Exponentiation
**
- Unary Plus and Minus, Bitwise NOT
+x
,-x
,~x
- Multiplication, Division, Floor Division, Modulus
*
,/
,//
,%
- Addition, Subtraction
+
,-
- Bitwise Shift Operators
<<
,>>
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Comparison Operators
==
,!=
,>
,<
,>=
,<=
- Boolean NOT
not
- Boolean AND
and
- Boolean OR
or
- Assignment Operators
=
,+=
,-=
,*=
,/=
, etc.
1. Parentheses ()
Parentheses have the highest precedence and are used to explicitly define the order of operations.
Example:
Output:
Explanation:
- The expression inside parentheses
(3 + 5)
is evaluated first, resulting in8
. - Then,
8 * 2
is evaluated to give16
.
2. Exponentiation **
Exponentiation has higher precedence than multiplication and division.
Example:
Output:
Explanation:
- Exponentiation is evaluated from right to left, so
3 ** 2
is evaluated first, which is9
. - Then,
2 ** 9
results in512
.
3. Unary Plus and Minus, Bitwise NOT +x
, -x
, ~x
Unary operations are evaluated before binary operations.
Example:
Output:
Explanation:
-5 + 3
evaluates to-2
.- Bitwise NOT of
5
is-6
due to two's complement representation.
4. Multiplication, Division, Floor Division, Modulus *
, /
, //
, %
These operators have higher precedence than addition and subtraction.
Example:
result = 4 + 3 * 2
print("Multiplication:", result)
result = 10 / 2 - 3
print("Division:", result)
result = 10 // 3 % 2
print("Floor Division and Modulus:", result)
Output:
Explanation:
3 * 2
is evaluated first, resulting in6
, then4 + 6
results in10
.10 / 2
is evaluated first, resulting in5.0
, then5.0 - 3
results in2.0
.10 // 3
results in3
, then3 % 2
results in1
.
5. Addition, Subtraction +
, -
These operators have lower precedence than multiplication and division.
Example:
Output:
Explanation:
- Addition and subtraction are evaluated from left to right, so
5 + 3
results in8
, then8 - 2
results in6
.
6. Bitwise Shift Operators <<
, >>
Bitwise shift operators shift bits to the left or right.
Example:
Output:
Explanation:
2 << 1
shifts2
left by 1 position, resulting in4
.4 >> 1
shifts4
right by 1 position, resulting in2
.
7. Bitwise AND &
Bitwise AND has lower precedence than shift operators.
Example:
Output:
Explanation:
6
(binary0110
) AND3
(binary0011
) results in2
(binary0010
).
8. Bitwise XOR ^
Bitwise XOR has the same precedence level as Bitwise AND.
Example:
Output:
Explanation:
6
(binary0110
) XOR3
(binary0011
) results in5
(binary0101
).
9. Bitwise OR |
Bitwise OR has the same precedence level as Bitwise XOR.
Example:
Output:
Explanation:
6
(binary0110
) OR3
(binary0011
) results in7
(binary0111
).
10. Comparison Operators ==
, !=
, >
, <
, >=
, <=
Comparison operators have lower precedence than arithmetic operators.
Example:
Output:
Explanation:
- Both
5 > 3
and2 < 4
areTrue
, soTrue and True
results inTrue
.
11. Boolean NOT not
The not
operator has higher precedence than and
and or
.
Example:
Output:
Explanation:
not (5 > 3)
evaluates toFalse
, soFalse and 2 < 4
results inFalse
.
12. Boolean AND and
The and
operator has higher precedence than or
.
Example:
Output:
Explanation:
True and False
isFalse
, andFalse or True
isTrue
.
13. Boolean OR or
The or
operator has the lowest precedence among boolean operators.
Example:
Output:
Explanation:
False or True
results inTrue
.
14. Assignment Operators =
, +=
, -=
, etc.
Assignment operators have the lowest precedence among operators.
Example:
Output:
Explanation:
a += 3
is evaluated asa = a + 3
, resulting in8
.
Conclusion
Understanding operator precedence in Python is crucial for writing correct expressions and avoiding unexpected results. By practicing and applying these principles, you can ensure that your code behaves as intended and maintains clarity in complex expressions.