python - 关于 Python 中的逻辑与的混淆
问题描述
以下是 Python 中的玩具示例:
a = 2
b= 10
result = a<b and print("Hello")
print(bool(result))
输出是:
Hello
False
为什么是输出False
而不是True
?由于result
评估为a<b= 2<10 = True
then,我们有result = True and print()
= True and True
= True
。有人可以解释这个答案的原因吗?
解决方案
print
返回None
:
>>> print("Hello") is None
True
并且None
是一个False
y 值:
>>> bool(None)
False
所以你有True and False
,评估为False
。
推荐阅读
- python-3.x - 我可以订阅 on_subscribe 回调吗?
- azure - graph api 1.0 创建事件团队日历访问被拒绝
- angular - 如何在@ngrx/data 中对 EntityCollectionServiceBase 进行单元测试?
- c# - 如何在 VS CODE 中使用“dotnet run”命令运行当前打开的文件?
- python - 朴素贝叶斯 MultinomialNB ValueError:维度不匹配
- azure - 流量管理器 Azure 端点公共 IP 地址
- angular - HttpInterceptor 未调用全局错误处理程序
- python - 列表元素的枚举python
- android - Firebase ML Kit 人脸检测,检索实例 ID 失败
- c# - C# 正则表达式返回重复的组