What does print() do in python 2 versus print



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]


Age 18
Name Tom

where as:

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


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

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?


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)


well, if you print a list:

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

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


yes, exactly.


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