Python:
1>0 and print("yes")
SyntaxError: invalid syntax at 'print'
Could anyone tell why? Thanks!
Python:
1>0 and print("yes")
SyntaxError: invalid syntax at 'print'
Could anyone tell why? Thanks!
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
In Python 2.* print is a statement and you cannot use statements in (boolean) expressions because they do not return a value.