A Practical Introduction to Python Programming
Brian Heinold
Department of Mathematics and Computer Science Mount St. Marys University
ii
2012 Brian Heinold
Licensed under a Cr eative Commons Attribution-Noncommercial-Share Alike 3.0 Unported Li- cense
Contents
1.1 3
1.2 3
1.3 4
1.4 5
1.5 6
1.6 6
1.7 7
1.8 9
2.1 11
2.2 13
2.3 13
2.4 14
2.5 15
3.1 19
3.2 19
3.3 21
3.4 21
3.5 21
3.6 22
3.7 22
3.8 23
4.1 27
4.2 28
4.3 28
4.4 29
4.5 30
iii
5.1 33
5.2 34
5.3 35
5.4 36
5.5 36
5.6 37
5.7 37
5.8 38
5.9 40
6.1 43
6.2 44
6.3 44
6.4 45
6.5 45
6.6 46
6.7 46
6.8 47
6.9 48
6.10 49
6.11 51
7.1 57
7.2 58
7.3 59
7.4 59
7.5 60
7.6 60
7.7 62
8.1 65
8.2 66
8.3 67
8.4 68
8.5 69
8.6 70
8.7 72
9.1 75
9.2 78
9.3 78
9.4 79
9.5 80
9.6 83
10.1 87
10.2 89
10.3 90
10.4 91
10.5 91
10.6 91
10.7 92
10.8 93
10.9 95
11.1 99
11.2 100
11.3 101
11.4 102
11.5 104
12.1 109
12.2 110
12.3 110
12.4 111
12.5 113
13.1 119
13.2 120
13.3 121
13.4 122
13.5 123
13.6 125
14.1 129
14.2 130
14.3 132
14.4 133
14.5 136
14.6 138
14.7 138
II
15.1 143
15.2 144
15.3 145
15.4 146
15.5 146
15.6 148
15.7 149
16.1 155
16.2 156
16.3 157
16.4 158
16.5 159
16.6 160
16.7 161
16.8 162
16.9 164
17.1 169
17.2 169
17.3 169
17.4 170
17.5 171
17.6 171
17.7 172
17.8 174
17.9 174
17.10 175
17.11 175
17.12 176
18.1 177
18.2 179
18.3 182
III
19.1 185
19.2 187
19.3 187
19.4 189
19.5 190
19.6 190
19.7 190
19.8 191
19.9 192
19.10 193
19.11 194
19.12 195
19.13 196
20.1 199
20.2 200
20.3 202
20.4 204
20.5 204
20.6 205
20.7 205
20.8 206
21.1 207
21.2 208
21.3 212
21.4 214
21.5 214
21.6 216
22.1 219
22.2 220
22.3 221
22.4 221
22.5 222
22.6 224
22.7 226
22.8 226
22.9 228
22.10 229
23.1 231
23.2 232
23.3 233
23.4 234
23.5 235
23.6 235
24.1 237
24.2 238
24.3 239
24.4 240
24.5 241
24.6 242
25.1 245
25.2 246
25.3 247
25.4 247
Preface
My goal here is for something that is partly a tutorial and partly a reference book. I like how tutorials get you up and running quickly, but they can often be a little wordy and disorganized. Reference books contain a lot of good information, but they are often too terse, and they dont often give you a sense of what is important. My aim here is for something in the spirit of a tutorial but still useful as a reference. I summarize information in tables and give a lot of short example programs. I also like to jump right into things and fill in background information as I go, rather than covering the background material first.
This book started out as about 30 pages of notes for students in my introductory programming class at Mount St. Marys University. Most of these students have no prior programming experience, and that has affected my approach. I leave out a lot of technical details and sometimes I oversimplify things. Some of these details are filled in later in the book, though other details are never filled in. But this book is not designed to cover everything, and I recommend reading other books and the Python documentation to fill in the gaps.
The style of programming in this book is geared towards the kinds of programming things I like to doshort programs, often of a mathematical nature, small utilities to make my life easier, and small computer games. In fact, the things I cover in the book are the things that I have found most useful or interesting in my programming experience, and this book serves partly to document those things for myself. This book is not designed as a thorough preparation for a career in software engineering. Interested readers should progress from this book to a book that has more on computer science and the design and organization of large programs.
In terms of structuring a course around this book or learning on your own, the basis is most of Part I. The first four chapters are critically important. Chapteris useful, but not all of it is critical. Chapter a bunch of miscellaneous topics, all of which are useful, but many can be skipped if need be. The final four chapters of Part I are about dictionaries, text files, functions, and object-oriented programming.
Part II is about graphics, mostly GUI programming with Tkinter. You can very quickly write some nice programs using Tkinter. For instance, Sectionpresents a 20-line working (though not
ix
perfect) tic-tac-toe game. The final chapter of Part II covers a bit about the Python Imaging Library.
Part III contains a lot of the fun and interesting things you can do with Python. If you are structur- ing a one-semester course around this book, you might want to pick a few topics in Part III to go over. This part of the book could also serve as a reference or as a place for interested and motivated students to learn more. All of the topics in this part of the book are things that I have found useful at one point or another.
Though this book was designed to be used in an introductory programming course, it is also useful for those with prior programming experience looking to learn Python. If you are one of those people, you should be able to breeze through the first several chapters. You should find Part II to be a concise, but not superficial, treatment on GUI programming. Part III contains information on the features of Python that allow you to accomplish big things with surprisingly little code.
In preparing this book the Python documentation atwww.python.orgwas indispensable. This book was composed entirely in L A T E X. There are a number of L A T E Xpackages, particularly listings and hyperref , that were particulary helpful. L A T E Xcode fromhttp://blog.miliauskas.lt/helped me get the listings package to nicely highlight the Python code.
Listings for the longer programs as well as text files used in the text and exercises are available at http://faculty.msmary.edu/heinold/python.html
Please send comments, corrections, and suggestions to Last updated August 19, 2019.