support Click to see our new support page.

Python2 vs Python3

blog_img_64
Author

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.

1. Division

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,

division operator in python 2

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.

For getting exact value again need to tell to python 2

Python 3:

division operator in python 3

2. Print Statement

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.

Print Statement in python 3

In python 2 you can write print statement simply :

Print Statement in python 2

The difference in Python 3 is just use an additional parenthesis  to print the statement, as follows:

Python 3 is just use an additional parenthesis  to print the statement

But there is no issue when you use parenthesis to print statement in Python 2.x versions.

3.Unicode

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.

Unicode support in python 2

Output in Python 2,same output for string and bytes:

Unicode support Output in Python 2,

Output in Python 3,different for bytes and strings:

unicode support Output in Python 3,

Python 2 also support Unicode.

Python 2 support Unicode

Output in Python 2,Unicode and Strings are different:

Output in Python 2,Unicode and Strings are different

Output in Python 3,Unicode and strings are same:

Output in Python 3,Unicode and strings are same

4. Exception Handling

There is a slight difference in Error handling in both versions. In python 3 we have to use “as” keyword

Python 2 :

Exception Handling in python 2

Output :

Exception handling: python 2 o/p

Python 3 :

Exception Handling in python 3

Output :

Exception Handling in python 3 code

5. User Input Method

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:

User Input Method in python 2

Python 3:

User Input Method in python 3

6. Xrange

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:

range function in Python 2

And the xrange returns iterator object that gives the same thing when looping over. This behavior is similar to lazy evaluation.

xrange returns iterator object python 2 vs python 3

This laziness is improved in Python 3 by removing the original range function and renamed x range to range

 laziness in python 3

_future_  Module:

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:

Division in Python 2 works same as Python 3 Output:

Division in Python 2 works same as Python 3: output

Hope you understood the concept of Python2 vs Python3..

Odoo ERP services

LinkedIn LinkedIn