python進階

5、and 和 or

and返回第一個假值,如果都爲真,返回最後一個真值。

or返回第一個真值,如果都爲假,返回最後一個假值。

複製代碼
>>> a = "first"
>>> b = "second"
>>> 1 and a or b 1
'first'
>>> 0 and a or b 2
'second'
複製代碼
類似於: bool ? a : b

安全使用:

>>> a = ""
>>> b = "second"
>>> (1 and [a] or [b])[0] 1

python http服務器 :
https://blog.csdn.net/mrbcy/article/details/72630201


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章