Search code examples
pythonpython-3.xread-eval-print-loopnonetype

Why do I get a print of a tuple of Nones in the Python console, but not of a single 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.


Solution

  • 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.