Python - Operators Precedence
Python Operators Precedence
Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.
For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.
Operator Associativity
Operator associativity is the direction in which an expression is evaluated. For example:
x = 10 y = 20 #associativity of = operator is #Left to Right, hence x will become 20 x = y
As the associativity of = is left to right. Hence x is assigned the value of y.
Example:
The example below illustrates the operator precedence in Python.
#evaluates 5 * 2 first retval1 = 15 - 5 * 2 #above expression is equivalent to retval2 = 15 - (5 * 2) #forcing compiler to evaluate 15 - 5 first retval3 = (15 - 5) * 2 print("15 - 5 * 2 = ", retval1) print("15 - (5 * 2) = ", retval2) print("(15 - 5) * 2 = ", retval3)
The output of the above code will be:
15 - 5 * 2 = 5 15 - (5 * 2) = 5 (15 - 5) * 2 = 20
Python Operators Precedence Table
The following table lists the precedence and associativity of Python operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.
Operators in the same group have associativity from left to right, except exponentiation, which has associativity from right to left.
Precedence | Operator | Description |
---|---|---|
18 | (expressions...) | Binding or parenthesized expression |
[expressions...] | List display | |
{key: value...} | Dictionary display | |
{expressions...} | Set display | |
17 | x[index] | Subscription |
x[index:index] | Slicing | |
x(arguments...) | Call | |
x.attribute | Attribute reference | |
16 | await x | Await expression |
15 | ** | Exponentiation |
14 | +x -x | Unary plus, Unary negation |
~ | Bitwise NOT | |
13 | * / // % | Multiplication, Division, Floor division, Remainder |
@ | Matrix multiplication | |
12 | + - | Addition, Subtraction |
11 | << >> | Bitwise left shift, right shift |
10 | & | Bitwise AND |
9 | ^ | Bitwise XOR |
8 | | | Bitwise OR |
7 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal |
is | Identity operators | |
is not | ||
in | Membership operators | |
not in | ||
6 | not x | Boolean NOT |
5 | and | Boolean AND |
4 | or | Boolean OR |
3 | if-else | Conditional expression |
2 | lambda | Lambda expression |
1 | = | Direct assignment |
+= -= *= /= //= %= **= | Compound assignment by addition, subtraction, multiplication, division, floor division, remainder, and exponentiation | |
<<= >>= | Compound assignment by Bitwise left shift, right shift | |
&= ^= |= | Compound assignment by Bitwise AND, XOR and OR |
❮ Python - Operators