Many wonder the difference between Py3 and Py2. Some beginners are not sure which version of Python to learn. While most organizations still enjoys using Python 2.x, it is important to note that while Python 2 is still commonly Python 3.x comes with its perks too. I recommend that you learn Python 3.x and then study the differences between both versions since Python 3 eliminates many quirks that can unnecessarily pops up.
Integer Division
This change is particularly dangerous if you are porting code, or if you are running Python
3 code in Python 2, since the change in integer division behavior often goes unnoticed and
usually raises no exceptions ().
python
text
And in python 3:
python
text
The Print Function
The change in the print syntax is one of the widely known changes. The print statement
in Python 2 has been replaced the function in Python 3. In that case, we have
to enclose the objects we want to print in a parenthesis
. Python 2 doesn’t have a
problem with additional parentheses, but in contrast, Python 3 would raise a
if we called the print function the Python 2 way without the parentheses.
python
text
In Python 3,
python
text
Raising Exceptions
Where Python 2 accepts some Py2 and Py3 syntax, Python 3 fails to do so and
raises a if we do not enclose the exception argument in parentheses.
python
text
python
text
python
text
Handling Exceptions
In Py3, we have to use the keyword while handling exceptions.
python
text
python
text
Parsing User Input with
Fortunately, the function was fixed in Py3 so that it always stores the user
inputs as string objects. In order to avoid the dangerous behavour in Py2 to read in
other types than strings, Py2 users have to use
instead.
Importing
Module
While Python 3.x introduced some Python 2 incompatible syntax, some of its features
can be imported via the inbuilt module in Python 2. It is recommended to
use this import if you are planning to use Python 3.x support for your code. For example,
if you want to import Python 3.x integer division in Python 2, we can do this using the
.
python
Still unsure which version to use? You can read more from Python Wiki and Python-future documentation.