San-ree
San-ree

Reputation: 5

What is the difference between print(var,var) and print(var),(var)?

**>>>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

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54733

There are three pertinent facts here.

  1. The print function returns nothing. Well, technically it returns "None". Its usefulness is entirely in its side effects -- printing to the console.

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

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

Related Questions