python笔记 多个字符串逻辑运算

python笔记 多个字符串逻辑运算

python用一个字符串变量和多个字符串比较的时候,不能用逻辑运算符直接连接几个字符串,例如

>>> str = 'abc'
>>> str == ('abcd' or 'abc')
False
>>> str = 'abc'
>>> str == ('abc' or 'abcd')
True

应将每个判断分开写

>>> str == 'abc' or  str == 'abcd'
True

这是因为,多个字符串进行逻辑or运算,返回第一个字符串,多个字符串进行逻辑and运算,返回最后一个字符串。

>>> str == 'abc' or  str == 'abcd'
True
>>> 'a' or 'b' or 'c'
'a'
>>> 'a' and 'b' and 'c'
'c'

如果逻辑运算式中有False, True, None等,输出与他们所在的位置有关,比较奇怪

>>> False or 'a' or 'b' or 'c'
'a'
>>> True or 'a' or 'b' or 'c'
True
'a' and 'b' and 'c' and True
True
'a' and 'b' and 'c' and False
False
>>> False and 'a' and 'b' and 'c'
False
>>> True and 'a' and 'b' and 'c'
'c'
>>> None or 'a' or 'b' or 'c'
'a'
>>> None and 'a' and 'b' and 'c'
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值