WELCOME TO PYTHON @ WINTERHAVEN

by Kirby Urner
originally customized for
8th graders in Portland, Oregon

First posted: Dec 31, 2005
Last modified: Jan 18, 2006

OVERVIEW

Python is a general purpose programming language, meaning you may use it in many walks of life. Here are some links to give you an idea of how different professionals make use of Python:

Our introduction began with a segment I call Hello World, which is not the traditional "hello world" of most computer science text books. Instead of writing a very short program that returns or prints the words "hello world" (Python: print "hello world"), we looked at our school's position in space by means of Google Earth, Stellarium and Celestia. In other words, we're using state of the art software to help us get properly oriented and say "hello" to our world.

We also looked at the history of some of the computer languages, using a timeline published by O'Reilly.

Exhibit 1: Winterhaven's location saved in an XML understood by Google Earth

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
    <Placemark>
        <name>Winterhaven School</name>
        <description>Winterhaven PPS, PDX</description>
        <LookAt>
             <longitude>-122.6503682556118</longitude>
             <latitude>45.49456608073391</latitude>
             <range>308.1158978073351</range>
             <tilt>-1.863832985868215e-010</tilt>
             <heading>-2.932467786604351</heading>
        </LookAt>
        <styleUrl>root://styleMaps#default+nicon=0x307+hicon=0x317</styleUrl>
        <Point>
           <coordinates>-122.6503682556118,45.49456608073392,0</coordinates>
        </Point>
    </Placemark>
</kml>
 

Stellarium and Celestia are good examples of open source projects, meaning the source code is available and people work collaboratively by submitting updates and patches (fixes, improvements) to the project manager. Many Python projects are likewise open source, including the language itself (or at least some, if not all of its implementations).

Unlike some computer languages, Python is designed to be usable in "shell mode", meaning you have an easy way to engage with the Python interpreter. Simply type Python commands and expressions, and have the interpreter evaluate them on the spot. You may get an error message. Error messages are useful feedback.

The shell we get with Python 2.4 (the version we're using in this class) is named IDLE. IDLE is partly implemented in Python, and partly implemented in another language called Tk. This is a pretty typical thing we do with Python: use it as a "glue language" to take control of libraries written in other languages.

Exhibit 2: Python Shell with some data structure action

Our first practice sessions involved using Python's primitive objects, such as different types of number, plus characters. Then we started looking at collection types, which are designed to organize information in easy-to-use data structures.

Examples of data structures built in to Python are:

[a, b, c] # list
{a :1, b :2, c :3} # dictionary
"abc" # string
(a, b, c) # tuple

Using data structures, we're able to save a lot of information in a ready-to-use form. These tools, being Python objects, share their powers with us by means of dot notation, e.g. thelist.append(member) or somedict.items(). Try playing with these objects in the shell. Consult documentation to discover or remind yourself what capabilities these objects give you.

Python programmers extend Python's capabilities by defining their own objects and saving them in Python modules and packages. Python also comes with a large Standard Library, already well-stocked with useful objects.

By importing saved modules into the Python shell, or into self-executing programs, we gain access to new and/or customized capabilities. The math module gives us access to a short list of math functions, such as you would find on an electronic calculator. The cmath module adds complex number capabilities. The decimal module supplies a fixed precision alternative to floating point numbers.

Exhibit 3: Using gis.py to write out and then parse cities.xml

using gis module

Note: the above screenshot actually contains an amusing error. The latitude tuples have a 'W' while the longitude tuples have an 'N'. This was pointed out to me by an alert reader, and I've fixed the underlying module, but left this screen shot as a reminder of the kinds of mistakes I'm capable of (sometimes surprising).

In one of our lessons, we imported the xml-rpc library, which sends commands to remote hosts over the Internet. The commands, formatted in an XML, get directed to methods, and must send sensible arguments. Results get returned, if the host does the process, in XML format.

When you write your own Python programs, these get saved as more importable modules. We wrote some simple functions and saved them in site-packages (by default, a subdirectory on the Python interpreter's search path). Then we imported and tested them in the shell.

Exhibit 4: Setting the stage for a first Python function

MORE COURSE MATERIALS

I've organized materials for this course into five sections:

These categories don't necessarily correspond to the chronological order in which we visit topics. They're just a handy way to organize your understanding of Python.

Exhibit 5: Python + POV-Ray: Rhombic Dodecahedra Filling Space

Contact the webmaster