Python运算符优先级

时间:2022-04-29 17:25:29

From Python documentation on operator precedence (Section 5.15)

Highest precedence at top, lowest at bottom.
Operators in the same box evaluate left to right.

 

Operator

Description
() Parentheses (grouping)
f(args...) Function call
x[index:index] Slicing
x[index] Subscription
x.attribute Attribute reference
** Exponentiation
~x Bitwise not
+x, -x Positive, negative
*, /, % Multiplication, division, remainder
+, - Addition, subtraction
<<, >> Bitwise shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
in, not in, is, is not, <, <=,  >,  >=,
<>, !=, ==
Comparisons, membership, identity
not x Boolean NOT
and Boolean AND
or Boolean OR
lambda Lambda expression