1 of 7

Python 2

vs

Python 3

2 of 7

7.2 // 3 # 2.0

3 of 7

What's the Difference Between Python 2 and 3?

Even though Python 3 introduced breaking changes to the Python language, many of these changes are fairly straightforward. As a student of Python, some of the aspects of the new version may make more sense or build better programming habits than the old version. That's a good thing.

Here are some of the changes to keep in mind:

4 of 7

The print statement in Python 2 did not require parentheses. In Python 3 print() is a function, and takes an argument in parentheses. This makes it more consistent with everything else in Python—old print required unique syntax that isn't used anywhere else, new print() is a normal built-in function.

5 of 7

Integer division in Python 2 necessarily returned an integer. If you calculated 10 / 8 Python would assume that, since you didn't use a decimal place in either operand, you wanted the result to be a number without a decimal. It would perform the division and then "truncate" off the decimal place, effectively rounding down the result to 1. In Python 3 all division returns a floating point number (a number with a decimal point), so 10 / 8 would return the expected (and accurate) value of 1.25.

6 of 7

Python 3 strings are Unicode by default. An at-length discussion of string encoding is out of the question in this blog post, but the upshot is that a program written in Python 3 won't freak out when attempting to handle characters not on an English-language keyboard. So you won't have to add extra wrangling code when your coworker Ørn Üsterman joins, and you can feel free to write a program that displays emojis 😉.

7 of 7

Many built-in functions in Python 3 that used to return lists return iterators instead. Lists can use a lot of memory, but an iterator only grabs the information as needed. This means that code like for number in range(50000), which would first build a list with 50,000 elements in Python 2, won't slow you down at all in Python 3.