发布时间:2019-08-28 09:07:53编辑:auto阅读(1855)
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
48866
47935
38710
35859
30285
27039
26072
20905
20700
19067
503°
597°
608°
611°
588°
573°
639°
714°
831°
929°