Readers of the Fourth Edition of Learning Python might recognize some
aspects of the running example used in this chapter—the characters here
are similar in spirit to those in the OOP tutorial chapter in that book,
and the later class-based examples here are essentially a variation on a
theme. Despite some redundancy, I’m revisiting the example here for
three reasons: it serves its purpose as a review of language fundamentals;
some readers of this book haven’t read Learning Python; and the exam-
ple receives expanded treatment here, with the addition of GUI and Web
interfaces. That is, this chapter picks up where Learning Python left off,
pushing this core language example into the realm of realistic applica-
tions—which, in a nutshell, reflects the purpose of this book.
The Task
Imagine, if you will, that you need to keep track of information about people for some
reason. Maybe you want to store an address book on your computer, or perhaps you
need to keep track of employees in a small business. For whatever reason, you want to
write a program that keeps track of details about these people. In other words, you
want to keep records in a database—to permanently store lists of people’s attributes
on your computer.
Naturally, there are off-the-shelf programs for managing databases like these. By writ-
ing a program for this task yourself, however, you’ll have complete control over its
operation. You can add code for special cases and behaviors that precoded software
may not have anticipated. You won’t have to install and learn to use yet another data-
base product. And you won’t be at the mercy of a software vendor to fix bugs or add
new features. You decide to write a Python program to manage your people.
Step 1: Representing Records
If we’re going to store records in a database, the first step is probably deciding what
those records will look like. There are a variety of ways to represent information about
people in the Python language. Built-in object types such as lists and dictionaries are
often sufficient, especially if we don’t initially care about processing the data we store.
Using Lists
Lists, for example, can collect attributes about people in a positionally ordered way.
Start up your Python interactive interpreter and type the following two statements:
>>> bob = ['Bob Smith', 42, 30000, 'software']
>>> sue = ['Sue Jones', 45, 40000, 'hardware']
4 | Chapter 1: A Sneak Preview