junjie 通过本文主要向大家介绍了python中的or,python的or和and,python关键字,python关键词提取,python 关键字参数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值。
or也是从左到有计算表达式,返回第一个为真的值。
IDLE 1.2.4
>>>'a'and'b'
'b'
>>>''and'b'
''
>>>'a'or'b'
'a'
>>>''or'b'
'b'
</div>
类似三目表达式的用法: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]不可能为假,至少有一个元素
''
>>>
</div>

