Table Of Contenttutor
Alan Gauld
27 May 2007
tutor
Table of Contents
Learning to program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Learning to program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What do I need to be a programmer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
What is programming?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Simple sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
More Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Looping the loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
A Little Bit of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Text Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Event Driven Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Introduction to GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Introduction to Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
A Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Python in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Working with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Working with the Operating System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Inter-Process Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Inter-Process Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
May 27, 2007
Learning to program 27/05/2007
Contents Learning to Program
Introduction
by Alan Gauld
Stop Press! Last Update: 27th May 2007
Concepts
The Web Site
What do I need?
What is Welcome to the Learning to Program web tutor!
'Programming'?
I've been very busy in my day-job over the last year so very little
Getting Started progress has been made, however I am working on the first web
programming topic. Meantime there have been several tweaks to
the existing topics, including many minor bug fixes and some
changes to the colour scheme to reducethe contrast which some
The Basics folks felt was a bit glaring.
Simple
Sequences The original version of the tutor using TCl and QBASIC is still
available here.
The Raw
Materials The Book
More Sequences
Latest news on my book is that it has moved to print-on-demand.
Basically this means that copies are only printed when firm orders
Loops
are received from retailers. Logically this means you are less likely
to see it in the shops but you can still place an order and it will be
Add a little style
delivered, it just takes a little longer is all.
Talking to the
The book is now significantly different to the new web site
user
although the basics remain the same. It is stuck at Python version
Branching 1.5 and doesn't have the new material, although it has extra
examples of its own plus several extra chapters compared to the
Modules & web site. These include an additional case study for an OO games
Functions framework. (A fully commented version of the framework is
available on the old Useless Python website as hmgui.zip)
Handling Files
You can order the book on Amazon by clicking here. Most of the
Handling Text other online bookshops have it in stock too.
Error Handling There is also a Japanese version available from the Japanese branch
of my publishers.
I now have a web site set up listing errata and providing
Advanced opportunities to send feedback and questions.
Topics Please pay a visit if you've bought the book!
Page 1
Learning to program 27/05/2007
What's in a
The Future
name?
Regular As I said above I am still adding new topics added as part of a new
Expressions section looking at practical applications of programming. This is in
response to many requests from readers who say - "OK, Now I
Object Oriented
know how to program, but what exactly can I do with my new
Programming
found skills?" These topics do not feature any VBScript or
JavaScript.
Event Driven
Programming
There is a Polish translation underway and the Czech version has
been updated to reflect the latest version. As ever, Petr has been my
GUI
most exacting reviewer and I thank him for his many corrections.
Programming
Any remaining errors are entirely my fault!
Recursion
The book is stagnant I'm afraid, but once I get the new topics
- or doing it to
written I might sound the publishers out about a second edition
yourself
updating it to the latest version of Python, whatever that will be by
then, and adding the new material. But don't hold your breath
Functional
waiting on that one...
Programming
Non English Editions now available!
A Case Study
Martin Pozzi was the first to translate the tutor (into Spanish) and
he has now been followed by several others. The following versions
are available:
Applications
Python in Language Translator
Practice
Czech version Petr P
Working with German Bruno Schaefer
Databases
Korean johnsonj
Using the Italian Luca Fini
Operating
Portuguese Wilson Edgar
System
Spanish Martin Pozzi
Inter-process
communications The tutorials above are all usable but at different stages of
completeness compared to the web site. That's because they reflect
Network the state of the web site at the time of translation. The Czech one in
programming particular is very professionally presented including a very
attractive appearance. I was so impressed with the style that I have
Writing web
used Petr's stylesheets as the basis for the English version.
clients
Hopefully I'll get round to changing the other language editions
too.
Writing Web
Applications
Page 2
Learning to program 27/05/2007
Parallel I think all of the translators deserve a vote of thanks for their
processing efforts in putting this together. It's always better to read a
translation of an old tutorial than to struggle reading a new one in a
foreign language! If anyone else wishes to do a translation, or
update any of the existing ones, just send me an email.
Appendices
References,
Books and As mentioned above there is a Japanese version of my book
Projects available too.
New Stuff
Download the Not much because of the PC problems mentioned above!
whole thing in
Download Archive Format Change
TGZ format
Or in ZIP I'll be posting both zip and tgz versions of the site from time to
format. time. I don't promise that they will be perfectly up to date but I'll try
not to let them get too far adrift. If you want the latest then check
Or in PDF the online version.
format.
Linux users can download the tar/gzip version.
Send Feedback
Windows users should be able to use that version too since
winzip and the freeware archiver Quick Zip by Joseph Leung can
both cope with tgz format. I'll also be using tgz format for the non
English versions, just look at the bottom of the contents frame for a
link.
Finally I have made a basic PDF version available too, it doesn't
have any fancy navigation features like bookmarks but if you like
PDF format it's here.
And Palm Users Too...
One Palm user has created a Palm doc file of the old site. I haven't
had a chance to try it yet but offer it here for those who want it. If
anyone has problems with this one then you're on your own, it's
unsupported. But hopefully it will be useful to someone. If you
don't have a Palm doc reader you can find a choice of freeware
ones at this site.
Page 3
Learning to program 27/05/2007
Introduction - What, Why, Who etc.
Why am I writing this?
The reason I am creating this tutorial is that there seems to be very
little for the absolute beginner to programming on the Web. Yet the
Internet and the Web encourage interest in computers and that
interest naturally leads to a desire to "take control", which means
learning to program!
Why me? Well I am a professional programmer who came to
programming from an electronic engineering background. I have used
(and continue to use) several computer languages and don't have any
personal interest in promoting any particular tool or language. Oh,
and when I started, nobody else seemed to be doing it! Since then
several other tutors for beginners have appeared, but this one seems
to have acquired enough of a fan base that I'll keep it going.
What will I cover
As much as I can. I will cover the basic theory of computer
programming - what it is, some of its history and the basic techniques
needed to solve problems. I will not be teaching esoteric techniques
or the details of any particular programming language, in fact I'll be
using several different languages, since I believe its important to
realize that different languages do different things well. That said, the
majority of the course will be in the language called Python.
Who should read it?
Put another way: what do I expect the reader to know already?
I expect the reader of this tutorial to be an experienced user of a
computer system, probably MS DOS, Windows or Unix although
others should be able to cope too. I also expect them to understand
some very basic mathematical concepts such as geometric
coordinates, sets, and basic algebra. These are all important in todays
programming environments, and many programming concepts are
based on these ideas. However the depth of knowledge needed is
very low and if you do find the math getting too hard, you can
usually just skip over a few paragraphs, try the code as it is and
hopefully the penny will drop even if the math still confuses you.
One thing you should know is how to run commands from your
operating system's command prompt. In Windows this is variously
known as a DOS box, the MS DOS Window or MS-DOS Prompt.
Basically it's a black window with a white text prompt that usually
Page 4
Learning to program 27/05/2007
says C:\WINDOWS> and you can start it by going to the Start->Run
dialog and typing COMMAND into the entry box and hitting OK. If
you use Linux then you should know all about terminal windows and
on MacOS you can run the Terminal program under Mac OS X
(which is found in the Applications->Utilities folder).
I will not be covering issues like how to create or copy text files, how
to install software, or the organization of files on a computer storage
system. Frankly if you need to know those things you probably are
not at the stage of being able to program, regardless of your desire to
do so. Find a tutorial for your computer first, then when you're
confident with the above concepts revisit this site.
Why Python?
Python happens to be a nice language to learn. Its syntax is simple
and it has some very powerful features built into the language. It
supports lots of programming styles from the very simple through to
state of the art Object Oriented techniques. It runs on lots of
platforms - Unix/Linux, MS Windows, Macintosh etc. It also has a
very friendly and helpful user community. All of these are important
features for a beginner's language.
Python however is not just a beginner's language. As your experience
grows you can keep on using Python either as an end in itself or as a
rapid prototyping language. There are a few things that Python is not
well suited to, but these are comparatively few and far between.
I will also use VBScript and JavaScript as alternatives. The reason
for this is to show that the same basic techniques apply regardless of
the language details. Once you can program in one language you can
easily pick up a new one in a few days. Why those languages? Well,
for a start they have very different styles to Python so form a useful
contrast, and more prosaically if we accept that most Web surfers
who are also beginners are using PCs with Microsoft Windows
installed, there is a programming environment built in to the
operating system called Windows Scripting Host which has support
for VBScript and JScript (which is Microsoft's variant of JavaScript).
In addition anyone using Microsoft's web browser can also use these
languages within their browser, and in fact JavaScript should work in
almost any browser. We'll only look at how to run VBScript and
JavaScript inside a browser, investigating WSH I'll leave as an
exercise for the interested Windows user!
Other resources
Page 5
Learning to program 27/05/2007
There are other Web sites trying to do this in other languages (and in
the time since I originally created this site a few other Python sites
have appeared). There are also lots of tutorials for those who already
know how to program but want to learn a new language. This section
contains links to some of those that I think are worthwhile!
The official Python language website with online
documentation, latest downloads etc.
The official Perl web site - Perl is a natural competitor to
Python in capability but is, I think, harder to learn.
JavaScript. is the source for information about JavaScript.
If you don't much like my style a web site with similar aims is
the How to think like a Computer Scientist produced by Jeff
Elkner who uses Python in his high School classes. It seems a
little bit less comprehensive than mine, but maybe I'm just
biased :-)
Since I first wrote this tutor a whole bunch of non
programmer's tutorials have appeared and they are listed on
the Python web site, so you can take your pick. Most of them
focus on just getting you programming in Python so they
don't explain so much of the jargon as I do, nor do they
explain the Computer Science theory like Jeff does. You can
find the page here.
Next Contents
If you have any ideas on how to improve this tutorial
please feel free to contact me
Page 6
Learning to program 27/05/2007
Learning to Program
by Alan Gauld
Stop Press! Last Update: 27th May 2007
The Web Site
Welcome to the Learning to Program web tutor!
I've been very busy in my day-job over the last year so very little progress has been
made, however I am working on the first web programming topic. Meantime there
have been several tweaks to the existing topics, including many minor bug fixes and
some changes to the colour scheme to reducethe contrast which some folks felt was
a bit glaring.
The original version of the tutor using TCl and QBASIC is still available here.
The Book
Latest news on my book is that it has moved to print-on-demand. Basically this
means that copies are only printed when firm orders are received from retailers.
Logically this means you are less likely to see it in the shops but you can still place an
order and it will be delivered, it just takes a little longer is all.
The book is now significantly different to the new web site although the basics
remain the same. It is stuck at Python version 1.5 and doesn't have the new material,
although it has extra examples of its own plus several extra chapters compared to the
web site. These include an additional case study for an OO games framework. (A
fully commented version of the framework is available on the old Useless
Python website as hmgui.zip)
You can order the book on Amazon by clicking here. Most of the other online
bookshops have it in stock too.
There is also a Japanese version available from the Japanese branch of my
publishers.
I now have a web site set up listing errata and providing opportunities to send
feedback and questions.
Please pay a visit if you've bought the book!
The Future
Page 7
Learning to program 27/05/2007
As I said above I am still adding new topics added as part of a new section looking at
practical applications of programming. This is in response to many requests from
readers who say - "OK, Now I know how to program, but what exactly can I do
with my new found skills?" These topics do not feature any VBScript or JavaScript.
There is a Polish translation underway and the Czech version has been updated to
reflect the latest version. As ever, Petr has been my most exacting reviewer and I
thank him for his many corrections. Any remaining errors are entirely my fault!
The book is stagnant I'm afraid, but once I get the new topics written I might sound
the publishers out about a second edition updating it to the latest version of Python,
whatever that will be by then, and adding the new material. But don't hold your
breath waiting on that one...
Non English Editions now available!
Martin Pozzi was the first to translate the tutor (into Spanish) and he has now been
followed by several others. The following versions are available:
Language Translator
Czech version Petr P
German Bruno Schaefer
Korean johnsonj
Italian Luca Fini
Portuguese Wilson Edgar
Spanish Martin Pozzi
The tutorials above are all usable but at different stages of completeness compared to
the web site. That's because they reflect the state of the web site at the time of
translation. The Czech one in particular is very professionally presented including a
very attractive appearance. I was so impressed with the style that I have used Petr's
stylesheets as the basis for the English version. Hopefully I'll get round to changing
the other language editions too.
I think all of the translators deserve a vote of thanks for their efforts in putting this
together. It's always better to read a translation of an old tutorial than to struggle
reading a new one in a foreign language! If anyone else wishes to do a translation, or
update any of the existing ones, just send me an email.
As mentioned above there is a Japanese version of my book available too.
New Stuff
Page 8
Description:Learn to Program Using Python is a rarity in today's marketplace: a great introduction to programming for true beginners. It started as a hugely popular Web tutorial; reflecting comments from learners worldwide, author Alan Gauld has extended it into book form, adding new material, deeper explanatio