欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python關鍵字and和or用法實例

 更新時間:2015年05月28日 16:35:13   投稿:junjie  
這篇文章主要介紹了python關鍵字and和or用法實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下

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為假時,則出現(xiàn)問題
'second'
>>>(1and[a]or[b])[0]# 安全用法,因為[a]不可能為假,至少有一個元素
''
>>>

相關文章

最新評論