expression_list |
::= |
expression ( ","
expression )* [","] |
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
一个表达式表是一个包括至少一个逗号的元组, 它的长是表中表达式的个数.其中表达式从左到右按顺序计算.
The trailing comma is required only to create a single tuple
(a.k.a. a singleton); it is optional in all other cases. A
single expression without a trailing comma doesn't create a tuple,
but rather yields the value of that expression. (To create an empty
tuple, use an empty pair of parentheses: ().)
最后的逗号仅仅在创建单元素元组(又称为"独元")时才需要; 在其它情况下, 它是可选的. 一个没有后缀逗号的表达式不会创建元组, 但仍会计算该表达式的值.(可以使用一对空括号() 创建一个空元组).
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:
expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
func(expr1, expr2, *expr3, **expr4)
expr3, expr4 = expr1, expr2
The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right -- see section 5.9 - and exponentiation, which groups from right to left).
下表总结了Python中运算符的优先级, 从低优先级(弱捆绑)到高优先级(强捆绑), 在同一格子中的运算符具有相同的优先级.如果没有特殊的句法指定, 运算符是二元的. 同一格子内的运算符都从左至右结合(比较运算符是个例外, 它们可以从左到右连接起来 -- 见上文, 并且幂运算符也是从左至右结合).
| Operator | Description |
|---|---|
| lambda | Lambda表达式 Lambda expression |
| or | 布尔OR Boolean OR |
| and | 布尔AND Boolean AND |
| not x | 布尔NOT Boolean NOT |
| in, not in | 成员测试 Membership tests |
| is, is not | 标识测试 Identity tests |
<,
<=, >, >=,
<>, !=, == |
比较 Comparisons |
| |
比特级OR Bitwise OR |
^ |
比特级XOR Bitwise XOR |
& |
比特级AND Bitwise AND |
<<,
>> |
移位 Shifts |
+,
- |
加减 Addition and subtraction |
*,
/, % |
乘,除,取余 Multiplication, division, remainder |
+x,
-x |
正运算,负运算 Positive, negative |
~x |
比特级not Bitwise not |
** |
幂 Exponentiation |
x.attribute |
属性引用 Attribute reference |
x[index] |
下标 Subscription |
x[index:index] |
片断 Slicing |
f(arguments...) |
函数调用 Function call |
(expressions...) |
表达式分组或元组 Binding or tuple display |
[expressions...] |
列表 List display |
{key:datum...} |
字典 Dictionary display |
`expressions...` |
串转换 String conversion |