2

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Slava Bugz
  • 318
  • 5
  • 17
  • @wjandrea No no, I just don't understand why if I will use 3 print statments it will return me a tuple, and if i will use a single one, it will return me a single print. Like why is the tuple created when I'm using 3 of them, but not if I'm using one, why is an object created when using 3, but not when using one, or atleast why why object that is created when using one print, is not shown unlike when using 3 as a tuple. – Slava Bugz Jul 04 '22 at 18:24
  • *"why object that is created when using one print, is not shown"* -- You've hit the nail on the head. `print()` returns `None`, which is not printed. Try typing `None` into the REPL: there's no output. That's covered in the official tutorial [here](https://docs.python.org/3/tutorial/controlflow.html?highlight=none#defining-functions). – wjandrea Jul 04 '22 at 18:31
  • Though in this case, it's at least a little more fair because the question really needed the clarifying comment, so this time the duplicate closure is the asker's fault to some extent. If it was more clearly "why do three Nones get printed but one doesn't in the Python REPL" i the question body it would be more obviously not a duplicate of that question. – mtraceur Jul 04 '22 at 18:31
  • 1
    Sry, was not sure how to ask it, now when I got the answer I can ask it better. – Slava Bugz Jul 04 '22 at 18:33

1 Answers1

5

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.

user_na
  • 2,154
  • 1
  • 16
  • 36
  • No no, I understand it, and I do get the abstract picture, but I don't understand why it returns a tuple of items when I'm doing 3 prints, but only "printing" something if I'm using a single one. – Slava Bugz Jul 04 '22 at 18:22
  • 1
    After edit - Thank you! got it now, also after looking at the duplicate question, I understand it now more clear, the python console just does not print a single None, but when creating a tuple of none there is no reason not to print it. – Slava Bugz Jul 04 '22 at 18:31
  • 1
    Yes, I added the information that a single `None` is not displays. It is actually the curical point here. – user_na Jul 04 '22 at 18:33