Skip to main content

Python 2 vs 3: Everything You Need to Know

In this article, we will cover the main differences between Python 2 and 3, which one is the best, and which one you should go for to start your data science journey
Aug 2022  · 6 min read

Python 2 vs 3

If you are considering breaking into data science, you will probably have heard about Python. Python is an open-source, general-purpose programming language with broad applicability in data science and other software domains, like web and game development, cybersecurity, and blockchain. 

The popularity of Python has boomed in recent years. It ranks first in various programming language popularity indices, including the TIOBE Index and the PYPL Index. You can learn more about what Python is used for in a separate article. 

Due to its simple and readable syntax, Python is often cited as one of the easiest programming languages to learn for new coders. If you are new to data science and don’t know which language to learn first, Python is one of the best options. You can start your data science journey now by taking Data Scientist with Python career track at DataCamp.

Yet, things can get a bit confusing. You may also have heard about Python 2 and 3. What is this? Isn’t there one single Python programming language? Which one should you learn? No need to stress: except for special circumstances, by now, you will always use Python 3. In any case, it’s always good to know the differences. 

In the following sections, we will explain what Python 2 and 3 are, the main differences between them, and which is the most preferable to learn and use. 

What is Python 2

Python was developed in the late 1980s by Guido van Rossum and made public for the first time in 1991. After nine years of development and increased popularity, Python 2.0 was released in 2000. 

Python 2 came with a brand new technical specification called Python Enhancement Proposal (PEP), which aimed at providing guidelines and best practices for writing Python code. It also comes with new features, such as list comprehension, Unicode support, and a cycle-detecting garbage collector.

But the most important change in Python 2 was to the development process itself. Python was conceived as a programming language that is easy to learn for beginners. To achieve this goal, the team responsible for developing Python –with Guido ban Rossum on top–decided to shift to a more transparent and community-backed development process.

Python 2 continued to develop over time. Successive versions added new functionality to the programming language. The last version of Python 2, released in 2010, was Python 2.7. The support for this version ended on January 1, 2020. 

What is Python 3

Python 3 is the next generation of the programming language. It was released in December 2008, along with several improvements and new features. 

Python 3 was not just another version of Python 2 code after debugging. The new version drastically changed the language to address security issues and design flaws in previous versions. Python 3 came with a new syntax intended to prevent redundant or repetitive code, that is, code that does the same task in different ways. By providing a single, clear way of doing things, the ease of use and readability of Python 3 has improved considerably.  

Some of the major changes in Python 3 include changing the print declaration to a built-in function, improved integer division, and improved Unicode support. The nature of these changes is such that Python 3 was incompatible with Python 2, in other words, it is backwards incompatible.

Why are there different Python versions?

Let’s see some coding examples to illustrate the differences between Python 2 and 3!

The new print() function

The print statement in Python 2 has been replaced by the print() function in Python 3, meaning that we have to wrap the object that we want to print in parentheses.

Python 2:

>>> print 'Hello DataCamp'
Hello DataCamp

Python3:

>>> print('Hello Datacamp')
Hello Datacamp

Integer division

In Python 2, when dividing integers, the result was always an integer. In Python3, the result always includes decimals, making integer division more intuitive.

Python 2:

>>> 5/2
2

Python 3:

>>> 5/2
2.5

Unicode support

In Python 2, every single Unicode string has to be marked with a “u” prefix as it uses ASCII characters by default. By contrast, Python 3 stores strings as Unicode by default, which is more versatile than ASCII strings. 

Python 2:

>>> print type("Hello DataCamp!") # this is an ASCII string
<type 'str'>

>>> print type(u"Hello DataCamp!") # this is a Unicode string
<type 'unicode'>

Python 3:

>>> print(type("Hello DataCamp!")) # this is a Unicode string
<class 'str'>

Range function

The xrange() function in Python 2 no longer exists in Python 3. It has been replaced by the range() function, which has improved performance when iterating over sequences

Python 2:

>>> xrange(1,10)
xrange(1, 10)
>>> type(xrange(1,10))
<type 'xrange'>

Python 3:

>>> range(1,10)
range(1, 10)
>>> type(range(1,10))
<class 'range'>

In the following table, you can find the main differences between Python 2 and 3.

 

Python 2

Python 3

Release date

2000

2008

Syntax

More complex and difficult to interprete

Readable and easily understandable 

Performance

Slower performance due to design flaws

Improved performance of the code’s runtime compared to Python 2

