python运算符优先级总结

  • 官方(从小到大)
OperatorDescription
lambdaLambda expression
ifelseConditional expression
orBoolean OR
andBoolean AND
not xBoolean NOT
in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons, including membership tests and identity tests
|Bitwise OR
^Bitwise XOR
&Bitwise AND
<<, >>Shifts
+, -Addition and subtraction
*, @, /, //, %Multiplication, matrix multiplication, division, floor division, remainder 5
+x, -x, ~xPositive, negative, bitwise NOT
**Exponentiation 6
await xAwait expression
x[index], x[index:index], x(arguments...), x.attributeSubscription, slicing, call, attribute reference
(expressions...), [expressions...], {key: value...}, {expressions...}Binding or tuple display, list display, dictionary display, set display
  • 自己查找和总结(从大到小)
运算符描述
**指数 (最高优先级)
~ + -按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % //乘,除,取模和取整除
+ -加法减法
>> <<右移,左移运算符
&位 ‘AND’
^ |位运算符
<= < > >=比较运算符
<> == !=等于运算符
= %= /= //= -= += *= **=赋值运算符
is is not身份运算符
in not in成员运算符
not and or逻辑运算符
运算符逻辑表达式描述实例
andx and y布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。(a and b) 返回 20。
orx or y布尔"或" - 如果 x 是 True,它返回 x 的值,否则它返回 y 的计算值。(a or b) 返回 10。
notnot x布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。not(a and b) 返回 False
  • 注意:
  • 优先级(一般)

    一元运算符 ( ** > ~,+,- ) >

    算数运算符 ( *,/,%,// > +,- ) >

    位运算符 ( >>,<< > & > ^,| ) >

    比较运算符 ( <=, <, >, >= > <>, ==, !=) >

    赋值运算符 ( =, %=, /=, //=, -=, +=, *=, **= ) >

    身份运算符 ( is, is not ) >

    成员运算符 ( in, not in ) >

    逻辑运算符 ( not > and > or )

  • 优先级( not, and, or )

    1. not > and > or

    2. and : True and xx 返回 xx, False and xx 返回 False ;

      or : xx1 or xx2 从前往后数, 哪个第一个为True 就返回那个 ;

      not : 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。

  • <> !=, 在3.0及以上版本中已经不能用

  • is 与 == 区别

    is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。

  • 也许你是想判断x是否为None,但是却把x==[]的情况也判断进来了,此种情况下将无法区分。
    对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

  • 而对于if x is not Noneif not x is None写法,很明显前者更清晰,而后者有可能使读者误解为if (not x) is None,因此推荐前者,同时这也是谷歌推荐的风格

  • 特殊用法

    • =的特殊用法
    a = 6 > 4 and 1 < 1 or 2 > 1
    b = 3 > 1 and 0
    print('a ==>', a)
    print('b ==>', b)
    # ------------ 结果 ------------- #
    a ==> True
    b ==> 0
    
    • if else+
    a = 5
    b = 2
    c = a + 1 if b == 2 else a
    d = a + 1 if b != 2 else a
    print('c ==>', c)
    print('d ==>', d)
    # ------------ 结果 ------------- #
    c ==> 6
    d ==> 5
    
    • 字符串之间的比较

    结论:
    1. 字符串None比时间和日期字符串都要大
    2. 日期字符串之间可以直接比较大小
    3. 时间字符串之间可以直接比较大小

    # 1.时间
    def test(param):
        if '09:00:00' < param < '18:00:00':
            print(param, 'ok')
        else:
            print(param, 'no')
    a = '08:59:59'
    b = '13:00:00'
    c = '18:00:01'
    test(a)
    test(b)
    test(c)
    # ------------ 结果 ------------- #
    08:59:59 no
    13:00:00 ok
    18:00:01 no
            
    # 2.日期
    a = '2020-11-17'
    b = '2020-11-16'
    c = a if a and a < b else b
    print(c)
    # ------------ 结果 ------------- #
    2020-11-16
    
    # 3.None与时间和比较(字符串None比时间和日期字符串都要大)
    a = 'None'
    b = '16:23:16'
    c = '2021-01-22'
    d = '2020-06-23'
    print('None-b', max(a, b))
    print('None-c', max(a, c))
    print('None-d', max(a, d))
    # ------------ 结果 ------------- #
    None-b None
    None-c None
    None-d None
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值