If you have an expression like 2 + 3 * 4, do you do the addition first, or do the multiplication first? Our high school math tells us that we should do multiplication first-which means that the multiplication operator takes precedence over the addition operator.
The following table gives the operator precedence for Python, from the lowest priority (the most loosely combined) to the highest priority (the most tightly coupled). This means that in an expression, Python first calculates the following operator in the table, and then computes the operator that is listed in the upper part of the table.
The following table (which is identical to the one in the Python reference manual) has taken into account the full need. In fact, I recommend that you use parentheses to group operators and operands so that you can clearly indicate the order of operations and make the program as easy to read as possible. For example, 2 + (3 * 4) is clearly clearer than 2 + 3 * 4. At the same time, parentheses should also be used correctly, and should not be used excessively (e.g. 2 + (3 + 4)).
table 1-2 Operator Precedence
operator |
Description |
Lambda |
Lambda expression |
Or |
Boolean "or" |
and |
Boolean "and" |
Not X |
Boolean "Non" |
In,not in |
Member Test |
Is,is not |
Identity testing |
<,<=,>,>=,!=,== |
Comparison |
| |
Bitwise OR |
^ |
Bitwise XOR OR |
& |
Bitwise-AND |
<<,>> |
Shift |
+,- |
Addition and subtraction |
*,/,% |
Multiplication, division and redundancy |
+x,-x |
PLUS sign |
~x |
Rollover by bit |
** |
Index |
X.attribute |
Property Reference |
X[index] |
Subscript |
X[index:index] |
Addressing segment |
F (Arguments ...) |
Function call |
(Experession,...) |
Binding or tuple display |
[Expression,...] |
List display |
{Key:datum,...} |
Dictionary display |
' Expression,... ' |
String conversions |
Calculation Order
By default, the operator precedence table determines which operator is evaluated before another operator. However, if you want to change the order in which they are calculated, you have to use parentheses. For example, if you want the addition to be evaluated before multiplication in an expression, then you have to write something like (2 + 3) * 4.
Binding law
Operators are usually left-to-right, i.e. operators with the same precedence are evaluated in left-to-right order. For example, 2 + 3 + 4 is calculated as (2 + 3) + 4. Some operators, such as the assignment operator, are combined right-to-left, that is, a = b = c is processed to a = (b = c).
Tip: Using parentheses to enhance the readability of your code is a good idea in many situations, and without parentheses, you can make your program get incorrect results or reduce the readability of your code, causing the reader to be confused. Parentheses do not have to exist in the Python language, but it is always worthwhile to use parentheses for readability. Anyone who maintains your code will thank you, and you will thank yourself when you read your code again.
Python operator Precedence