Download from Wow! eBook
What Readers Are Saying About
Practical Programming, Second Edition
I wish I could go back in time and give this book to my 10-year-old self when I
first learned programming! It’s so much more engaging, practical, and accessible
than the dry introductory programming books that I tried (and often failed) to
comprehend as a kid. I love the authors’ hands-on approach of mixing explanations
with code snippets that students can type into the Python prompt.
➤ Philip Guo
Creator of Online Python Tutor (www.pythontutor.com), Assistant Professor, Depart-
ment of Computer Science, University of Rochester
Practical Programming delivers just what it promises: a clear, readable, usable
introduction to programming for beginners. This isn’t just a guide to hacking
together programs. The book provides foundations to lifelong programming skills:
a crisp, consistent, and visual model of memory and execution and a design recipe
that will help readers produce quality software.
➤ Steven Wolfman
Senior Instructor, Department of Computer Science, University of British
Columbia
Download from Wow! eBook
The second edition of this excellent text reflects the authors’ many years of expe-
rience teaching Python to beginning students. Topics are presented so that each
leads naturally to the next, and common novice errors and misconceptions are
explicitly addressed. The exercises at the end of each chapter invite interested
students to explore computer science and programming language topics.
➤ Kathleen Freeman
Director of Undergraduate Studies, Department of Computer and Information
Science, University of Oregon
Download from Wow! eBook
Practical Programming, 2nd Edition
An Introduction to Computer Science Using Python 3
Paul Gries
Jennifer Campbell
Jason Montojo
The Pragmatic Bookshelf
Dallas, Texas • Raleigh, North Carolina
Download from Wow! eBook
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and The Pragmatic
Programmers, LLC was aware of a trademark claim, the designations have been printed in
initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,
Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-
marks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes
no responsibility for errors or omissions, or for damages that may result from the use of
information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create
better software and have more fun. For more information, as well as the latest Pragmatic
titles, please visit us at http://pragprog.com.
The team that produced this book includes:
Lynn Beighley (editor)
Potomac Indexing, LLC (indexer)
Molly McBeath (copyeditor)
David J Kelly (typesetter)
Janet Furlow (producer)
Juliet Benda (rights)
Ellie Callahan (support)
Copyright © 2013 The Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form, or by any means, electronic, mechanical, photocopying,
recording, or otherwise, without the prior consent of the publisher.
Printed in the United States of America.
ISBN-13: 978-1-93778-545-1
Encoded using the finest acid-free high-entropy binary digits.
Book version: P1.0—September 2013
Download from Wow! eBook
Contents
Acknowledgments
.
.
.
.
.
.
.
.
.
.
.
xi
Preface .
.
.
.
.
.
.
.
.
.
.
.
.
.
xiii
1. What’s Programming?
.
.
.
.
.
.
.
.
.
.
Programs and Programming
1.1
1.2 What’s a Programming Language?
1.3 What’s a Bug?
1.4
The Difference Between Brackets, Braces, and
Parentheses
Installing Python
1.5
2.
3.
.
.
.
.
.
.
.
.
.
.
Hello, Python .
.
2.1
How Does a Computer Run a Python Program?
2.2 Expressions and Values: Arithmetic in Python
2.3 What Is a Type?
2.4 Variables and Computer Memory: Remembering Values
2.5 How Python Tells You Something Went Wrong
2.6 A Single Statement That Spans Multiple Lines
2.7 Describing Code
2.8 Making Code Readable
2.9
2.10 Exercises
The Object of This Chapter
.
.
.
.
.
.
Functions That Python Provides
Designing and Using Functions .
3.1
3.2 Memory Addresses: How Python Keeps Track of Values
3.3 Defining Our Own Functions
3.4 Using Local Variables for Temporary Storage
3.5
Tracing Function Calls in the Memory Model
3.6 Designing New Functions: A Recipe
3.7 Writing and Running a Program
.
.
Download from Wow! eBook
1
2
3
4
5
5
7
7
9
12
15
22
23
25
26
27
27
31
31
34
35
39
40
47
59
Contents • vi
3.8 Omitting a Return Statement: None
3.9 Dealing with Situations That Your Code Doesn’t Handle
3.10 What Did You Call That?
3.11 Exercises
4. Working with Text
.
.
.
.
.
.
.
.
.
.
.
Creating Strings of Characters
4.1
4.2 Using Special Characters in Strings
4.3 Creating a Multiline String
4.4
4.5 Getting Information from the Keyboard
4.6 Quotes About Strings in This Text
4.7 Exercises
Printing Information
5. Making Choices .
.
.
.
.
.
.
.
.
.
.
.
A Boolean Type
5.1
5.2 Choosing Which Statements to Execute
5.3 Nested If Statements
5.4 Remembering the Results of a Boolean Expression
Evaluation
You Learned About Booleans: True or False?
5.5
5.6 Exercises
6.
7.
Importing Modules
A Modular Approach to Program Organization
6.1
6.2 Defining Your Own Modules
6.3
6.4
6.5 Organizing Our Thoughts
6.6 Exercises
Testing Your Code Semiautomatically
Tips for Grouping Your Functions
.
.
.
.
.
.
.
Modules, Classes, and Methods
Using Methods .
7.1
7.2 Calling Methods the Object-Oriented Way
7.3 Exploring String Methods
7.4 What Are Those Underscores?
7.5 A Methodical Review
7.6 Exercises
.
.
.
.
.
.
.
.
8.
Storing Collections of Data Using Lists .
8.1
Storing and Accessing Data in Lists
8.2 Modifying Lists
.
.
.
.
.
Download from Wow! eBook
60
61
62
63
65
65
68
70
70
73
74
75
77
77
86
92
92
94
94
99
100
104
109
112
113
114
115
115
117
119
123
125
125
129
129
133