This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
Preface
In 2013, when we incorporated our Norway-based software consultancy and trainingbusiness Sixty North, we were courted by Pluralsight, a publisher of onlinevideo training material, to produce Python training videos for the rapidlygrowing MOOC market. At the time, we had no experience of producing videotraining material, but we were sure we wanted to carefully structure ourintroductory Python content to respect certain constraints. For example, wewanted an absolute minimum of forward references since those would be veryinconvenient for our viewers. Were both men of words who live by Turing Awardwinner Leslie Lamports maxim If youre thinking without writing you onlythink youre thinking, so it was natural for us to attack video courseproduction by first writing a script.
In the intervening years we worked on three courses with Pluralsight: PythonFundamentals,Python Beyond the Basics,and Advanced Python.These three online courses have been transformed into three booksThe Python Apprentice,The Python Journeyman, and this one,The Python Master.
You can read The Python Master either as a standalone Python tutorial, or asthe companion volume to the corresponding Advanced Python video course,depending on which style of learning suits you best. In either case we assumethat youre up to speed with the material covered in the preceding books orcourses.
Errata and Suggestions
All the material in this book has been thoroughly reviewed and tested;nevertheless, its inevitable that some mistakes have crept in. If you do spota mistake, wed really appreciate it if youd let us know via the LeanpubPython Master Discussionpage so we can make amends and deploy a new version.
Conventions Used in This Book
Code examples in this book are shown in a fixed-width text which is colored withsyntax highlighting:
>>>
def
square
(
x
):
...
return
x
*
x
...
Some of our examples show code saved in files, and others such as the oneabove are from interactive Python sessions. In such interactive cases, weinclude the prompts from the Python session such as the triple-arrow(>>>
) and triple-dot (...
) prompts. You dont need to type these arrows ordots. Similarly, for operating system shell-commands we will use a dollar prompt($
) for Linux, macOS and other Unixes, or where the particular operatingsystem is unimportant for the task at hand:
$ python3 words.py
In this case, you dont need to type the $
character.
For Windows-specific commands we will use a leading greater-than prompt:
> python words.py
Again, theres no need to type the >
character.
For code blocks which need to be placed in a file, rather than enteredinteractively, we show code without any leading prompts:
def
write_sequence
(
filename
,
num
):
"""Write Recaman's sequence to a text file."""
with
open
(
filename
,
mode
=
'wt'
,
encoding
=
'utf-8'
)
as
f
:
f
.
writelines
(
"{0}
\n
"
.
format
(
r
)
for
r
in
islice
(
sequence
(),
num
+
1
))
Weve worked hard to make sure that our lines of code are short enough so thateach single logical line of code corresponds to a single physical line of codein your book. However, the vagaries of publishing e-books to differentdevices and the very genuine need for occasional long lines of code mean wecant guarantee that lines dont wrap. What we can guarantee, however, is thatwhere a line does wrap, the publisher has inserted a backslash character \
inthe final column. You need to use your judgement to determine whether thischaracter is legitimate part of the code or has been added by the e-bookplatform.
>>>
print
(
"This is a single line of code which is very long. Too long, in fact, to fit on
\
a single physical line of code in the book."
)
If you see a backslash at the end of the line within the above quoted string, itis not part of the code, and should not be entered.
Occasionally, well number lines of code so we can refer to them easily fromthe narrative next. These line numbers should not be entered as part of thecode. Numbered code blocks look like this:
1
def
write_grayscale
(
filename
,
pixels
):
2
height
=
len
(
pixels
)
3
width
=
len
(
pixels
[
0
])
4
5
with
open
(
filename
,
'wb'
)
as
bmp
:
6
# BMP Header
7
bmp
.
write
(
b
'BM'
)
8
9
# The next four bytes hold the filesize as a 32-bit
10
# little-endian integer. Zero placeholder for now.
11
size_bookmark
=
bmp
.
tell
()
12
bmp
.
write
(
b
'
\x00\x00\x00\x00
'
)
Sometimes we need to present code snippets which are incomplete. Usually thisis for brevity where we are adding code to an existing block, and where we wantto be clear about the block structure without repeating all existing contents ofthe block. In such cases we use a Python comment containing three dots # ...
to indicate the elided code:
class
Flight
:
# ...
def
make_boarding_cards
(
self