I l@ve RuBoard Next Section

About This Book

This book provides a quick introduction to the Python programming language. Python is a popular object-oriented language used for both standalone programs and scripting applications in a variety of domains. It's free, portable, powerful, and remarkably easy to use. Whether you're new to programming or a professional developer, this book's goal is to bring you up to speed on the core Python language in a hurry. Before we jump into details, we'd like to use this preface to say a few words about the book's design.

This Book's Scope

Although this text covers the essentials of the Python language, we've kept its scope narrow in the interest of speed and size. Put another way, the presentation is focused on core concepts and is sometimes deliberately simplistic. Because of that, this book is probably best described as both an introduction and a stepping stone to more advanced and complete texts.

For example, we won't say anything about Python/C integration—a big, complicated topic, with lots of big, complicated examples, which is nevertheless central to many Python-based systems. We also won't talk much about the Python community, Python's history, or some of the philosophies underlying Python development. And popular Python applications such as GUIs, system tools, network scripting, and numeric programming get only a short survey at the end (if they are mentioned at all). Naturally, this misses some of the big picture.

By and large, Python is about raising the quality bar a few notches in the scripting world. Some of its ideas require more context than can be provided here, and we'd be remiss if we didn't recommend further study after you finish this text. We hope that most readers of this book will eventually go on to gain a deeper and more complete understanding, from texts such as O'Reilly's Programming Python. The rest of the Python story requires studying examples that are more realistic than there is space for here.[1]

[1] See http://www.ora.com and http://www.python.org for details on supplemental Python texts. Programming Python was written by one of this book's authors. As its title implies, it discusses practical programming issues in detail.

But despite its limited scope (and perhaps because of it), we think you'll find this to be a great first book on Python. You'll learn everything you need to get started writing useful standalone Python programs and scripts. By the time you've finished this book, you will have learned not only the language itself, but also how to apply it to day-to-day tasks. And you'll be equipped to tackle more advanced topics as they come your way.

This Book's Style

Much of this book is based on training materials developed for a three-day hands-on Python course. You'll find exercises at the end of most chapters, with solutions in Appendix C. The exercises are designed to get you coding right away, and are usually one of the highlights of the course. We strongly recommend working through the exercises along the way, not only to gain Python programming experience, but also because some exercises raise issues not covered elsewhere in the text. The solutions at the end should help if you get stuck (we encourage you to cheat as much and as often as you like). Naturally, you'll need to install Python to run the exercises; more on this in a moment.

Because this text is designed to introduce language basics quickly, we've organized the presentation by major language features, not examples. We'll take a bottom-up approach here: from built-in object types, to statements, to program units, and so on (in fact, if you've seen Appendix E in Programming Python, parts of this book may stir up feelings of déjà vu). Each chapter is fairly self-contained, but later chapters use ideas introduced in earlier ones (e.g., by the time we get to classes, we'll assume you know how to write functions), so a linear reading probably makes the most sense. From a broader perspective, this book is divided into three sections:

Part I

This part of the book presents the Python language, in a bottom-up fashion. It's organized with one chapter per major language feature—types, functions, and so forth—and most of the examples are small and self-contained (some might also call the examples in this section artificial, but they illustrate the points we're out to make). This section represents the bulk of the text, which tells you something about the focus of the book.

Chapter 1

We begin with a quick introduction to Python and then look at how to run Python programs so you can get started coding examples and exercises immediately.

Chapter 2

Next, we explore Python's major built-in object types: numbers, lists, dictionaries, and so on. You can get a lot done in Python with these tools alone.

Chapter 3

The next chapter moves on to introduce Python's statements—the code you type to create and process objects in Python.

Chapter 4

This chapter begins our look at Python's higher-level program structure tools. Functions turn out to be a simple way to package code for reuse.

Chapter 5

Python modules let you organize statements and functions into larger components, and this chapter illustrates how to create, use, and reload modules on the fly.

Chapter 6

Here we explore Python's object-oriented programming (OOP) tool, the class. As you'll see, OOP in Python is mostly about looking up names in linked objects.

Chapter 7

We wrap up the section with a look at Python's exception handling model and statements. This comes last, because exceptions can be classes if you want them to be.

Part II

In this section, we sample Python's built-in tools, and put them to use in a more or less random collection of small example programs.

Chapter 8Built-in Tools

This chapter presents a selection of the modules and functions that are included in the default Python installation. By definition, they comprise the minimum set of modules you can reasonably expect any Python user to have access to. Knowing the contents of this standard toolset will likely save you weeks of work.

Chapter 9Common Tasks in Python

This chapter presents a few nontrivial programs. By building on the language core explained in Part I and the built-in tools described in Chapter 8, we present many small but useful programs that show how to put it all together. We cover three areas that are of interest to most Python users: basic tasks, text processing, and system interfaces.

Chapter 10Frameworks and Applications

This final chapter shows how real applications can be built, leveraging on more specialized libraries that are either part of the standard Python distribution or freely available from third parties. The programs in this chapter are the most complex, but also the most satisfying to work through. We close with a brief discussion of JPython, the Java port of Python, and a substantial JPython program.

Part III

The book ends with three appendixes that list Python resources on the Net (Appendix A), give platform-specific tips for using Python on Unix, Windows, and Macintosh-based machines (Appendix B), and provide solutions to exercises that appear at the end of chapters (Appendix C). Note: the index can be used to hunt for details, but there are no reference appendixes in this book per se. The Python Pocket Reference from O'Reilly (http://www.ora.com), as well as the free Python reference manuals maintained at http://www.python.org, will fill in the details.

Prerequisites

There are none to speak of, really. This is an introductory-level book. It may not be an ideal text for someone who has never touched a computer before (for instance, we're not going to spend a lot of time explaining what a computer is), but we haven't made many assumptions about your programming background or education. On the other hand, we won't insult readers by assuming they are "dummies" either (whatever that means); it's easy to do useful things in Python, and we hope to show you how. The text occasionally contrasts Python with languages such as C, C++, and Pascal, but you can safely ignore these comparisons if you haven't used such languages in the past.

One thing we should probably mention up front: Python's creator, Guido van Rossum, named it after the BBC comedy series Monty Python's Flying Circus. Because of this legacy, many of the examples in this book use references to that show. For instance, the traditional "foo" and "bar" become "spam" and "eggs" in the Python world. You don't need to be familiar with the series to make sense of the examples (symbols are symbols), but it can't hurt.

Book Updates

Improvements happen (and so do mis^H^H^H typos). Updates, supplements, and corrections for this book will be maintained (or referenced) on the Web, at one of the following sites:

If we could be more clairvoyant, we would, but the Web tends to change faster than books.

I l@ve RuBoard Next Section