Reputation: 5
**>>>a=2
>>>b=5
>>>print(a,b)
2 5
>>>print(a),(b)
2
(None, 5)**
Can someone help me understand why the 2nd print statement returns (None,5)? I'm new to this, so sorry if this is a silly question.
Thanks.
Upvotes: 0
Views: 82
Reputation: 54733
There are three pertinent facts here.
The print
function returns nothing. Well, technically it returns "None". Its usefulness is entirely in its side effects -- printing to the console.
The command line interpreter (what you get with ">>>") doesn't print anything if the line returned None
. Thus, for print(a,b)
, you see the effects of the print
function, but the return value of the print
function (None
) is suppressed.
Commas are used to create tuples. Your third statement creates a two-element tuple. The first element is the return from print
, which is None
. The second element is the value 5, from the name b
. So, on your console, you see the effect of the print
function (printing a
which is 2
). Then, you see the command line interpreter displaying the final result of your statement, which is the tuple (None, 5)
.
(Actually, "print
is a function not a statement" is also a pertinent fact...)
Upvotes: 2