2

Python:

1>0 and print("yes")

SyntaxError: invalid syntax at 'print'

Could anyone tell why? Thanks!

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Edison
  • 61
  • 6
  • 1
    possible duplicate of [Python print statement “SyntaxError: invalid syntax”](http://stackoverflow.com/questions/7584489/python-print-statement-syntaxerror-invalid-syntax) – Rong Nguyen Apr 14 '14 at 07:05
  • 2
    @RongNguyen It is actually the exact opposite of it, I believe :) – thefourtheye Apr 14 '14 at 07:06
  • 2
    People should seriously stop jumping at every opportunity to state that a question is a "possible duplicate". – Lee White Apr 14 '14 at 07:07
  • @LeeWhite The problem is related to the question i reported, seriously! – Rong Nguyen Apr 14 '14 at 07:09
  • @thefourtheye The answer is similar for both :-) – Rong Nguyen Apr 14 '14 at 07:10
  • you are combining boolean and a print statement. Interpreter cannot figure what you intended too, Neither can we if you do not explain what it is. This squestion is probably not suited to this site See http://stackoverflow.com/questions/how-to-ask. Also try an online python guide. – Joop Apr 14 '14 at 07:26

2 Answers2

10

In Python 2 print is a statement and it cannot used as an expression.

To use the Python 3's print function in Python 2 you need to import it first:

from __future__ import print_function

Demo:

>>> 1>0 and print("yes")
  File "<ipython-input-2-0714eacbdec3>", line 1
    1>0 and print("yes")
                ^
SyntaxError: invalid syntax

>>> from __future__ import print_function
>>> 1>0 and print("yes")
yes
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

In Python 2.* print is a statement and you cannot use statements in (boolean) expressions because they do not return a value.