Table Of ContentBeginning
Python
From Novice to Professional
—
Third Edition
—
Magnus Lie Hetland
Beginning Python
From Novice to Professional
Third Edition
Magnus Lie Hetland
Beginning Python: From Novice to Professional
Magnus Lie Hetland
Trondheim, Norway
ISBN-13 (pbk): 978-1-4842-0029-2 ISBN-13 (electronic): 978-1-4842-0028-5
DOI 10.1007/978-1-4842-0028-5
Library of Congress Control Number: 2017934891
Copyright © 2017 by Magnus Lie Hetland
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Steve Anglin
Development Editor: Matthew Moodie
Technical Reviewer: Michael Thomas
Coordinating Editor: Mark Powers
Copy Editor: Kim Wimpsett
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover image designed by Shutterstock
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com/us/services/
rights-permission.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web
page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers
for download or cloning at Github via the book's product page, located at www.apress.com/9781484200292.
For more detailed information, please visit http://www.apress.com/us/services/source-code.
Printed on acid-free paper
For Kjersti and Tor.
Contents at a Glance
About the Author ���������������������������������������������������������������������������������������������������xxv
About the Technical Reviewer �����������������������������������������������������������������������������xxvii
Preface �����������������������������������������������������������������������������������������������������������������xxix
Introduction ����������������������������������������������������������������������������������������������������������xxxi
■ Chapter 1: Instant Hacking: The Basics �����������������������������������������������������������������1
■ Chapter 2: Lists and Tuples ���������������������������������������������������������������������������������25
■ Chapter 3: Working with Strings �������������������������������������������������������������������������45
■ Chapter 4: Dictionaries: When Indices Won’t Do �������������������������������������������������59
■ Chapter 5: Conditionals, Loops, and Some Other Statements �����������������������������71
■ Chapter 6: Abstraction���������������������������������������������������������������������������������������101
■ Chapter 7: More Abstraction �����������������������������������������������������������������������������129
■ Chapter 8: Exceptions ����������������������������������������������������������������������������������������149
■ Chapter 9: Magic Methods, Properties, and Iterators����������������������������������������163
■ Chapter 10: Batteries Included ��������������������������������������������������������������������������195
■ Chapter 11: Files and Stuff ��������������������������������������������������������������������������������241
■ Chapter 12: Graphical User Interfaces ��������������������������������������������������������������253
■ Chapter 13: Database Support ���������������������������������������������������������������������������261
■ Chapter 14: Network Programming �������������������������������������������������������������������273
■ Chapter 15: Python and the Web �����������������������������������������������������������������������289
■ Chapter 16: Testing, 1-2-3 ���������������������������������������������������������������������������������307
v
■ Contents at a GlanCe
■ Chapter 17: Extending Python ���������������������������������������������������������������������������321
■ Chapter 18: Packaging Your Programs ��������������������������������������������������������������337
■ Chapter 19: Playful Programming ���������������������������������������������������������������������343
■ Chapter 20: Project 1: Instant Markup ��������������������������������������������������������������353
■ Chapter 21: Project 2: Painting a Pretty Picture������������������������������������������������373
■ Chapter 22: Project 3: XML for All Occasions ����������������������������������������������������383
■ Chapter 23: Project 4: In the News ��������������������������������������������������������������������397
■ Chapter 24: Project 5: A Virtual Tea Party ���������������������������������������������������������409
■ Chapter 25: Project 6: Remote Editing with CGI ������������������������������������������������425
■ Chapter 26: Project 7: Your Own Bulletin Board ������������������������������������������������435
■ Chapter 27: Project 8: File Sharing with XML-RPC ��������������������������������������������451
■ Chapter 28: Project 9: File Sharing II—Now with GUI! ��������������������������������������467
■ Chapter 29: Project 10: Do-It-Yourself Arcade Game ����������������������������������������475
■ Appendix A: The Short Version ��������������������������������������������������������������������������495
■ Appendix B: Python Reference ��������������������������������������������������������������������������503
Index ���������������������������������������������������������������������������������������������������������������������519
vi
Contents
About the Author ���������������������������������������������������������������������������������������������������xxv
About the Technical Reviewer �����������������������������������������������������������������������������xxvii
Preface �����������������������������������������������������������������������������������������������������������������xxix
Introduction ����������������������������������������������������������������������������������������������������������xxxi
■ Chapter 1: Instant Hacking: The Basics �����������������������������������������������������������������1
The Interactive Interpreter ������������������������������������������������������������������������������������������������1
Algo � � � What? �����������������������������������������������������������������������������������������������������������������2
Numbers and Expressions ������������������������������������������������������������������������������������������������3
Hexadecimals Octals and Binary �����������������������������������������������������������������������������������������������������������5
Variables ���������������������������������������������������������������������������������������������������������������������������5
Statements �����������������������������������������������������������������������������������������������������������������������6
Getting Input from the User ����������������������������������������������������������������������������������������������7
Functions ��������������������������������������������������������������������������������������������������������������������������8
Modules ����������������������������������������������������������������������������������������������������������������������������9
cmath and Complex Numbers ��������������������������������������������������������������������������������������������������������������10
Back to the __future__ �������������������������������������������������������������������������������������������������������������������������11
Saving and Executing Your Programs �����������������������������������������������������������������������������11
Running Your Python Scripts from a Command Prompt �����������������������������������������������������������������������13
Making Your Scripts Behave Like Normal Programs ����������������������������������������������������������������������������13
Comments ��������������������������������������������������������������������������������������������������������������������������������������������14
Strings ����������������������������������������������������������������������������������������������������������������������������14
Single-Quoted Strings and Escaping Quotes ����������������������������������������������������������������������������������������15
Concatenating Strings ��������������������������������������������������������������������������������������������������������������������������16
vii
■ Contents
String Representations, str and repr ����������������������������������������������������������������������������������������������������16
Long Strings, Raw Strings, and bytes ���������������������������������������������������������������������������������������������������17
A Quick Summary �����������������������������������������������������������������������������������������������������������22
New Functions in This Chapter �������������������������������������������������������������������������������������������������������������23
What Now? �������������������������������������������������������������������������������������������������������������������������������������������24
■ Chapter 2: Lists and Tuples ���������������������������������������������������������������������������������25
Sequence Overview��������������������������������������������������������������������������������������������������������25
Common Sequence Operations ��������������������������������������������������������������������������������������26
Indexing ������������������������������������������������������������������������������������������������������������������������������������������������26
Slicing ���������������������������������������������������������������������������������������������������������������������������������������������������28
Adding Sequences ��������������������������������������������������������������������������������������������������������������������������������30
Multiplication ����������������������������������������������������������������������������������������������������������������������������������������31
Membership ������������������������������������������������������������������������������������������������������������������������������������������32
Length, Minimum, and Maximum ���������������������������������������������������������������������������������������������������������33
Lists: Python’s Workhorse ����������������������������������������������������������������������������������������������34
The list Function �����������������������������������������������������������������������������������������������������������������������������������34
Basic List Operations ����������������������������������������������������������������������������������������������������������������������������34
List Methods �����������������������������������������������������������������������������������������������������������������������������������������36
Tuples: Immutable Sequences����������������������������������������������������������������������������������������42
A Quick Summary �����������������������������������������������������������������������������������������������������������44
New Functions in This Chapter �������������������������������������������������������������������������������������������������������������44
What Now? �������������������������������������������������������������������������������������������������������������������������������������������44
■ Chapter 3: Working with Strings �������������������������������������������������������������������������45
Basic String Operations ��������������������������������������������������������������������������������������������������45
String Formatting: The Short Version �����������������������������������������������������������������������������45
String Formatting: The Long Version ������������������������������������������������������������������������������47
Replacement Field Names ��������������������������������������������������������������������������������������������������������������������47
Basic Conversions ��������������������������������������������������������������������������������������������������������������������������������48
Width, Precision, and Thousands Separators ���������������������������������������������������������������������������������������49
Signs, Alignment, and Zero-Padding ����������������������������������������������������������������������������������������������������50
viii
■ Contents
String Methods ���������������������������������������������������������������������������������������������������������������52
center ���������������������������������������������������������������������������������������������������������������������������������������������������53
find �������������������������������������������������������������������������������������������������������������������������������������������������������53
lower �����������������������������������������������������������������������������������������������������������������������������������������������������54
replace ��������������������������������������������������������������������������������������������������������������������������������������������������55
split �������������������������������������������������������������������������������������������������������������������������������������������������������55
strip ������������������������������������������������������������������������������������������������������������������������������������������������������56
translate �����������������������������������������������������������������������������������������������������������������������������������������������56
Is My String … �������������������������������������������������������������������������������������������������������������������������������������57
A Quick Summary �����������������������������������������������������������������������������������������������������������57
New Functions in This Chapter �������������������������������������������������������������������������������������������������������������58
What Now? �������������������������������������������������������������������������������������������������������������������������������������������58
■ Chapter 4: Dictionaries: When Indices Won’t Do �������������������������������������������������59
Dictionary Uses ��������������������������������������������������������������������������������������������������������������59
Creating and Using Dictionaries �������������������������������������������������������������������������������������60
The dict Function ����������������������������������������������������������������������������������������������������������������������������������60
Basic Dictionary Operations �����������������������������������������������������������������������������������������������������������������61
String Formatting with Dictionaries ������������������������������������������������������������������������������������������������������63
Dictionary Methods�������������������������������������������������������������������������������������������������������������������������������63
A Quick Summary �����������������������������������������������������������������������������������������������������������70
New Functions in This Chapter �������������������������������������������������������������������������������������������������������������70
What Now? �������������������������������������������������������������������������������������������������������������������������������������������70
■ Chapter 5: Conditionals, Loops, and Some Other Statements �����������������������������71
More About print and import ������������������������������������������������������������������������������������������71
Printing Multiple Arguments �����������������������������������������������������������������������������������������������������������������71
Importing Something as Something Else ���������������������������������������������������������������������������������������������72
Assignment Magic ����������������������������������������������������������������������������������������������������������73
Sequence Unpacking ����������������������������������������������������������������������������������������������������������������������������73
Chained Assignments ���������������������������������������������������������������������������������������������������������������������������75
Augmented Assignments ����������������������������������������������������������������������������������������������������������������������75
ix
■ Contents
Blocks: The Joy of Indentation ���������������������������������������������������������������������������������������76
Conditions and Conditional Statements �������������������������������������������������������������������������76
So That’s What Those Boolean Values Are For ��������������������������������������������������������������������������������������76
Conditional Execution and the if Statement �����������������������������������������������������������������������������������������78
else Clauses �����������������������������������������������������������������������������������������������������������������������������������������78
elif Clauses �������������������������������������������������������������������������������������������������������������������������������������������79
Nesting Blocks ��������������������������������������������������������������������������������������������������������������������������������������79
More Complex Conditions ���������������������������������������������������������������������������������������������������������������������79
Assertions ���������������������������������������������������������������������������������������������������������������������������������������������84
Loops ������������������������������������������������������������������������������������������������������������������������������85
while Loops ������������������������������������������������������������������������������������������������������������������������������������������85
for Loops �����������������������������������������������������������������������������������������������������������������������������������������������86
Iterating Over Dictionaries ��������������������������������������������������������������������������������������������������������������������87
Some Iteration Utilities �������������������������������������������������������������������������������������������������������������������������87
Breaking Out of Loops ��������������������������������������������������������������������������������������������������������������������������89
else Clauses in Loops ���������������������������������������������������������������������������������������������������������������������������92
Comprehensions—Slightly Loopy ����������������������������������������������������������������������������������92
And Three for the Road ���������������������������������������������������������������������������������������������������94
Nothing Happened! �������������������������������������������������������������������������������������������������������������������������������94
Deleting with del �����������������������������������������������������������������������������������������������������������������������������������94
Executing and Evaluating Strings with exec and eval ��������������������������������������������������������������������������96
A Quick Summary �����������������������������������������������������������������������������������������������������������98
New Functions in This Chapter �������������������������������������������������������������������������������������������������������������99
What Now? �������������������������������������������������������������������������������������������������������������������������������������������99
■ Chapter 6: Abstraction���������������������������������������������������������������������������������������101
Laziness Is a Virtue �������������������������������������������������������������������������������������������������������101
Abstraction and Structure ��������������������������������������������������������������������������������������������102
Creating Your Own Functions ���������������������������������������������������������������������������������������102
Documenting Functions ����������������������������������������������������������������������������������������������������������������������103
Functions That Aren’t Really Functions ����������������������������������������������������������������������������������������������104
x