#3 The "in" Operator. Difference Between print and print()?


#1

https://www.codecademy.com/courses/learn-python/lessons/advanced-topics-in-python/exercises/the-in-operator?action=lesson_resume

Hello, I have a quick question. This code works:

my_dict = {"country": "Montenegro", "continent": "Europe", "date": 2006}

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

…And this code does not work (it runs, but puts parentheses on the output)

my_dict = {"country": "Montenegro", "continent": "Europe", "date": 2006}

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

I’ve heard that print is a function in python, hence the parenthesis. Are there different variants of Python that uses the print() syntax?


#2

in python3 print is a function, in python2 (what codecademy uses) print is a statement. So this:

print (key,my_dict[key])

will print a tuple (immutable list)


#3

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