The Main Differences Between Python 2 and Python 3
In this lesson you’ll meet some of the main differences between Python 2 and 3, such as the print
-function, the long
-function or the try-except
-statement.
00:00 As you can see here, I have two interpreters—one with Python 2.7.2 and Python 3.2. If you are currently using Python 2.7, a lot of these idioms or ways of writing particular pieces of code have been backported into Python 2.7.2. So some of the things I may show you, you may seem to think that they work in 2.7, but that’s only because they’ve been moved backwards.
00:25
I will first show you the way of doing it in traditional Python 2 and then I’ll show you that it doesn’t work in 3.2 and how to fix it. So, let’s first take the simplest statement that we can think of, which is the print
statement. In Python 2, you can simply do things like print 5
and print 'dog'
without much trouble.
00:46
Now when you try to do the same thing in Python 3, you get an error. You can’t print 5
and you definitely can’t print 'dog'
. In order to combat this and fix it in Python 3, you need to do this. Because the print
statement was converted into a function, so you have to call it with arguments, as you see here.
01:06
And the same thing with 'dog'
.
01:12
Now, another thing that’s been combined in Python 3 is longs. There’s no such thing as a long
anymore in Python 3. You were able to do this before,
01:25
and get a long
back. And in Python 3, there is no such thing as long
, so when you do this, you get long
is undefined—there’s no such thing. In order to combat this, you assign long
to int
and then you go long()
, whatever number you want, and then that’s how you do it. The int
and long
syntaxes were combined.
01:47
So before you run anything in Python 3.2 using longs, you just convert the meaning of long
—or, you assign long
a meaning, since in Python 3.2 it doesn’t exist.
02:00
Now let’s try something that’s a bit more involved. Let’s try dividing by zero and then catching that exception. So, in Python 3, in order to do such thing, you’d simply divide by 0
… Oh, sorry. Divide by 0
, except ZeroDivisionError, e:
—one, two, three, four—print
e.args
, and we want the first argument printed back out to us.
02:32
And as you can see, integer division or modulo by zero
exception. And we got the print
to come out, no problem. So, let’s try the similar thing in Python 3. try:
1
divided by 0
, except ZeroDivisionError, e:
, and that gives us a SyntaxError
.
02:59
So, the proper way of doing this in Python 3 is try:
—
03:05
one, two, three, four—1
divided by 0
,
03:09
except ZeroDivisionError as e:
.
03:24
One, two, three, four. Now when we’re printing, we have to be careful to use the function, so we will do this. .args[0]
.
03:40
Oh, we need the closing brace there. And then we get the exception, division by zero
. So, that’s just to show that there’s a lot of subtleties when going from Python 2 to Python 3, and there are a lot of libraries we’ll cover today when discussing how to convert delorean
from Python 2 to 3.
Become a Member to join the conversation.