What does print() do in python 2 versus print


#1

Hi

What does print() do in Python 2, besides printing the result out?

What I mean is:

for key in my_dict:
    print key, my_dict[key]

Outputs:

Age 18
Name Tom
None

where as:

for key in my_dict:
    print(key, my_dict[key])

Outputs:

('Age', 18)
('Name', 'Tom')
None

I'm trying to get an understanding of both version of the language, so was wondering how the print function with parenthesis and without, differs?


#2

well, in python2 print is a statement where as in python3 its a function call

so using parentheses in python2 causes the output to be a tuple (immutable list), if a comma is present. (which in your case it is, so a tuple is printed)


#4

well, if you print a list:

print ['a','b','c']

the apstrophes are also printed. Lists and tuples include '' around strings


#6

yes, exactly.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.