When you are starting to learn Python, you have to decide whether to learn Python 2 or Python 3. Good news is, when you’re starting out, it doesn’t matter! In beginner code, the differences are minimal - in this post we will discuss the 3 differences beginners will notice: print, input, and division. Why is it different and why is it a problem? The interpreters are different!
The debate continues and will continue into the forseeable future: do I learn Python 2 or Python 3? I believe you should learn Python 3 (and if you want to read a more detailed discussion of why, I wrote about it in my introductory post). Instead, I want to talk about how you can add a few lines to simple Python 3 code (which this site uses in the examples) that will let you run the code through Python 2.
First, a quick word on interpreters. What is an interpreter? In English the word interpreter means someone or something that can extract meaning out from words or text. In Python it is the same thing - the Python interpreter translates your Python code that looks something like this:
Into code that a computer can interpret 1, like this:
1 I have no idea if the code compiles to this lovely sequence of 0s and 1s. It is just an illustration.
When you run a Python program through the Python shell, through hitting “play” in IDLE, or by running
python3 in the command line, what you are doing is telling the interpreter to start translating.
How does this relate to Python 2 and Python 3? Well, Python 2 and Python 3 are the same language, but they rely on slightly different interpreters to translate. For example, a syntax difference between Python 2 and 3 is how you print something to the screen. In Python 2 you do
print "Michele" but in Python 3 you do
print("Michele"). The meaning is the same but the way you write it is different. The syntax for Python 2 is not understood by the Python 3 interpreter, and vice versa. The other kind of difference is an interpretation difference - the syntax is the same, but the meaning is different. An example is division, which we will discuss below. Both kinds of differences cause errors in your program, and you must be aware of it.
Let’s talk about the 3 small differences you as a beginner will notice.
For beginners, there are a few important differences (both in syntax and interpretation) between Python 2 and Python 3. We will discuss 3 of them here:
There are many many manyy more small differences that I won’t get into, mostly because they won’t affect you being able to get your hands dirty with Python! If you want to read more about the changes to the Python language introduced in the various versions of Python 3, I have included links to each of the changelogs from Python 3.0 to Python 3.6 here.
The first difference most people will notice between Python 2 and Python 3 is printing information to the screen. The difference is straightforward:
|Python 2||Python 3|
In Python 2,
So if you get an error like this:
It means you were trying to use Python 2
It will make sure you can run
The next difference a beginner will notice is the function that is used to take in user input. In Python 2, the
raw_input() function was used. However, this function is removed in Python 3 and is replaced with the
input() function instead. Beware - the
input() function existed in Python 2 as well, but it was a LOT less useful, and had wrong unexpected behavior.
If you try to use
raw_input() in your Python 3 programs, you will get an error like this:
The default division behavior is different between Python 2 and 3.
|Python 2||Python 3|
In Python 2, the division is integer division - two integers that are divided must return an integer, and the convention is that this is done with the
floor() function, or that the integer returned from the division is just the integer part, not rounded. In Python 3, two integers divided can return a decimal. Strangely, if you do
2.0 / 3 in Python 2, the result will be
0.6666666 as you expect. But remembering to add the
.0 at the end is hard, and can lead to errors in your programs.
Instead, to make Python 2 division behave just like Python 3 division, just add the following import to your program to take advantage of the fact that Python’s future (i.e. Python 3) has fixed this annoyance:
Yes, we are learning division from the future.
For beginners, we talked about 3 small syntax and interpretation differences between Python 2 and Python 3. The differences are so small, that you shouldn’t debate about which to learn! Here are the differences summarized in a single table:
|Python 2||Python 3|
And, in case you have Python 2 but want to run the code examples from this blog (which are in Python 3), all you need to do is add a few imports to the top of your Python 2 code for your Python 2 interpreter:
We haven’t even begun to scratch the surface of the new features that the various releases of Python 3 have added to the language - we focused on 3 small things that beginners will notice.
If you want to read about ALL the new features of Python 3 (not just the 3 tip-of-the-iceberg features), you can read the various “whats new” pages for the latest few releases of Python: