Introduction to Python 3

This textbook assumes you have already learned basic python programming. You should already be familiar with concepts such as: iteration (for/while loops), conditionals (if statements), function calls, and compound data structures (lists, dictionaries). You should understand basic file input and output (opening a file, reading from it, and writing to it.)

If you do not know how to program in Python, I recommend that you work through chapters 1-12 of How to Think Like a Computer Scientist in Python by Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers.

If you have already learned how to program using Python version 2.x, you should be aware that Python 3 has some minor differences. This text will be using python version 3. Here are the main differences you need to know about for now:

Integer Division

In Python 2 the division operator ( / ) would perform integer division if it was given two operands that were integers. For example, 11 / 3 would give an answer of 3 instead of 3.6. You could then use the ( % ) operator to determine the remainder (11 % 3 = 2) of the division. In Python 3 the division operator ( / ) will always perform floating point division, and you must use ( // ) to perform integer division.

The print statement

In python 2 the print statement was a built in language keyword. In python 3 it is a built in function. To convert a print statement in python 2 to work in python 3, simply add ( open and close ) parenthesis around what should be printed. For example:

print "This is fine in python 2!"

print( "This is fine with python 3!" )

The raw_input function

The raw_input function of python 2 has been renamed to input in python 3.

name = raw_input("Enter your name")  # Python 2

name = input("Enter your name")      # Python 3

Both functions return strings of whatever characters the user types.

The range() function

For speed reasons, the range function now returns an iterable instead of a list. The python for loop can use this as normal, so if you just want to write a for loop that iterates over a range of numbers no changes are needed. Because range does not return a list by default, if you want to create a list of numbers you’ll need to use the list() function to convert the iterable to a list.

aList = range(5,10)          #Python 2
aList = list( range(5,10) )  #Python 3

Dictionary Changes

The has_key() method: In Python 2 you could use the has_key() method to check if a specific key existed in a dictionary. In Python 3 you use the “in” keyword instead:

a_dictionary.has_key(5)              #Works in Python 2
5 in a_dictionary                    #Works in Python 3

Also, several dictionary methods such as keys(), values(), and items() would return lists of objects in Python 2. In Python 3 they instead return dynamic views (iterators). If you use them with a FOR loop, this isn’t a problem, but if you try to index into them with a list operation (such as slicing) it will fail. Use the list() method to convert a dynamic view into a list of items.

a_dictionary.keys()           #Returns a list in Python 2
list( a_dictionary.keys() )   #Returns a list in Python 3

Tkinter changes

Tkinter is a GUI programming API that is bundled with the standard CPython 3.x installation. However, many of the tutorials on the web that introduce you to tkinter programming, such as Fredrik Lundh’s An Introduction to Tkinter and effbot.org’s updated Introduction to Tkinter have code examples for Python 2.x.

In Python 3, the module name has been made all lowercase, so you must import it as follows:

from tkinter import *    #Proper way to import tkinter for Python 3.x

Many other sub-modules have also been made all lowercase, and moved within the main tkinter module. For example, the tkMessageBox module is now named messagebox, and is available inside the tkinter module. To use it, you must specifically import it:

from tkinter import messagebox
messagebox.showwarning("Invalid Move", "I'm sorry, that move is not valid!")

urllib

In python 2 the two main libraries used for parsing, encoding, and fetching data from the web were urllib and urllib2. In python 3, these two libraries are combined into urllib. For this class, you will mostly be using urllib.request.urlopen().

Functional Programming

Much like the range() function, the functional programming functions map(), filter() and reduce() now return an iterator instead of a sequence. Also, the reduce function is no longer in the global namespace. To use it, you must first import it from the functools module.