Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Python Apprentice

You're reading from   The Python Apprentice Introduction to the Python Programming Language

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781788293181
Length 352 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Austin Bingham Austin Bingham
Author Profile Icon Austin Bingham
Austin Bingham
Robert Smallshire Robert Smallshire
Author Profile Icon Robert Smallshire
Robert Smallshire
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting started FREE CHAPTER 2. Strings and Collections 3. Modularity 4. Built-in types and the object model 5. Exploring Built-in Collection types 6. Exceptions 7. Comprehensions, iterables, and generators 8. Defining new types with classes 9. Files and Resource Management 10. Unit testing with the Python standard library 11. Debugging with PDB 12. Afterword – Just the Beginning
13. Virtual Environments 14. Packaging and Distribution 15. Installing Third-Party Packages

Conventions Used in This Book

Code examples in this book are shown in a lucida console text:

>>> def square(x):
... return x * x
...

Some of our examples show code saved in files, and others — such as the one above — are from interactive Python sessions. In such interactive cases, we include the prompts from the Python session such as the triple-arrow >>> and triple-dot ... prompts. You don't need to type these arrows or dots. Similarly, for operating system shell-commands we will use a dollar prompt $ for Linux, macOS and other Unixes, or where the particular operating system is unimportant for the task at hand:

$ python3 words.py

In this case, you don't need to type the $ character.

For Windows-specific commands we will use a leading greater-than prompt:

> python words.py

Again, there's no need to type the > character. For code blocks which need to be placed in a file, rather than entered interactively, 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))

We've worked hard to make sure that our lines of code are short enough so that each single logical line of code corresponds to a single physical line of code in your book. However, the vagaries of publishing e-books to different devices and the very genuine need for occasional long lines of code mean we can't guarantee that lines don't wrap. What we can guarantee, however, is that where a line does wrap, the publisher has inserted a backslash character \ in the final column. You need to use your judgement to determine whether this character is legitimate part of the code or has been added by the e-book platform.

>>> 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, it is not part of the code, and should not be entered.

Occasionally, we'll number lines of code so we can refer to them easily from the narrative next. These line numbers should not be entered as part of the code. Numbered code blocks look like this:

def write_grayscale(filename, pixels):
height = len(pixels)
width = len(pixels[0])

with open(filename, 'wb') as bmp:
# BMP Header
bmp.write(b'BM')

# The next four bytes hold the filesize as a 32-bit
# little-endian integer. Zero placeholder for now.
size_bookmark = bmp.tell()
bmp.write(b'\x00\x00\x00\x00')

Sometimes we need to present code snippets which are incomplete. Usually this is for brevity where we are adding code to an existing block, and where we want to be clear about the block structure without repeating all existing contents of the block. In such cases we use a Python comment containing three dots # ... to indicate the elided code:

class Flight:

# ...

def make_boarding_cards(self, card_printer):
for passenger, seat in sorted(self._passenger_seats()):
card_printer(passenger, seat, self.number(),
self.aircraft_model())

Here it is implied that some other code already exists within the Flight class block before the make_boarding_cards() function.

Finally, within the text of the book, when we are referring to an identifier which is also a function we will use the identifier with empty parentheses, just as we did with make_boarding_cards() in the preceding paragraph.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime