57

I understand the difference between a statement and an expression, and I understand that Python3 turned print() into a function.

However I ran a print() statement surrounded with parenthesis on various Python2.x interpreters and it ran flawlessly, I didn't even have to import any module.

My question: Is the following code print("Hello SO!") evaluated as a statement or an expression in Python2.x?

smci
  • 32,567
  • 20
  • 113
  • 146
rahmu
  • 5,708
  • 9
  • 39
  • 57

3 Answers3

64

Consider the following expressions:

a = ("Hello SO!")
a = "Hello SO!"

They're equivalent. In the same way, with a statement:

statement_keyword("foo")
statement_keyword "foo"

are also equivalent.

Notice that if you change your print function to:

print("Hello","SO!")

You'll notice a difference between python 2 and python 3. With python 2, the (...,...) is interpteted as a tuple since print is a statement whereas in python 3, it's a function call with multiple arguments.

Therefore, to answer the question at hand, print is evaluated as a statement in python 2.x unless you from __future__ import print_function (introduced in python 2.6)

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 2
    Would using from __future__ import print_function hurt in Python3? It doesn't give an import error. – sgoblin Jul 21 '15 at 22:28
  • 2
    @sgoblin -- Nope. the `__future__` module gurantees that nothing will ever be removed from it. It's effectively a no-op on python3.x – mgilson Jul 21 '15 at 22:30
22

print("Hello SO!") is evaluated as the statement print ("Hello SO!"), where the argument to the print statement is the expression ("Hello SO!").

This can make a difference if you are printing more than one value; for example print("Hello", "world") will print the 2-element tuple ('Hello', 'world') instead of the two strings "Hello" and "world".

For compatibility with Python 3 use from __future__ import print_function:

>>> print("Hello", "world")
('Hello', 'world')
>>> from __future__ import print_function
>>> print("Hello", "world")
Hello world
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • If you want to be compatible with python2.5 and python3.x you can use string interpolation (although I don't think too many people are advocating that these days ...) – mgilson Aug 28 '12 at 15:40
  • 7
    This is also quite interesting. Note that if you put this into a script, it will fail because `__future__` imports must occur at the beginning of the file, but by some deep python magic, it works in the interactive interpreter. – mgilson Aug 28 '12 at 15:45
2

It is still evaluated as a statement, you are simply printing ("Hello SO!"), which simply evaluates to "Hello SO!" since it is not a tuple (as mentioned by delnan).

Lanaru
  • 9,421
  • 7
  • 38
  • 64
  • 3
    And to be clear, `("Hello SO!")` is exactly equivalent to `"Hello SO!"` -- it's not a tuple. –  Aug 28 '12 at 15:36
  • Thanks @delnan, I saw your comment right before posting a nice answer about 1-element tuples. – Matthew Adams Aug 28 '12 at 15:40
  • 4
    And to expand on delnan's comment, to create a 1-tuple, you need a comma (e.g. `("Hello SO!",)`) – mgilson Aug 28 '12 at 15:50