pyhton 中的not and or 运用

先上代码

a = 10
b = 20

result_AandB = a and b
result_AorB = a or b
result_not_AandB = not(a and b)
result_not_AorB = not(a or b)
result_BandA = b and a 
result_BorA = b or a

print('a and b:'+str(result_AandB))     # a and b 结果为20
print('a or b:'+str(result_AorB))       # a or b 结果为10
print('b and a:'+str(result_BandA))     # b and a 结果为10
print('b or a:'+str(result_BorA))       # b or a  结果为20
print('not(a and b):'+str(result_not_AandB))    # not(a and b) 结果为fasle
print('not(a or b):'+str(result_not_AorB))      # not(a or b) 结果为false

pyhon也允许其他类型的对象进行 and or not 运算,所以逻辑运算的结果也可能时其他类型

message = "cat"
number = 1024
print(bool(message)) # True          非空的字符串为真
print(bool(number)) # True           非零的数字为真
print(message and number) # 1024     message为真,所以结果为number
print(message or number) # cat       message为真,所以结果为message
print(not message) # false          mewssage为真,所以结果为false
print(not number) # false            number为真,所以结果为False

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值