Python operator precedence
Python operator precedence describes the order in which operations are performed when a computer evaluates an expression. Operations with higher priority are performed first, and then operations with lower priority are performed. For example, we often say that multiplication and division are performed first, and then addition and subtraction operations are performed.
Priority | Operator | Description | |
---|---|---|---|
1 | lambda | Lambda expression | |
2 | or | Boolean or | Boolean NOT |
5 | in, not in | Membership test | |
6 | is, is not | Identity test | |
7 | <, <=, >, >=, !=, == | Comparison | |
8 | | | Bitwise OR | |
9 | ^ | Bitwise XOR | |
10 | & | Bitwise AND | |
11 | <<,>> | Shift | |
12 | +,- | Addition and subtraction | |
13 | *,/,% | Multiplication, division and remainder | |
14 | +x,-x | Sign | |
15 | ~x | Bitwise flip | |
16 | ** | Exponent | |
17 | x.attribute | Attribute reference | |
18 | x[index] | Subscript | |
19 | x[index:index] | Addressing segment | |
20 | f(arguments...) | Function call | |
21 | (experession,...) | Binding or tuple display | |
22 | [expression,...] | List display | |
23 | {key:datum,...} | Dictionary display | |
24 | 'expression,...' | String conversion |