为什么我在Python控制台中获得了Nones元组的印刷,而不是单个元组?

发布于 2025-02-12 23:55:13 字数 296 浏览 3 评论 0原文

当使用Python(通过CMD)并在内部写下此内容时:

>>> import random
>>> print("hello"),print("world"),print(random.randint(5,10))

我得到的输出是:

hello
world
8
(None, None, None)

现在我不确定解释器为什么返回none的元组,而不是单个

While using Python (via cmd) and writing this inside:

>>> import random
>>> print("hello"),print("world"),print(random.randint(5,10))

the output I'm getting is:

hello
world
8
(None, None, None)

Now I'm not sure why the interpreter returns the tuple of None's, but not a single None.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2025-02-19 23:55:13

python在第二个输入行中解释是从print的返回值中创建元组,这些全部none。只需对每个打印语句都有一行。
这是此行为的另一个示例:

>>> 5,4,3

返回

(5,4,3)

[更新以解决您的评论]

因为Python的解释a,b, c元组(a,b,c),so print(),print(),print()等同于tuple(print(),print(),print())
如果您在行行时进行IT IT,则不会创建Tuple,而Python只是执行print()语句。
现在是实际上使您感到困惑的重点。调用print()也将返回,但这会自动隐藏,因为这是正常行为。 的元组无 s,但是不是自动隐藏的,因为它不是none

Python is interpreting the , in the second input line as creation of a tuple out of the return values of print which are all None. Just have a single line for every print statement.
Here is another example of this behavior:

>>> 5,4,3

returns

(5, 4, 3)

[Update to address your comment]

Because the python interpretation of a,b,c is tuple(a,b,c), so print(),print(),print() is equivalent to tuple(print(),print(),print()).
If you do it line after line, the tuple is not created and python just executes the print() statement.
Now comes the point which actually confuses you. Calling print() will also return None, but this automatically hidden, as this is the normal behavior. the tuple of Nones however is not automatically hidden as it is not None.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文