本文摘自php中文网,作者silencement,侵删。

bool是Boolean的缩写,只有真(True)和假(False)两种取值
bool函数只有一个参数,并根据这个参数的值返回真或者假。
1.当对数字使用bool函数时,0返回假(False),任何其他值都返回真。
1 2 3 4 5 6 7 8 | >>> bool(0)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(21334)
True
|
2.当对字符串使用bool函数时,对于没有值的字符串(也就是None或者空字符串)返回False,否则返回True。
1 2 3 4 5 6 7 8 | >>> bool('')
False
>>> bool(None)
False
>>> bool('asd')
True
>>> bool('hello')
True
|
3.bool函数对于空的列表,字典和元祖返回False,否则返回True。
1 2 3 4 5 6 | >>> a = []
>>> bool(a)
False
>>> a.append(1)
>>> bool(a)
True
|
4.用bool函数来判断一个值是否已经被设置。
1 2 3 4 5 6 7 8 | >>> 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是什么意思的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
linux如何安装Python
Python怎么粘贴代码
Python变量类型 -元组的实际运用与意义
Python爬取文章实例教程
Python函数中的可变参数定义方法以及参数的传递方法解析
为什么黑客都使用Python
Python如何编写阶乘?
详解Python使用回溯法子集树模板解决迷宫问题
Python中knn算法(k-近邻算法)的详细介绍(附示例)
pandas 实现将重复表格去重,并重新转换为表格
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中的bool是什么意思