Python_表达式的优先级

一.表达式的优先级

表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列

代码段

a = 1
b = 2
c = 3
print("表达式计算结果是:",a or b and c)

  结果输出

表达式计算结果是: 1

 会优先计算 and,取值3,后面计算or,最后结果为1

运算符

描述

**

指数 (最高优先级)

~ + -

按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)

* / % //

乘,除,取模和取整除

+ -

加法减法

>> <<

右移,左移运算符

&

位 'AND'

^ |

位运算符

<= < > >=

比较运算符

<> == !=

等于运算符

= %= /= //= -= += *= **=

赋值运算符

is is not

身份运算符

in not in

成员运算符

not  and or

逻辑运算符

 实例

a = 1
b = 2
c = 2
print(not a or b+2 == c)  # 执行优先级 (not a) or ((b+2)== c)
print(b+2 == c)
print( not a)

  输出结果

False
False
False

逻辑运算符优先级 not>and>or

猜你喜欢

转载自www.cnblogs.com/joy-sir/p/12324400.html
今日推荐