Python的布尔运算,有以下几个
- and
- or
- not
# 布尔值只有True、False两个值,
# 实际上是Int的子类,True等价于1,False等价于0
# 但是布尔运算有逻辑与and、逻辑或or、逻辑非not;优先级依次为not、and、or
# 逻辑与and,只有当所有操作为True的时候,结果为True;有一个False,结果就是False
print(1 > 0 and 3 < 5 and 6 > 3) # True
print(1 > 0 and 3 > 5 and 6 > 3) # False
# 短路特性:如果第一个操作为False,就不会往后面计算了
print(1 < 0 and 3 < 5 and 6 > 3) # False
# 逻辑或or,只要有一个操作为True的时候,结果为True;否则结果就是False
print(1 > 0 or 3 < 5 or 6 > 3) # True
print(1 < 0 or 3 > 5 or 6 < 3) # False
# 短路特性:如果第一个操作为True,就不会往后面计算了
print(1 > 0 or 3 > 5 or 6 < 3) # True
# 逻辑非not,对布尔值取反
print(not True) # 输出 False
print(not False) # 输出 True
来源链接:https://www.cnblogs.com/maoning/p/18767015
© 版权声明
本站所有资源来自于网络,仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您(转载者)自己承担!
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
THE END
暂无评论内容