Learning Python 4th Edition
Download
About This Book
This section underscores some important points about this book in general, regardless
of its edition number. No book addresses every possible audience, so it’s important to
understand a book’s goals up front.
This Book’s Prerequisites
There are no absolute prerequisites to speak of, really. Both true beginners and crusty
programming veterans have used this book successfully. If you are motivated to learn
Python, this text will probably work for you. In general, though, I have found that any
exposure to programming or scripting before this book can be helpful, even if not
required for every reader.
This book is designed to be an introductory-level Python text for programmers.*
It may
not be an ideal text for someone who has never touched a computer before (for instance,
we’re not going to spend any time exploring what a computer is), but I haven’t made
many assumptions about your programming background or education.
On the other hand, I won’t insult readers by assuming they are “dummies,” either,
whatever that means—it’s easy to do useful things in Python, and this book will show
you how. The text occasionally contrasts Python with languages such as C, C++, Java,
and Pascal, but you can safely ignore these comparisons if you haven’t used such languages
in the past
This Book’s Style and Structure
This book is based on training materials developed for a three-day hands-on Python
course. You’ll find quizzes at the end of each chapter, and exercises at the end of the
last chapter of each part. Solutions to chapter quizzes appear in the chapters themselves,
and solutions to part exercises show up in Appendix B. The quizzes are designed to
review material, while the exercises are designed to get you coding right away and are
usually one of the highlights of the course.
I strongly recommend working through the quizzes and exercises along the way, not
only to gain Python programming experience, but also because some of the exercises
raise issues not covered elsewhere in the book. The solutions in the chapters and in
Appendix B should help you if you get stuck (and you are encouraged to peek at the
answers as much and as often as you like).
The overall structure of this book is also derived from class materials. Because this text
is designed to introduce language basics quickly, I’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. Each chapter is fairly
self-contained, but later chapters draw upon ideas introduced in earlier ones (e.g., by
the time we get to classes, I’ll assume you know how to write functions), so a linear
reading makes the most sense for most readers.
In general terms, this book presents the Python language in a linear fashion. It is organized
with one part 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 text artificial, but they illustrate the points it aims to make). More specifically,
here is what you will find:
Part I, Getting Started
We begin with a general overview of Python that answers commonly asked initial
questions—why people use the language, what it’s useful for, and so on. The first
chapter introduces the major ideas underlying the technology to give you some
background context. Then the technical material of the book begins, as we explore
the ways that both we and Python run programs. The goal of this part of the book
is to give you just enough information to be able to follow along with later examples
and exercises.
Part II, Types and Operations
Next, we begin our tour of the Python language, studying Python’s major built-in
object types in depth: numbers, lists, dictionaries, and so on. You can get a lot done
in Python with these tools alone. This is the most substantial part of the book
because we lay groundwork here for later chapters. We’ll also look at dynamic
typing and its references—keys to using Python well—in this part.
Part III, Statements and Syntax
The next part moves on to introduce Python’s statements—the code you type to
create and process objects in Python. It also presents Python’s general syntax
model. Although this part focuses on syntax, it also introduces some related tools,
such as the PyDoc system, and explores coding alternatives.
Part IV, Functions
This part begins our look at Python’s higher-level program structure tools. Functions
turn out to be a simple way to package code for reuse and avoid code redundancy.
In this part, we will explore Python’s scoping rules, argument-passing
techniques, and more.
Part V, Modules
Python modules let you organize statements and functions into larger components,
and this part illustrates how to create, use, and reload modules. We’ll also look at
some more advanced topics here, such as module packages, module reloading, and
the __name__ variable.
Part VI, Classes and OOP
Here, we explore Python’s object-oriented programming tool, the class—an optional
but powerful way to structure code for customization and reuse. As you’ll
see, classes mostly reuse ideas we will have covered by this point in the book, and
OOP in Python is mostly about looking up names in linked objects. As you’ll also
see, OOP is optional in Python, but it can shave development time substantially,
especially for long-term strategic project development.
Part VII, Exceptions and Tools
We conclude the language fundamentals coverage in this text with a look at Python’s
exception handling model and statements, plus a brief overview of development
tools that will become more useful when you start writing larger programs
(debugging and testing tools, for instance). Although exceptions are a fairly lightweight
tool, this part appears after the discussion of classes because exceptions
should now all be classes.
Part VIII, Advanced Topics (new in the fourth edition)
In the final part, we explore some advanced topics. Here, we study Unicode and
byte strings, managed attribute tools like properties and descriptors, function and
class decorators, and metaclasses. These chapters are all optional reading, because
not all programmers need to understand the subjects they address. On the other
hand, readers who must process internationalized text or binary data, or are responsible
for developing APIs for other programmers to use, should find something
of interest in this part.
Part IX, Appendixes
The book wraps up with a pair of appendixes that give platform-specific tips for
using Python on various computers (Appendix A) and provide solutions to the endof-part
exercises (Appendix B). Solutions to end-of-chapter quizzes appear in the
chapters themselves.
Note that the index and table of contents can be used to hunt for details, but there are
no reference appendixes in this book (this book is a tutorial, not a reference). As mentioned
earlier, you can consult Python Pocket Reference, as well as other books, and the
free Python reference manuals maintained at http://www.python.org for syntax and
built-in tool details.