在python中bool函数的取值方法
作者:有一种宿命叫无能为力 时间:2021-10-06 00:47:37
bool是Boolean的缩写,只有真(True)和假(False)两种取值
bool函数只有一个参数,并根据这个参数的值返回真或者假。
1.当对数字使用bool函数时,0返回假(False),任何其他值都返回真。
>>> bool(0)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(21334)
True
2.当对字符串使用bool函数时,对于没有值的字符串(也就是None或者空字符串)返回False,否则返回True。
>>> bool('')
False
>>> bool(None)
False
>>> bool('asd')
True
>>> bool('hello')
True
3.bool函数对于空的列表,字典和元祖返回False,否则返回True。
>>> a = []
>>> bool(a)
False
>>> a.append(1)
>>> bool(a)
True
4.用bool函数来判断一个值是否已经被设置。
>>> 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
来源:https://blog.csdn.net/You_are_my_dream/article/details/52925750
标签:python,bool
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python之进行URL编码案例讲解
2023-03-31 03:46:58
![](https://img.aspxhome.com/file/2023/1/87761_0s.png)
GO语言实现文件上传代码分享
2023-07-22 10:56:33
Python实现Telnet自动连接检测密码的示例
2021-10-05 11:08:37
![](https://img.aspxhome.com/file/2023/9/112989_0s.jpg)
在python中使用[[v]*n]*n遇到的坑及解决
2022-02-03 10:31:35
![](https://img.aspxhome.com/file/2023/5/89315_0s.png)
使用Python制作表情包实现换脸功能
2022-01-14 09:10:18
![](https://img.aspxhome.com/file/2023/4/81794_0s.webp)
使用 django orm 写 exists 条件过滤实例
2022-01-20 02:42:48
pandas处理csv文件的方法步骤
2022-05-31 10:51:04
![](https://img.aspxhome.com/file/2023/3/83803_0s.png)
python实现微信每日一句自动发送给喜欢的人
2022-10-13 02:09:25
Access报错:文件共享锁定数溢出
2009-03-21 18:32:00
基于python的docx模块处理word和WPS的docx格式文件方式
2021-11-13 12:07:55
如何使用python wasmtime调用rust生成的wasm库
2023-11-26 23:58:38
注册表单之死
2008-08-07 13:02:00
![](https://img.aspxhome.com/file/UploadPic/20088/7/20088713716694s.gif)
解析Javascript中中括号“[]”的多义性
2023-09-03 09:59:35
Python流程控制语句详解
2022-03-01 22:36:37
![](https://img.aspxhome.com/file/2023/0/92370_0s.jpg)
Pytorch 实现自定义参数层的例子
2023-01-27 22:00:06
PHP获取url的函数代码
2023-10-15 12:45:00
不拘小节的中文字体设计
2009-05-21 10:44:00
asp如何显示最后十名来访者信息?
2010-06-09 18:45:00
Pytorch Tensor基本数学运算详解
2022-12-09 03:26:50
python实现ip查询示例
2021-07-07 20:04:32