发布时间:2019-08-28 09:07:53编辑:auto阅读(2000)
From 《dive into python》
python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值。
or也是从左到有计算表达式,返回第一个为真的值。
IDLE 1.2.4
>>>'a'and'b'
'b'
>>>''and'b'
''
>>>'a'or'b'
'a'
>>>''or'b'
'b'
类似三目表达式的用法:bool? a : b
>>> a ='first'
>>> b ='second'
>>>1and a or b # 等价于 bool = true时的情况
'first'
>>>0and a or b # 等价于 bool = false时的情况
'second'
>>> a =''
>>>1and a or b # a为假时,则出现问题
'second'
>>>(1and[a]or[b])[0]# 安全用法,因为[a]不可能为假,至少有一个元素
''
>>>
上一篇: Python 方法
下一篇: python hash
50477
49773
40364
37393
31814
28670
27606
22391
22388
20699
457°
1075°
874°
811°
1054°
930°
1547°
2889°
2586°
1985°