Python Information


Most algorithms textbooks use pseudo-code, but this term we’ll use Python as often as we can. The advantages for our course are:
  • It’s concise like pseudo-code.
  • Any of us can run and modify my examples.
  • Lists and dictionaries can be easily defined, input and used.
  • It’s interpreted and there’s a simple IDE (called Idle).
  • You might just learn a new language.

I’ll be using Python 2.6 (or 2.5) in the course. Python 3 is available but many folks are still using Python 2.

All Python examples done in class can be found at the Code link on this website.


Getting Python: A version of Python is probably already installed on your Mac. Download Python for any major platform here at the Python website.

Learning Python:
The tutorial on that site is here. (Another tutorial is A Beginner's Python Tutorial here. You can also look at the full text in HTML or PDF of a Dive Into Python, a Python book for experienced programmers (last updated in 2004).


Features you need to Know:



Declaring variables:
Python is dynamically typed, so you don’t have to define a variable and state its type before you use it. (Except for lists and dictionaries -- see below.) Functions do have to be defined before they are used.

Code-blocks? Must Use Indentation!
If you’ve coded in Java or C or C++, you’re quite comfortable with the idea of a block of code defined by curly-brackets. You know how these are used as the “body” of an if, an else, a loop, or a function. No curly brackets in Python (or semi-colons at the end of lines)! Instead, indentation must be used to set off a block of statements. Also, the colon symbol must appear at the end of the line for an if, else, elif, for, define etc. The colon means a block begins here.
A Python IDE like Idle (comes with Python) is really important in helping you easily keep the indentation right.

Lists Couldn’t be Simpler (IMHO):
In Python you can define a list by inclosing a sequence of items in square-brackets. E.g.
my_list = [0, 1, “hi”, 3.14, “there”]Some facts:
  • Items can be different types.
  • Items are indexed from 0 using the array-like square-bracket notation, e.g. my_list[0] is the first item.
  • A negative index counts back from the end. E.g. my_list[-1] is the last item.
  • You can get the length of a list with the len() function, e.g. len(my_list).
  • You can remove an item from a list with the del() function, like this: del(my_list[3]) deletes the 4 item.
  • If you want a variable to be an empty list, you must first say: my_list = [ ]
  • Then you can add items to the end with the append() function, e.g. my_list.append(“new item”)

A splice in Python lets you get (or set) a subsequence of a list. The form is my_list[i:j] and this references the subsequence from index i up to but not including index j. So my_list[1:3] references the sublist from my_list[1] through my_list[2].
  • If you leave the first index (before the colon) out, it means from the start of the list.
  • If you leave the 2nd index out (after the colon), it means to the end of the list.
  • You can put a splice on the lefthand-side of an assignment operator = to change a list (possibly changing its length). E.g. my_list[0:2] = my_list[-3:] updates my_list so the first two items are gone and a copy of the last three items take their place.

More coming!