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

Python如何在bool函數(shù)中取值

 更新時(shí)間:2020年09月21日 09:16:25   作者:python學(xué)習(xí)者0  
這篇文章主要介紹了Python如何在bool函數(shù)中取值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

bool是Boolean的縮寫(xiě),只有真(True)和假(False)兩種取值

bool函數(shù)只有一個(gè)參數(shù),并根據(jù)這個(gè)參數(shù)的值返回真或者假。

1.當(dāng)對(duì)數(shù)字使用bool函數(shù)時(shí),0返回假(False),任何其他值都返回真。

>>> bool(0)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(21334)
True

2.當(dāng)對(duì)字符串使用bool函數(shù)時(shí),對(duì)于沒(méi)有值的字符串(也就是None或者空字符串)返回False,否則返回True。

>>> bool('')
False
>>> bool(None)
False
>>> bool('asd')
True
>>> bool('hello')
True

3.bool函數(shù)對(duì)于空的列表,字典和元祖返回False,否則返回True。

>>> a = []
>>> bool(a)
False
>>> a.append(1)
>>> bool(a)
True

4.用bool函數(shù)來(lái)判斷一個(gè)值是否已經(jīng)被設(shè)置。

>>> x = raw_input('Please enter a number :')
Please enter a number :
>>> bool(x.strip())
False
>>> x = raw_input('Please enter a number :')
Please enter a number :4
>>> bool(x.strip())
True

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論