Python 逻辑操作符 and 和 or 的比较

在Python中,空字符串为假,非空字符串为真。非零的数为真

and操作符

只要左边的表达式为真,整个表达式返回的值是右边表达式的值,否则,返回左边表达式的值

or操作符

只要两边的表达式为真,整个表达式的结果是左边表达式的值。
如果是一真一假,返回真值表达式的值
如果两个都是假,比如空值和0,返回的是右边的值。(空值或0)

举例:

test1 = 12
test2 = 0
test3 = ''
test4 = "First"
print test1 and test3   #result = ''
print test3 and test1   #result = ''
print test1 and test4   #result = "First"
print test4 and test1   #result = 12
print test1 or test2    #result = 12
print test1 or test3    #result = 12
print test3 or test4    #result = "First"
print test2 or test4    #result = "First"
print test1 or test4    #result = 12
print test4 or test1    #result = "First"
print test2 or test3    #result = ''
print test3 or test2    #result = 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值