Introduction to Python 3
Python is probably the easiest to learn and nicest to use programming language in widespread use. Python is clear to read and write, and is concise with-out being cryptic. Python is a very expressive language, which means that we can usually write far fewer lines of Python code than would be required for an equivalent application written in, say, C++ or Java.
Python is a cross-platform language: in general, the same Python program can be run on Windows and Unix-like systems such as Linux, BSD, and Mac OS X, simply by copying the file or files that make up the program to the target machine, with no "building" or compiling necessary. It is possible to create Python programs that use platform-specific functionality, but this is rarely necessary since almost all of Python's standard library and most third party libraries, are fully and transparently cross-platform.
One of Python's great strengths is that it comes with a very complete standard library—this allows us to do such things as download a file from the Internet, unpack a compressed archive file, or create a web server, all with just one or a few lines of code. And in addition to the standard library, thousands of third party libraries are available, some providing more powerful and sophisticated facilities than the standard library—for example, the Twisted networking library and the NumPy numeric library—while others provide functionality that is too specialized to be included in the standard library—for example, the SimPy simulation package. Most of the third party libraries are available from the Python Package Index, pypi.python.org/pypi.
Python can be used to program in procedural, object-oriented, and to a lesser extent, in functional style, although at heart Python is an object-oriented language. This book shows how to write both procedural and object-oriented programs, and also teaches Python's functional programming features.
The purpose of this book is to show you how to write Python programs in good idiomatic Python 3 style, and to be a useful reference for the Python 3 language after the initial reading. Although Python 3 is an evolutionary rather than revolutionary advance on Python 2, some older practices are no longer appropriate or necessary in Python 3, and new practices have been introduced to take advantage of Python 3 features. Python 3 is a better language than Python 2—it builds on the many years of experience with Python 2 and adds lots of new features (and omits Python 2's mis-features), to make it even more of a pleasure to use than Python 2, as well as more convenient, easier, and more consistent.
The book's aim is to teach the Python language, and although many of the standard Python libraries are used, not all of them are. This is not a problem because once you have read the book, you will have enough Python knowledge to be able to make use of any of the standard libraries, or any third party Python library—and be able to create library modules of your own.
The book is designed to be useful to several different audiences, including self-taught and hobbyist programmers, students, scientists, engineers and others who need to program as part of their work, and of course, computing professionals and computer scientists. To be of use to such a wide range of people without boring the knowledgeable or losing the less-experienced, the book assumes at least some programming experience (in any language). In particular, it assumes a basic knowledge of data types (such as numbers and strings), collection data types (such as sets and lists), control structures (such as if and while statements), and functions. In addition, some examples and exercises assume a basic knowledge of HTML markup, and some of the more specialized chapters at the end assume a basic knowledge of their subject area, for example, the databases chapter assumes a basic knowledge of SQL.
The book is structured in such a way as to make you as productive as possible as quickly as possible. By the end of the first chapter you will be able to write small but useful Python programs. Each successive chapter introduces new topics, and often both broadens and deepens the coverage of topics introduced in earlier chapters. This means that if you read the chapters in sequence, you can stop at any point and be able to write complete programs with what you have learnt up to that point, and can of course resume reading to learn more advanced and sophisticated techniques when you are ready. For this reason, some topics are introduced in one chapter, and then explored further in one or more later chapters.
Two key problems arise when teaching a new programming language. The first is that sometimes when it is necessary to teach one particular concept, that concept depends on another concept, which in turn depends either directly or indirectly on the first. The second is that, at the beginning, the reader may know little or nothing of the language, so it is very difficult to present interesting or useful examples and exercises. In this book, we seek to solve both these problems, first by assuming some prior programming experience, and second by presenting Python's "beautiful heart" in Chapter 1—eight key pieces of Python that are sufficient on their own to write decent programs. One consequence of this approach is that in the early chapters some of the examples are a bit artificial in style since they only use what has been taught up to the point where they are presented; this effect diminishes chapter by chapter, until by the end of Chapter 7, all the examples are written in completely natural and idomatic Python 3 style.
The book's approach is wholly practical, and you are encouraged to try out the examples and exercises for yourself to get hands-on experience. Wherever possible, small but complete programs are used as examples, to provide realistic use cases. The examples and excercise solutions are available online at www.qtrac.eu/py3book.html—all of them have been tested with Python 3.0 final on Windows and Unix.
The Structure of the Book
Chapter 1 presents eight key pieces of Python that are sufficient to write complete programs with. It also describes some of the Python programming environments that are available, and presents two tiny example programs both built using the eight key pieces of Python covered earlier in the chapter.
Chapters 2 to 5 introduce Python's procedural programming features, including its basic data types and collection data types, and many useful built-in functions and control structures, as well as very simple text file handling. Chapter 5 shows how to create custom modules and packages and provides an overview of Python's standard library, so that you will have a good idea of the functionality that Python provides out of the box and can avoid reinventing the wheel.
Chapter 6 introduces object-oriented programming with Python. All of the material on procedural programming that was learnt in earlier chapters is still applicable, since object-oriented programming is built on procedural foundations—for example, making use of the same data types, collection data types, and control structures.
Chapter 7 covers writing and reading files. For binary files, the coverage includes compression and random access, and for text files, the coverage includes parsing manually and with regular expressions. This chapter also shows how to write and read XML files, including using element trees, DOM (Document Object Model), and SAX (Simple API for XML).
Chapter 8 revisits material covered in some earlier chapters, exploring many of Python's more advanced features in the areas of data types and collection data types, control structures, functions, and object oriented programming. This chapter also introduces many new functions, classes, and advanced techniques, including functional-style programming—the material it covers is both challenging and rewarding.
The remaining chapters cover other advanced topics. Chapter 9 shows techniques for spreading a program's workload over multiple processes and over multiple threads. Chapter 10 shows how to write client/server applications using Python's standard networking support. Chapter 11 covers database programming (both simple key–value "DBM" files, and SQL databases). Chapter 12 explains and illustrates Python's regular expression mini-language and covers the regular expressions module, and Chapter 13 introduces GUI (Graphical User Interface) programming.
Most of the book's chapters are quite long to keep all the relevant material together in one place for ease of reference. However, the chapters are broken down into sections, subsections, and sometimes subsubsections, so it is easy to read at a pace that suits you, for example, reading one section at a time.