免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2342 | 回复: 11
打印 上一主题 下一主题

True/False用于or/and的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-03-13 14:44 |只看该作者 |倒序浏览
本帖最后由 m0338005 于 2017-03-13 14:45 编辑

Hi,
Python版本是2.7.11.

Python刚开始学习,在实践中关于True False和or/and一起使用有些不懂。

比如下面同样是True,与or一起可以识一堆字符串,和and似乎就不行了。False正相反。

>>> True or dfa2e32r4
True
>>> True and dfa2e32r4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dfa2e32r4' is not defined
>>> False and dfa2e32r4
False
>>> False or dfa2e32r4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dfa2e32r4' is not defined

碰到上面问题怎么找出原因?

论坛徽章:
0
2 [报告]
发表于 2017-03-14 08:10 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。
请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

屏幕快照 2017-03-14 上午8.08.08.png (91.94 KB, 下载次数: 50)

屏幕快照 2017-03-14 上午8.08.08.png

论坛徽章:
0
3 [报告]
发表于 2017-03-14 08:10 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

论坛徽章:
0
4 [报告]
发表于 2017-03-14 08:11 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

论坛徽章:
0
5 [报告]
发表于 2017-03-14 08:11 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

论坛徽章:
0
6 [报告]
发表于 2017-03-14 08:12 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

论坛徽章:
0
7 [报告]
发表于 2017-03-14 08:12 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

论坛徽章:
0
8 [报告]
发表于 2017-03-14 08:22 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

请看
https://github.com/pythonpeixun/ ... _brother_huang_5.md

论坛徽章:
0
9 [报告]
发表于 2017-03-14 08:23 |只看该作者
True or dfa2e32r4
这个是短路计算了,所以后面的dfa2e32r4 不计算。

False or dfa2e32r4  这个因为是or  没有短路,所以要计算后面的。

True and dfa2e32r4


这个and 没有短路计算,name 'dfa2e32r4' is not defined  
你写dfa2e32r4 ,python 认为是变量,但变量没有先定义,就使用了。

论坛徽章:
0
10 [报告]
发表于 2017-03-14 08:34 |只看该作者
这个逻辑表达式,请看黄哥的文章,写得很清楚
https://github.com/pythonpeixun/article/blob/master/python/learn_python_follow_brother_huang_5.md

屏幕快照 2017-03-14 上午8.08.08.png (91.94 KB, 下载次数: 41)

屏幕快照 2017-03-14 上午8.08.08.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP