运算符 | 优先级 |
postfix |
|
unary |
|
multiplicative |
|
additive |
|
shift |
|
relational |
|
equality |
|
bitwise AND |
|
bitwise exclusive OR |
|
bitwise inclusive OR | ` |
logical AND |
|
logical OR | ` |
ternary |
|
assignment | `= += -= *= /= %= &= ^= |
Precedence | Operator | Description | Associativity |
1 |
| Scope resolution | Left-to-right |
2 |
| Suffix/postfix increment and decrement | |
| Function call | ||
| Array subscripting | ||
| Element selection by reference | ||
| Element selection through pointer | ||
3 |
| Prefix increment and decrement | Right-to-left |
| Unary plus and minus | ||
| Logical NOT and bitwise NOT | ||
| Type cast | ||
| Indirection (dereference) | ||
| Address-of | ||
| Size-of | ||
| Dynamic memory allocation | ||
| Dynamic memory deallocation | ||
4 |
| Pointer to member | Left-to-right |
5 |
| Multiplication, division, and remainder | |
6 |
| Addition and subtraction | |
7 |
| Bitwise left shift and right shift | |
8 |
| For relational operators < and ≤ respectively | |
| For relational operators > and ≥ respectively | ||
9 |
| For relational = and ≠ respectively | |
10 |
| Bitwise AND | |
11 |
| Bitwise XOR (exclusive or) | |
12 | ` | ` | Bitwise OR (inclusive or) |
13 |
| Logical AND | |
14 | ` | ` | |
15 |
| Ternary conditional | Right-to-Left |
16 |
| Direct assignment (provided by default for C++ classes) | |
| Assignment by sum and difference | ||
| Assignment by product, quotient, and remainder | ||
| Assignment by bitwise left shift and right shift | ||
| =` | Assignment by bitwise AND, XOR, and OR | |
17 |
| Throw operator (for exceptions) | |
18 |
| Comma | Left-to-right |
Precedence | Operator | Description | Associativity |
1 |
| Suffix/postfix increment and decrement | Left-to-right |
| Function call | ||
| Array subscripting | ||
| Structure and union member access | ||
| Structure and union member access through pointer | ||
| Compound literal(C99) | ||
2 |
| Prefix increment and decrement | Right-to-left |
| Unary plus and minus | ||
| Logical NOT and bitwise NOT | ||
| Type cast | ||
| Indirection (dereference) | ||
| Address-of | ||
| Size-of | ||
| Alignment requirement(C11) | ||
3 |
| Multiplication, division, and remainder | Left-to-right |
4 |
| Addition and subtraction | |
5 |
| Bitwise left shift and right shift | |
6 |
| For relational operators < and ≤ respectively | |
| For relational operators > and ≥ respectively | ||
7 |
| For relational = and ≠ respectively | |
8 |
| Bitwise AND | |
9 |
| Bitwise XOR (exclusive or) | |
10 | ` | ` | Bitwise OR (inclusive or) |
11 |
| Logical AND | |
12 | ` | ` | |
13 |
| Ternary conditional | Right-to-Left |
14 |
| Simple assignment | |
| Assignment by sum and difference | ||
| Assignment by product, quotient, and remainder | ||
| Assignment by bitwise left shift and right shift | ||
| =` | Assignment by bitwise AND, XOR, and OR | |
15 |
| Comma | Left-to-right |
结合方向 | 运算符 | 附加信息 |
非结合 |
| clone 和 new |
左 |
| array() |
非结合 |
| 递增/递减运算符 |
非结合 |
| 类型 |
非结合 |
| 类型 |
右结合 |
| 逻辑操作符 |
左 |
| 算术运算符 |
左 |
| 算术运算符 和 字符串运算符 |
左 |
| 位运算符 |
非结合 |
| 比较运算符 |
非结合 |
| 比较运算符 |
左 |
| 位运算符 和 引用 |
左 |
| 位运算符 |
左 | ` | ` |
左 |
| 逻辑运算符 |
左 | ` | |
左 |
| 三元运算符 |
右 | `= += -= *= /= .= %= &= | = ^= <<= >>=` |
左 |
| 逻辑运算符 |
左 |
| 逻辑运算符 |
左 |
| 逻辑运算符 |
左 |
| 多处用到 |
这个表给出Python的运算符优先级(从低到高).
从最低的优先级(最松散地结合)到最高的优先级(最紧密地结合)。
这意味着在一个表达式中,Python会首先计算表中较下面的运算符,然后在计算列在表上部的运算符。
运算符 | 描述 |
| Lambda表达式 |
| 布尔“或” |
| 布尔“与” |
| 布尔“非” |
| 成员测试 |
| 同一性测试 |
| 比较 |
` | ` |
| 按位异或 |
| 按位与 |
| 移位 |
| 加法与减法 |
| 乘法、除法与取余 |
| 正负号 |
| 按位翻转 |
| 指数 |
| 属性参考 |
| 下标 |
| 寻址段 |
| 函数调用 |
| 绑定或元组显示 |
| 列表显示 |
| 字典显示 |
| 字符串转换 |