python布尔类型运算_Python3 布尔类型

Python3 布尔类型

阅读 (104) |

发布于 2020-05-19 11:43:24

布尔类型即 对于错、0和1、正与反。

在Python语言中,布尔类型只有两个值,True与False。

布尔值通常用来判断条件判断是否成立age = 15

if age >= 18:

print("成年人")

else:

print("未成年人")

python 内置bool()函数可以用来测试一些表达式是否成立。>>> bool("")

False

>>> bool("a")

True

>>> bool(0)

False

>>> bool(1)

True

>>> bool(0.0)

False

>>> bool(-0.0)

False

>>> bool(-0)

False

>>> bool([])

False

>>> bool({})

False

>>> bool(())

False

在python中, 0 -0 0.0 -0.0 空字符 空列表 空字典 空元组 都为False。

布尔类型可以进行and、or和not运算

and 与运算 and前后都为真时为真,and 有一项为假则为假>>> True and True

True

>>> True and False

False

>>> False and False

False

or 或运算 有且至少有一项为真则为真。>>> False or False

False

>>> False or True

True

>>> True or True

True

not 非运算>>> not True

False

>>> not False

True

空值(None)

None 不是布尔型 , 而是 NoneType, 它是一个特殊的值。 我们来看看None用bool() 来判断时:>>> bool(None)

False

脑洞一下

如果我们用布尔型的值做四则运算会怎么样呢?>>> True > False

True

>>> True < False

False

>>> True

True

>>> True > 0

True

>>> True < 0

False

>>> True - 1

0

>>> False - 1

-1

我们发现在python的四则运算中, 把True 看作1 , 而把False看作0.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值