鱼C论坛

 找回密码
 立即注册
查看: 6107|回复: 4

Python的"&"与'"and"的关系,逻辑运算结果不一致

[复制链接]
发表于 2015-12-19 19:59:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 落殇飞羽 于 2015-12-19 21:05 编辑
  1. x = 1;y = 2;z = 3
  2. >>> x < y <z
  3. True
  4. >>> x < y <z & (x < y and y < z)
  5. False
  6. >>> x < y < z & (x < y and y < z)
  7. False
  8. >>> x < y < z & (x < y and y < z)
  9. False
  10. >>> x < y and y < z
  11. True
复制代码

为什么会出现下面代码的输出结果?为什么“&”与“and”结果会不一样,而且是左右双方都为“True“的时候
  1. >>> x < y & y < z
  2. True
  3. >>> x < y <z & (x < y and y < z)
  4. False
  5. >>> x < y <z and (x < y and y < z)
  6. True
  7. >>> x < y <z and (x < y & y < z)
  8. True
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-12-19 20:55:47 | 显示全部楼层
本帖最后由 落殇飞羽 于 2015-12-19 20:59 编辑
~风介~ 发表于 2015-12-19 20:14
楼主自己对比一下~


谢谢 @~风介~
我去查了一下帮助文档:”The priorities of the binary bitwise operations are all lower than the numeric operations and higher than the comparisons; the unary operation ~ has the same priority as the other unary numeric operations (+ and -).
其实是这段代码先运行了( z & (x < y & y < z),输出为1),造成整体运行错误,只需要加前面一个括号就ok了,代码如下:
  1. >>> x < y <z & (x < y & y < z)
  2. False
  3. >>> (x < y <z )& (x < y & y < z)
  4. True
  5. >>> z & (x < y & y < z)
  6. 1
  7. >>> y <z & (x < y & y < z)
  8. False
复制代码

评分

参与人数 1荣誉 +3 鱼币 +5 收起 理由
~风介~ + 3 + 5 自力更生!:)

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-19 20:00:36 | 显示全部楼层
请各位鱼油赐教!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-19 20:14:03 | 显示全部楼层
楼主自己对比一下~
2.png 1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-22 10:10:07 | 显示全部楼层
顶一下,支持一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 22:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表