This book is intended for people who already have experience in one or more programming languages and want to learn the basics of Python 3 as quickly and directly as possible. Although some basic concepts are covered, there’s no attempt to teach basic programming skills in this book, and the basic concepts of flow control, OOP, file access, exception handling, and the like are assumed. This book may also be of use to users of earlier versions of Python who want a concise reference for Python 3.
Part 1 introduces Python and explains how to download and install it on your system. It also includes a very general survey of the language, which will be most useful for experienced programmers looking for a high-level view of Python.
Part 2 is the heart of the book. It covers the ingredients necessary for obtaining a working knowledge of Python as a general-purpose programming language. The chapters are designed to allow readers who are beginning to learn Python to work their way through sequentially, picking up knowledge of the key points of the language. These chapters also contain some more advanced sections, allowing you to return to find in one place all the necessary information about a construct or topic.
Part 3 introduces advanced language features of Python, elements of the language that aren’t essential to its use but that can certainly be a great help to a serious Python programmer.
Part 4 describes more advanced or specialized topics that are beyond the strict syntax of the language. You may read these chapters or not, depending on your needs.
A suggested plan if you’re new to Python is to start by reading chapter 3 to obtain an overall perspective and then work through the chapters in part 2 that are applicable. Enter in the interactive examples as they are introduced. This will immediately reinforce the concepts. You can also easily go beyond the examples in the text to answer questions about anything that may be unclear. This has the potential to amplify the speed of your learning and the level of your comprehension. If you aren’t familiar with OOP or don’t need it for your application, skip most of chapter 15. If you aren’t interested in developing a GUI, skip chapter 16.
Those familiar with Python should also start with chapter 3. It will be a good review and will introduce differences between Python 3 and what may be more familiar. It’s a reasonable test of whether you’re ready to move on to the advanced chapters in parts 3 and 4 of this book.
It’s possible that some readers, although new to Python, will have enough experience with other programming languages to be able to pick up the bulk of what they need to get going from chapter 3 and by browsing the Python standard library modules listed in chapter 23 and the Python Library Reference in the Python documentation.
Chapter 1 discusses the strengths and weaknesses of Python and shows why Python 3 is a good choice of programming language for many situations.
Chapter 2 covers downloading, installing, and starting up the Python interpreter and IDLE, its integrated development environment.
Chapter 3 is a short overview of the Python language. It provides a basic idea of the philosophy, syntax, semantics, and capabilities of the language.
Chapter 4 starts with the basics of Python. It introduces Python variables, expressions, strings, and numbers. It also introduces Python’s block-structured syntax.
Chapters 5, 6, and 7 describe the five powerful built-in Python data types: lists, tuples, sets, strings, and dictionaries.
Chapter 8 introduces Python’s control flow syntax and use (loops and if-else statements).
Chapter 9 describes function definition in Python along with its flexible parameter-passing capabilities.
Chapter 10 describes Python modules. They provide an easy mechanism for segmenting the program namespace.
Chapter 11 covers creating standalone Python programs, or scripts, and running them on Windows, Mac OS X, and Linux platforms. The chapter also covers the support available for command-line options, arguments, and I/O redirection.
Chapter 12 describes how to work and navigate through the files and directories of the filesystem. It shows how to write code that’s as independent as possible of the actual operating system you’re working on.
Chapter 13 introduces the mechanisms for reading and writing files in Python. These include the basic capability to read and write strings (or byte streams), the mechanism available for reading binary records, and the ability to read and write arbitrary Python objects.
Chapter 14 discusses the use of exceptions, the error-handling mechanism used by Python. It doesn’t assume that you have any prior knowledge of exceptions, although if you’ve previously used them in C++ or Java, you’ll find them familiar.
Chapter 15 introduces Python’s support for writing object-oriented programs.
Chapter 16 focuses on the available Tkinter interface and ends with an introduction to some of the other options available for developing GUIs.
Chapter 17 discusses the regular-expression capabilities available for Python.
Chapter 18 introduces the package concept in Python for structuring the code of large projects.
Chapter 19 covers the simple mechanisms available to dynamically discover and work with data types in Python.
Chapter 20 introduces more advanced OOP techniques, including the use of Python’s special method-attributes mechanism, metaclasses, and abstract base classes.
Chapter 21 covers two strategies that Python offers for testing your code: doctests and unit testing.
Chapter 22 surveys the process, issues, and tools involved in porting code from earlier versions of Python to Python 3.
Chapter 23 is a brief survey of the standard library and also includes a discussion of where to find other modules and how to install them.
Chapter 24 is a brief introduction to using Python for database and web programming. A small web application is developed to illustrate the principles involved.
The appendix contains a comprehensive guide to obtaining and accessing Python’s full documentation, the Pythonic style guide, PEP 8, and “The Zen of Python,” a slightly wry summary of the philosophy behind Python.
The code samples in this book, and their output, appear in a fixed-width font and are often accompanied by annotations. The code samples are deliberately kept as simple as possible, because they aren’t intended to be reusable parts that can plugged into your code. Instead, the code samples are stripped down so that you can focus on the principle being illustrated.
In keeping with the idea of simplicity, the code examples are presented as interactive shell sessions where possible; you should enter and experiment with these samples as much as you can. In interactive code samples, the commands that need to be entered are on lines that begin with the >>> prompt, and the visible results of that code (if any) are on the line below.
In some cases a longer code sample is needed, and these are identified in the text as file listings. You should save these as files with names matching those used in the text and run them as standalone scripts.
The source code for the samples in this book is available from the publisher’s website at www.manning.com/TheQuickPythonBookSecondEdition.
The samples and code in this book have been written with Windows (XP through Windows 7), Mac OS X, and Linux in mind. Because Python is a cross-platform language, they should work on other platforms for the most part, except for platform-specific issues, like the handling of files, paths, and GUIs.
This book is based on Python 3.1, and all examples should work on any subsequent version of Python 3. The examples also work on Python 3.0, but I strongly recommend using 3.1—there are no advantages to the earlier version, and 3.1 has several subtle improvements. Note that Python 3 is required and that an earlier version of Python will not work with the code in this book.
The purchase of The Quick Python Book, Second Edition includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/TheQuickPythonBookSecondEdition. This page provides information about how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It’s not a commitment to any specific amount of participation on the part of the author, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking him some challenging questions, lest his interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
Second edition author Naomi Ceder has been programming in various languages for over 20 years and has been a Linux system administrator since 2000. She started using Python for a variety of projects in 2001. She is currently IT Director at Zoro Tools, Inc., in Buffalo Grove, Illinois, and an organizer and teacher of the Chicago Python Workshops.
The illustration on the cover of The Quick Python Book, Second Edition is taken from a late 18th century edition of Sylvain Maréchal’s four-volume compendium of regional dress customs published in France. Each illustration is finely drawn and colored by hand. The rich variety of Maréchal’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by what they were wearing.
Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns or regions. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.
At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Maréchal’s pictures.