這篇文章將為大家詳細(xì)講解有關(guān)怎么在python中對(duì)bool函數(shù)取值,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(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ì)于沒有值的字符串(也就是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ù)來判斷一個(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
關(guān)于怎么在python中對(duì)bool函數(shù)取值就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。