Arunima PSept. 21, 2018
Beginning Python users are confused to choose which version is better for learning. The most common used versions of Python are Python 2.x and Python 3.x. Python 2 is for legacy and Python 3 is the future of programming language. Both versions are almost similar and easy to learn. But, there are plenty of differences between these Python versions.
Python 3 has improved division operator. By using Python 3 you will get expected result without any worry.
But, in the case of Python 2 , if you use division operator in your calculation it rounds down to the nearest whole number.
For example,
Your expected answer is 2.5 . But the Python 2 gives only integer back when you input only integers. That is the answer in 2 in the case of Python 2.
In order to get the result 2.5, you would have to write 5.0/2.0 to tell Python that you want it to return a float, that is, to include digits after the decimal point in the result.
Python 3:
The most well known difference in Python 2 and Python 3 is print statement. Python 2 uses print statement and Python 3 uses print method. It will be dangerous when you put the print statement in Python 2 to Python 3.It becomes a syntax error.
In python 2 you can write print statement simply :
The difference in Python 3 is just use an additional parenthesis to print the statement, as follows:
But there is no issue when you use parenthesis to print statement in Python 2.x versions.
In Python 3, there is better Unicode support . Unicode Strings can store different language letters ,Roman numbers,symbols etc.,
In Python 2, implicitly string type is ASCII by default.
Output in Python 2,same output for string and bytes:
Output in Python 3,different for bytes and strings:
Python 2 also support Unicode.
Output in Python 2,Unicode and Strings are different:
Output in Python 3,Unicode and strings are same:
There is a slight difference in Error handling in both versions. In python 3 we have to use “as” keyword
Python 2 :
Output :
Python 3 :
Output :
The user input method problem in Python 2 is fixed in Python 3.
In the case of Python 3 ,input() always receives input as ‘str’ or string type. In Python 2 there are two methods raw_input() and input() method.
It is recommended that use raw_input() method to read other type than Strings to avoid complications.
Python 2:
Python 3:
The range in Python 3 is more power full than range in Python 2.
In Python 2 the range function return a list as follows:
And the xrange returns iterator object that gives the same thing when looping over. This behavior is similar to lazy evaluation.
This laziness is improved in Python 3 by removing the original range function and renamed x range to range
This is the idea of merging Python 3 code to Python 2 code. This not referred to as the difference between two versions. If you want to support Python 3 in Python 2 code by importing _future_ module .
Division in Python 2 works same as Python 3 in below given code:
Output:
Hope you understood the concept of Python2 vs Python3..