新聞中心
Python中的函數(shù)每一個(gè)都有各自的作用,今天就bool函數(shù)為大家進(jìn)行講解。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供向陽(yáng)企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為向陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
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
以上就是Python bool函數(shù)的詳解。更多Python學(xué)習(xí)推薦:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
分享文章:創(chuàng)新互聯(lián)Python教程:Pythonbool類(lèi)型是什么?怎么用?
文章位置:http://m.5511xx.com/article/dhpciss.html


咨詢
建站咨詢
