Python 简明教程

Python Operator Precedence

Python Operator Precedence

一个表达式可能有多个操作符要评估。操作符优先级定义了评估操作符的顺序。换而言之,操作符评估的顺序由操作符优先级决定。

An expression may have multiple operators to be evaluated. The operator precedence defines the order in which operators are evaluated. In other words, the order of operator evaluation is determined by the operator precedence.

如果某个表达式包含多个运算符,其评估顺序由优先级决定。例如,考虑以下表达式:

If a certain expression contains multiple operators, their order of evaluation is determined by the order of precedence. For example, consider the following expression

>>> a = 2+3*5

此处, a 的值是什么?——是 17(先乘 3 再乘 5)还是 25(先加 2 和 3,再乘 5)?此时 Python 的操作符优先级规则就显现了。

Here, what will be the value of a? - yes it will be 17 (multiply 3 by 5 first and then add 2) or 25 (adding 2 and 3 and then multiply with 5)? Python’s operator precedence rule comes into picture here.

我们仅考虑 Python 中的算术运算符时,传统的 BODMAS 规则也被 Python 解释器所采用,即:先评估 brackets ,再评估 divisionmultiplication ,最后是 additionsubtraction 。因此,a 在上述表达式中将变为 17。

If we consider only the arithmetic operators in Python, the traditional BODMAS rule is also employed by Python interpreter, where the brackets are evaluated first, the division and multiplication operators next, followed by addition and subtraction operators. Hence, a will become 17 in the above expression.

除了操作符优先级外,操作符的结合性也很重要。如果一个表达式由优先级相同的运算符组成,结合性将决定其顺序。大多数运算符从左到右结合。也就是说,左边的运算符先于右边的运算符评估。

In addition to the operator precedence, the associativity of operators is also important. If an expression consists of operators with same level of precedence, the associativity determines the order. Most of the operators have left to right associativity. It means, the operator on the left is evaluated before the one on the right.

我们考虑另一个表达式:

Let us consider another expression:

>>> b = 10/5*4

在本例中,*(乘法)和 /(除法)两个运算符具有相同的优先级。但是,从左到右的结合性规则先执行除法(10/5 = 2),然后执行乘法(2*4 = 8)。

In this case, both * (multiplication) and / (division) operators have same level of precedence. However, the left to right associativity rule performs the division first (10/5 = 2) and then the multiplication (2*4 = 8).

Python Operator Precedence Table

以下表格列出了 Python 中所有运算符的递减优先级。Operators 列下同一单元格中的运算符具有相同的优先级。

The following table lists all the operators in Python in their decreasing order of precedence. Operators in the same cell under the Operators column have the same precedence.

Sr.No.

Operator & Description

1

(),[], {} Parentheses and braces

2

[index], [index:index] Subscription, slicing,

3

await x Await expression

4

** Exponentiation

5

+x, -x, ~x Positive, negative, bitwise NOT

6

*, @, /, //, % Multiplication, matrix multiplication, division, floor division, remainder

7

+, - Addition and subtraction

8

<<, >> Left Shifts, Right Shifts

9

& Bitwise AND

10

^ Bitwise XOR

11

*

* Bitwise OR

12

*in, not in, is, is not, <, ⇐, >, >=, !=, == * Comparisons, including membership tests and identity tests

13

not x Boolean NOT

14

and Boolean AND

15

or Boolean OR

16

if – else Conditional expression

17

lambda Lambda expression

18

Python Operator Precedence Example

a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d       #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ",  e)

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ",  e)

e = (a + b) * (c / d);    # (30) * (15/5)
print ("Value of (a + b) * (c / d) is ",  e)

e = a + (b * c) / d;      #  20 + (150/5)
print ("Value of a + (b * c) / d is ",  e)

执行以上程序时,产生以下结果−

When you execute the above program, it produces the following result −

Value of (a + b) * c / d is  90.0
Value of ((a + b) * c) / d is  90.0
Value of (a + b) * (c / d) is  90.0
Value of a + (b * c) / d is  50.0