gpt4 book ai didi

python - 无法理解 Python 输出的原因

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:11 26 4
gpt4 key购买 nike

我刚刚开始学习 Python。我遇到了以下代码

if 0:

  print "And now for something completely different..."

else:

  print "What's all this, then?"

我知道输出应该是 What's all this, then? 但我似乎找不到合适的解释。希望有人能让我明白这一点。

谢谢。

最佳答案

0 在 bool 上下文中是 False。同样适用于空字符串、列表等。

来自python docs :

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the __nonzero__() special method for a way to change this.)

关于python - 无法理解 Python 输出的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4890172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com