Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python 2
python 3

Python 2 vs Python 3

Educative Answers Team

Python 2 was released in October 2000 and was a big hit in the programmers’ market. It brought new features to Python including, the cycle-detecting garbage collector. It played a very essential role in building the python community.

Python 3 was released in December 2008, and the Python community quickly began to shift towards it. Today, in 2019, there are hardly any Python 2 users remaining. Let’s analyze a few of the ​basic differences between the languages.


1. Library support

Perhaps the most important difference is library support. New contributors are making libraries for Python 3 that aren’t backward compatible. Most of the Python 2 libraries are also not supported by Python 3. Although libraries can be ported from one version to another, it is a hectic process and has limited use.

2. ASCII vs.​ Unicode

By default, Python 2 encodes text strings with ASCII encoding whereas Python 3 encodes text strings with Unicode. Python 2 supports Unicode encoding as well, although conversion is required.

3. Print

Perhaps the most popular change is the replacement of the print statement by the print() function. This means that anything to be printed must be wrapped around by parentheses.

# Python 2
print "Hello World" 

# Python 3
print ("Hello World")

4. Division

The / operator in Python 3 represents standard or float division ( when dividing two numbers gives the exact result) and includes anything that is in the form of a decimal. However, in Python 2 the / character represents integer division ( i.e.,​ if you divide two numbers and the result is rounded off to the closest smaller integer). In Python 3, this task is achieved by // or the Floor division operator.

In order to achieve the float division in Python 2, a .0 needs to be written with at least one of the numbers involved in the division.

# Python 2

print 8 / 5
print 8 / 5.0

5. Range() and xrange()

Python 2 had two ways of creating iterable objects, range() and xrange(); xrange() is faster for iterating only once. However, for multiple iterations, range() would be the​ better choice.

In Python 3, however, the range() function is implemented like Python 2’s xrange(). Hence there is no such thing as xrange() in Python 3.

# Python 2 has range() and xrange()

for i in range(5):
  print i

for i in xrange(5):
  print i

6. Exception handling

There’s a very minor change in error handing syntax. Note the introduction of the as keyword

  rrrrrr # introducing an error
except NameError, err:

7. The __ future__ module

Many of the new keywords and syntax​ introduced in Python 3 can be imported to Python 2 via the built-in __ future__ module. This may be important if one wishes to include Python 3 compatibility to programs.

Should I learn Python 2 or Python 3?

The answer to this question is very straightforward. Support for Python 2 ends in 2020; therefore, people new to Python should definitely learn Python 3. In fact, people already developing in Python 2 should start slowly transitioning to Python 3. Python 3’s community is growing rapidly while Python 2’s community is shrinking.

Perhaps the only reason to learn Python 2 would be if the company you’re currently working for, works in Python 2.


python 2
python 3
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring