涅牵 发表于 昨天 12:05

Python学习之布尔运算

Python的布尔运算,有以下几个

[*]and
[*]or
[*]not
# 布尔值只有True、False两个值,
# 实际上是Int的子类,True等价于1,False等价于0
# 但是布尔运算有逻辑与and、逻辑或or、逻辑非not;优先级依次为not、and、or

# 逻辑与and,只有当所有操作为True的时候,结果为True;有一个False,结果就是False
print(1 > 0and 3 < 5 and 6 > 3) # True
print(1 > 0and 3 > 5 and 6 > 3) # False

# 短路特性:如果第一个操作为False,就不会往后面计算了
print(1 < 0and 3 < 5 and 6 > 3) # False

# 逻辑或or,只要有一个操作为True的时候,结果为True;否则结果就是False
print(1 > 0or 3 < 5 or 6 > 3) # True
print(1 < 0or 3 > 5 or 6 < 3) # False
# 短路特性:如果第一个操作为True,就不会往后面计算了
print(1 > 0or 3 > 5 or 6 < 3) # True

# 逻辑非not,对布尔值取反
print(not True)   # 输出 False
print(not False)# 输出 True
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Python学习之布尔运算