加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 使用-1的逻辑OR运算

发布时间:2020-12-20 13:04:05 所属栏目:Python 来源:网络整理
导读:为什么我在 python中尝试的以下逻辑操作的输出不同? -1 or 1 1 or -1 First返回-1,第二个返回1 解决方法 而且或者都是懒惰的;它们会评估操作数,直到它们可以决定结果(并在第一个False操作数处停止;或者在第一个True操作数处停止).它们返回评估的最后一个操
为什么我在 python中尝试的以下逻辑操作的输出不同?

-1 or 1
 1 or -1

First返回-1,第二个返回1

解决方法

而且或者都是懒惰的;它们会评估操作数,直到它们可以决定结果(并在第一个False操作数处停止;或者在第一个True操作数处停止).它们返回评估的最后一个操作数,如 the documentation中所述:

Note that neither and nor or restrict the value and type they return to False and True,but rather return the last evaluated argument. This is sometimes useful,e.g.,if s is a string that should be replaced by a default value if it is empty,the expression s or 'foo' yields the desired value.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读