Practice Python

Beginner Python exercises

09 February 2017

Python 2 or 3? Beginners shouldn't care!

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!

Python 2 vs Python 3

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.

Interpreters

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:

month = "December"
day = "06"
year = "2016"
print("The date is: {} {}, {}".format(month, day, year))

Into code that a computer can interpret 1, like this:

01000100100001
00001010010111
10111101010101
01001010101001

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.

Syntax differences between Python 2 and Python 3

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:

  1. printing to the screen (syntax)
  2. user input (syntax)
  3. division (interpretation)

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.

Printing

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
print "my string"
print("my string")

In Python 2, print is an operator, which means it operates on whatever comes after it. In Python 3, print is a function that takes an argument.

So if you get an error like this:

>>> print "Michele"
  File "<stdin>", line 1
    print "Michele"
                  ^
SyntaxError: Missing parentheses in call to 'print'

It means you were trying to use Python 2 print syntax in Python 3. Just add parentheses and you’re good to go. Fortunately, this capability has been back-ported to Python 2, so you can run Python 3 print statements through Python 2 and you should be all set. In case you want to be extra sure, if you are running code through a Python 2 interpreter, just add the following import to the header:

from __future__ import printfunction

It will make sure you can run print as a function in Python 2.

User input

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:

>>> raw_input("input something: ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined

So use input() instead.

Division

The default division behavior is different between Python 2 and 3.

Python 2 Python 3
>>> 2 / 3
0
>>> 2 / 3
0.666666

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:

from __future__ import division

Yes, we are learning division from the future.

Summary

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
Printing
print "my string"
print("my string")
User input
raw_input()
input()
Division
>>> 2 / 3
0
>>> 2 / 3
0.666666

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:

from __future__ import division, printfunction

Read more

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:

Happy Coding!

Enjoying Practice Python?


Explore Yubico
Explore Yubico
comments powered by Disqus