Archangel Macsika Sikademy Image
Archangel Macsika

Learn the Main Differences Between Python 2 & Python 3

Tutorial 2 of 3 | 6 minutes read
Learn the Main Differences Between Python 2 & Python 3 - Sikademy
Tutorial Guide
Scroll for more menu list

In the early days when python 3 was first released, there were few arguments in the coding community about which Python version was the better choice to learn, the battle line was drawn — Python 2 vs Python 3.

As of the time of writing this article, Python 3 is the chosen version for new programmers, big-shot programmers new to python, and software developers wanting to update their knowledge stack. In this tutorial, we'll cover why Python 3 is a better choice and why we chose it as our choice version.

Before that, we want to point out that there are still some situations where coding with Python 2 might be advantageous over Python 3. This is mainly because of the useful python packages that are only available in Python 2.

So, without wasting much time, and since this is a beginner tutorial, let’s take a look at some of the major differences between Python 2 and Python 3 that is relevant to you as a new Python learner.

Brief History & Timeline of Python 2 and Python 3

Python 2 saw its first release, Python 2.0, in the year 2000. Its latest version, Python 2.7, was released in 2010. On the other hand, Python 3.0 was first released in 2008, and its latest version (at the time of writing this tutorial) is Python 3.7.3, and was released in 2019.

Please know that you can use the latest version of Python 3 to follow along with these tutorial series as we always update the entire tutorial series to align with the latest version of Python 3 as soon as a new version is released.

You should also know that in January 1, 2020, Python 2.7 will be deprecated and absolutely no longer maintained by the core Python team.

What are the Main Differences Between Python 2 and Python 3?

1. Division Operator

In Python 2, if you perform a division using integer (whole numbers) for numerator and denominator, the result will be rounded up to the nearest whole number. To get the correct result to a division in Python 2, the numerator and denominator should be written in decimal. For example:

The output will be:

3
3.5

In Python 3, you can perform a division using integer (whole numbers) for both numerator and denominator, and the result will be given. For example:

The output will be:

3.5
3.5

2. Print Function Syntax

There is a syntactical difference between the print statement in Python 2 and Python 3. Even though it may be regarded as trivial, it is the most noticeable change and still worth considering when coding in Python. That said, in Python 2, you can print with or without additional parentheses.

The output will be:

Hello, Archangel
Hello, Archangel

In Python 3, the print function must be called with parentheses. For example:

The output will be:

Hello, Archangel

But in contrast, Python 3 would raise a SyntaxError when you call the print function without the parentheses. For example:

The output will be an error similar to the one below:

SyntaxError: Missing parentheses in call to 'print'.

3. Input Function

In Python 2, there are two ways of using the input function — input() and raw_input(). The raw_input() function, evaluates whatever data the user enters as a string. This means that python doesn't try to even understand the entered data again. All it will consider is that the entered data will be string, whether or not it is an actual string or int or any data type. The input() function on the other hand treats the received data as string (text) when written within quotes ' ... ' or " ... ", otherwise the data is treated as the evaluated data type. For example:

The output will be:

What is your name? Archangel
What is your other name? Macsika
Archangel
Macsika

In Python 3, the input() function was deprecated and the raw_input() function was renamed to input(). This means that the input() function in Python 3 has the properties of the Python 2 raw_input() function. For example:

The output will be:

What is your name? Archangel
Archangel

To get an evaluated input in Python 3 i.e to use the Python 2 input() function in Python 3, you make use of the eval() with the input() function. For example:

The output will be:

What is your name? Archangel
Archangel

4. Unicode Support

In Python 2, strings are stored as ASCII by default. To store strings as Unicode in Python 2, you have to add a "u". In Python 3, text strings are Unicode by default.

This is important because Unicode is more versatile as it can store foreign language letters, Roman letters and numerals, symbols, emojis, etc., offering you more choices.

5. Code Compatibility and __future__ module

Since Python 3 is the future, many of today's developers are creating libraries that are not backward-compatible with Python 2. Thus, they are strictly for use with Python 3. In like manner, many older libraries built for Python 2 are not forward-compatible with Python 3 and can only be used in Python 2.

You may be able to port Python 2 library to Python 3, but this process can be quite tasking and definitely not an "introduction to programming with Python" type of activity.

Nevertheless, Python 3 introduced a method that allows some keywords and features in Python 3 to be used in Python 2. They can be imported via the in-built __future__ module in Python 2. We strongly recommend you use the __future__ imports, if you are planning to support some Python 3 functionalities in your Python 2 program.

For example, if we want to use Python 3 integer division in Python 2, add the following import statement.


from __future__ import division

Wrap Off

As stated earlier, we will only talk about the differences that we feel is relevant to an introduction to Python programming. There are many other differences between Python 2 and Python 3. You can find them with a little bit of googling. If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as pdf or report bugs using the buttons below.

Tutorial Exercise

What does it mean when a programming language or technology is deprecated?

Find out other differences between Python 2 and Python 3?


Enjoy this Tutorial? Please don't forget to share.