Net-informations.com

Syntax error on print with Python 3

Syntax is changed in new Python 3.x releases rather than old Python 2.x releases: they have replaced the print statement with the print() function . This means that you need to include parenthesis now like mentioned below:

Usage



In Python 3,



gives "Syntax Error: invalid syntax"
Syntax error on print with Python 3

from __future__ import print_function

If you write print() function in a program and someone using Python 2.x tries to run it, they will get an error. To avoid this, it is a good practice to import print function :



The from __future__ import print_function ; to bring the print function from Python 3 into Python 2.x. Now your code works on both Python 2.x and Python 3.x . The __future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning.

A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code . It may even be the case that a new feature introduces new incompatible syntax , in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.



NEXT.....How do I install Python packages