- 官方(从小到大)
Operator | Description |
---|---|
lambda | Lambda expression |
if – else | Conditional expression |
or | Boolean OR |
and | Boolean AND |
not x | Boolean 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 , ~x | Positive, negative, bitwise NOT |
** | Exponentiation 6 |
await x | Await expression |
x[index] , x[index:index] , x(arguments...) , x.attribute | Subscription, 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 | 逻辑运算符 |
运算符 | 逻辑表达式 | 描述 | 实例 |
---|---|---|---|
and | x and y | 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 | (a and b) 返回 20。 |
or | x or y | 布尔"或" - 如果 x 是 True,它返回 x 的值,否则它返回 y 的计算值。 | (a or b) 返回 10。 |
not | not x | 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 | not(a and b) 返回 False |
- 注意:
优先级(一般):
一元运算符 ( ** > ~,+,- ) >
算数运算符 ( *,/,%,// > +,- ) >
位运算符 ( >>,<< > & > ^,| ) >
比较运算符 ( <=, <, >, >= > <>, ==, !=) >
赋值运算符 ( =, %=, /=, //=, -=, +=, *=, **= ) >
身份运算符 ( is, is not ) >
成员运算符 ( in, not in ) >
逻辑运算符 ( not > and > or )
优先级( not, and, or ):
not > and > or
and : True and xx 返回 xx, False and xx 返回 False ;
or :
xx1
orxx2
从前往后数, 哪个第一个为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 None
和if 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