print function

print “Welcome to Datacamp”

print (“Welcome to Datacamp”)

Integer division

The result is an integer value. Decimals are always truncated

The result is always a float value

Unicode support

Uses by default ASCII characters. To store Unicode values, you need to define them using “u”

The default storing of strings is Unicode

Range

xrange() function to create a sequence of numbers

range() function is more efficient when iterating than xrange()

Backward compatibility

Relatively easy to port Python2 to Python 3.

Python 3 is not backwardly compatible with python 2

Libraries

Many older libraries for Python 2 are not forward-compatible

Most of the new libraries for Python 3 cannot be used in python 2

Can you Convert Python 2 to 3? 

Evolution is a vital feature of every programming language. Languages evolve over time to improve performance and stay relevant to users. The move from Python 2 to 3 has been the biggest change in the history of Python. 

While it makes sense for companies and developers to eventually migrate their codebase to Python 3 –especially after the end of support for Python 2–, porting code from an older to a newer version can be a difficult and intimidating process. This is particularly acute in Python since many Python 2 libraries are not forward-compatible. 

Fortunately, there are some resources that can help convert Python 2 code to Python3.

  • 2to3: A Python program that takes Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code.
  • Python-Future: Allows you to use a single, clean Python 3.x-compatible codebase to support both Python 2 and Python 3 with minimal overhead.
  • Six: A Python 2 and 3 compatibility library. It provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible with both Python versions.

Python 2 vs 3: Which is best? 

While this was a debating question some years ago, today, there is no doubt Python 3 is a better choice. First, Python 2 is no longer supported since 2020. So it makes sense for new projects to be written in Python 3.x. Second, since Python 2 is no longer supported, all the developments lie on the Python 3 side. The language has experienced constant improvements following every new  upgrade (the most recent version is Python 3.10.5, and there is already a beta version of Python 3.11 with important new features). 

Given this context, the massive adoption of Python 3 comes with no surprise. According to the 2021 Python Developers Survey 2021, conducted by JetBrains, on average, the share of Python developers using Python 3 was 95%. Further, the share of Python 2 users decreases by 5 percentage points each year.

Adoption of Python 2 vs Python 3

Adoption of Python 2 vs Python 3

Source: JetBrains

Which Python version to use? 

Python 3 is the best version of Python nowadays.  Going for it is the safest choice, especially for novel programmers. Since its stop of support, Python 2 is rapidly running out of steam, and more and more companies are migrating their code to Python 3. Given its growing demand in many software domains, widespread usage, and huge collection of community-backed libraries, it makes sense to learn Python 3.

However, it’s important to note that in certain circumstances, you will still need to use Python 2. First, many companies still have legacy codebases written in Python 2 that are difficult to port to Python 3. If that’s the case for the company or project you are working on, you will need to learn Python 2. Second, there are some software domains, such as DevOps, where Python 2 is still required. 

In any case, if you are considering starting a new career in data science, Python 3 is the smart choice. With DataCamp, everything is set for you. Check out our Python course and tracks, and start your data journey today!

Python Best Practices FAQs

Which version of Python is more popular?

Python 3 is the indisputable winner, with massive adoption compared to Python 2.

Is Python a suitable language for data science?

The most popular programming languages for data science are Python and R. DataCamp has a large catalog of Python courses that will help you get started

Which version performs better runtimes?

Yes! in almost all tests. Python 3 is usually faster than Python 2.

What is the latest version of Python 2?

Python 2.7. In 2020, the support and maintenance of this version was ended.

Why upgrade to Python 3?

Python 3 is a better language and comes with a better set of standard libraries than Python 2. Plus, since 2020, the language and standard libraries are improving only in Python 3.

Can I port Python 2 to Python 3?

There are some resources  you can use to update your code to Python 3, such as 2to3, Python-Future, and Six.

Can I install both Python 2 and 3?

Yes. You can do it by maintaining separate virtual environments for Python 2 and 3.

Intermediate Python

Beginner
4 hours
847,024
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.
See DetailsRight Arrow
Start Course

Python Data Science Toolbox (Part 2)

Beginner
4 hours
218,313
Continue to build your modern Data Science skills by learning about iterators and list comprehensions.

Python Data Science Toolbox (Part 1)

Beginner
3 hours
333,714
Learn the art of writing your own functions in Python, as well as key concepts like scoping and error handling.
See MoreRight Arrow
← Back to Blogs