Tuesday, October 18, 2011

Differences in print between Python 2.x and Python 3.x

Recently, I started learning Python and installed the latest version available online - Python 3.2. I did this in spite of the fact that the book I was using Practical Programming: An Introduction to Computer Science using Python.

After completing a few exercises using the Python command line, I decided to save my script and execute them from command line. That is when things started to fail. Python would not allow me to print the output. It kept showing the variable to be printed and said SyntaxError: invalid syntax. To illustrate in command line, it was something like:

>>> i=5
>>> print i
File "", line 1
print i
SyntaxError: invalid syntax

Google could not help me with this. Finally, I went with intuition and solved it. I needed to enclose the arguments to print using parenthesis. But the book did not mention it.

This is because the enclosing parenthesis was not a requirement in Python 2.x, but is required in Python 3.x.

It kind of pissed me off (and I am sure I am not the first Python newbie to feel this way), that the language designers did not give a reasonably clear error message for this one.

Karthick S.

Post a Comment