python中0、1、True、False、if条件
生活随笔
收集整理的這篇文章主要介紹了
python中0、1、True、False、if条件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?總結:
無論是python還是C++,true=1,false=0
非零就是true,此說法只應用在判斷條件中
1、python中0、1、True、False
>>> print(True == 1) >>> print(True == 2) >>> print(False == 0) >>> print(False == 2) True False True False這說明1與True,0與False在python來說是完全相等的東西。
2 if條件
if 和 elif 后面的“表達式”的形式是很自由的,只要表達式有一個結果,不管這個結果是什么類型,Python 都能判斷它是“真”還是“假”。
布爾類型(bool)只有兩個值,分別是 True 和 False,Python 會把 True 當做“真”,把 False 當做“假”。
對于數字,Python 會把 0 和 0.0 當做“假”,把其它值當做“真”。
對于其它類型,當對象為空或者為 None 時,Python 會把它們當做“假”,其它情況當做真。
demo3-python 鏈式法則
print(1 == 1!=True) # False print(1==1) # True print((1 == 1)!=True) # False print(1!=True) # False print(1 == (1!=True)) # False print('--------------') print(2 == 2 != True) # True print(2==2) # True print((2 == 2)!=True) # False print(2!=True) # True print(2 == (2!=True)) # False print()?python鏈式法則:1 == 1!=True 相當于 1==1 and 1!=True
類似于a<b<c 相當于 a<b and b<c
?參考自:5.9 Comparisons
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python中0、1、True、False、if条件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: octave错误-error: ‘squ
- 下一篇: 项目案例:在线拍卖系统_冀拓公司在张家口