2011
Python Programming FundamentalsAuthors:
- Kent D. Lee
- …show all 1hide
ISBN: 978-1-84996-536-1 (Print) 978-1-84996-537-8 (Online)
Series: Undergraduate Topics in Computer Science
Lee, Kent D.
2011, XII, 241 p.
ISBN 978-1-84996-537-8
Immediately available per PDF-download (no DRM, watermarked)
About this textbook
- Employs a classroom-tested, hands-on learning approach
- Offers numerous examples and solved practice exercises
- An associated website provides additional resources for instructors
Features:
- Contains numerous examples and solved practice exercises designed for an interactive classroom environment
- Highlights several patterns which commonly appear in programs, and presents exercises that reinforce recognition and application of these patterns
- Introduces the use of a debugger, and includes supporting material that reveals how programs work
- Presents the Tkinter framework for building graphical user interface applications and event-driven programs
- Provides helpful additional resources for instructors at the associated website: http://cs.luther.edu/~leekent/CS1
Dr. Kent D. Lee is Associate Professor of Computer Science at the Department of Computer Science at Luther College, Decorah, Iowa, USA. He is the author of the successful Springer textbook, Programming Languages: An Active Learning Approach.
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 The Python Programming Language . . . . . . . . . . . . . . . . . . . 2
1.2 Installing Python and Wing IDE 101 . . . . . . . . . . . . . . . . . . . 3
1.3 Writing Your First Program . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 What is aComputer? . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Binary Number Representation . . . . . . . . . . . . . . . . . . . . . . 10
1.6 What is a Programming Language? . . . . . . . . . . . . . . . . . . . 13
1.7 Hexadecimal and Octal Representation . . . . . . . . . . . . . . . . . . 15
1.8 Writing Your Second Program . . . . . . . . . . . . . . . . . . . . . . 16
1.9 SyntaxErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.10 Types of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.11 The Reference Type and Assignment Statements . . . . . . . . . . . . 20
1.12 Integers and Real Numbers . . . . . . . . . . . . . . . . . . . . . . . . 21
1.13 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.14 Integer to String Conversion and Back Again . . . . . . . . . . . . . . 25
1.15 Getting Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.16 Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.17 When Things Go Wrong . . . . . . . . . . . . . . . . . . . . . . . . . 30
1.18 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.19 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.20 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 36
2 Decision Making . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.1 Finding theMax ofThree Integers . . . . . . . . . . . . . . . . . . . . 45
2.2 The Guess and Check Pattern . . . . . . . . . . . . . . . . . . . . . . . 47
2.3 Choosing from a List of Alternatives . . . . . . . . . . . . . . . . . . . 48
2.4 The Boolean Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5 ShortCircuitLogic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.6 Comparing Floats for Equality . . . . . . . . . . . . . . . . . . . . . . 54
2.7 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.8 ReviewQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
ix
x Contents
2.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.10 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 61
3 Repetitive Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.2 Iterating Over a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.4 The Guess and Check Pattern for Lists . . . . . . . . . . . . . . . . . . 74
3.5 Mutability of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.6 The Accumulator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.7 Reading from and Writing to a File . . . . . . . . . . . . . . . . . . . . 80
3.8 ReadingRecords fromaFile . . . . . . . . . . . . . . . . . . . . . . . 82
3.9 ReviewQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.11 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 88
4 Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.1 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
4.2 Accessor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
4.3 Mutator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
4.4 ImmutableClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
4.5 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . 100
4.6 Working with XML Files . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.7 Extracting Elements from an XML File . . . . . . . . . . . . . . . . . 103
4.8 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4.9 Getting at the Data in an XML File . . . . . . . . . . . . . . . . . . . . 105
4.10 Working with Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.11 Parallel Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.12 Visualizing a Workout . . . . . . . . . . . . . . . . . . . . . . . . . . 108
4.13 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4.15 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 113
5 Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.1 WhyWriteFunctions? . . . . . . . . . . . . . . . . . . . . . . . . . . 120
5.2 PassingArguments andReturning aValue . . . . . . . . . . . . . . . . 121
5.3 ScopeofVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.4 The Run-time Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5.5 MutableData andFunctions . . . . . . . . . . . . . . . . . . . . . . . 129
5.6 Predicate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.7 Top-DownDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
5.8 Bottom-UpDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
5.9 RecursiveFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
5.10 The Main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.11 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Contents xi
5.12 Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.13 Functions with Variable Number of Parameters . . . . . . . . . . . . . 140
5.14 Dictionary Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . 141
5.15 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.16 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.17 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 145
6 Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 149
6.1 TheRootWindow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
6.2 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
6.3 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
6.4 TheTextWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
6.5 TheButtonWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
6.6 Creating a Reminder! . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
6.7 Finishing up the Reminder! Application . . . . . . . . . . . . . . . . . 156
6.8 Label andEntryWidgets . . . . . . . . . . . . . . . . . . . . . . . . . 157
6.9 Layout Management . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
6.10 Message Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.11 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.13 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 164
7 Defining Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
7.1 Creating an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
7.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
7.3 ABouncingBallExample . . . . . . . . . . . . . . . . . . . . . . . . 178
7.4 Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
7.5 Getting Hooked on Python . . . . . . . . . . . . . . . . . . . . . . . . 181
7.6 ReviewQuestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
7.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
7.8 Solutions to Practice Problems . . . . . . . . . . . . . . . . . . . . . . 190
Appendix A Integer Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Appendix B Float Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Appendix C String Operators and Methods . . . . . . . . . . . . . . . . . . . 197
Appendix D List Operators and Methods . . . . . . . . . . . . . . . . . . . . . 201
Appendix E Dictionary Operators and Methods . . . . . . . . . . . . . . . . . 203
Appendix F Turtle Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Appendix G TurtleScreen Methods . . . . . . . . . . . . . . . . . . . . . . . . 217
Appendix H The Reminder! Program . . . . . . . . . . . . . . . . . . . . . . . 225
xii Contents
Appendix I The Bouncing Ball Program . . . . . . . . . . . . . . . . . . . . . 227
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
- Reviews
From the reviews:
“Phython’s recent gain in popularity, this new work is timely. Though intended primarily as a textbook for introductory programming classes, it will work well as a guide to self-teaching. … The writing is clear and succinct; the choice of topics is excellent; and the examples work well. By introducing the Tkinter graphical user interface kit, the book does much to help readers work with interesting programs without the need for another book. … Summing Up: Highly recommended. Students of all levels, professionals/practitioners, and general readers.” (S. L. Tanimoto, Choice, Vol. 48 (9), May, 2011)
“This book follows a classical Kernighan-Ritchie-style approach … . Each chapter concludes with review questions, exercises, and solutions for practice problems--all making this book applicable for self-study. Overall, this is a fine book. … a well-written, well-structured, self-contained modern introduction to programming with an important platform-independent language.” (Klaus Galensa, ACM Computing Reviews, December, 2011)