Practical Programming 2017
Practical Programming 2017
Practical Programming 2017
License xi
Foreword xiii
Preface xv
iii
1.10 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Functions: part 1 27
3.1 Composing expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2 Function calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3 Function definitions and use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.4 Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.5 Parameters and arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.6 Function composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.7 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.9 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4 Functions: part 2 37
4.1 Variables and parameters are local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2 Stack diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.3 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.4 Type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.5 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.6 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5 Conditionals 45
5.1 Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.2 Boolean values and expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.3 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.4 Alternative execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.5 Chained conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
iv
5.6 Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.7 Booleans and type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.8 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.9 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
6 Fruitful functions 55
6.1 The return statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.2 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.3 Program development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.4 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.5 Boolean functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.7 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
8 Strings part 1 73
8.1 A compound data type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.2 Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.3 String slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
8.4 String comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.5 Strings are immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
8.6 Traversal and the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.7 The in operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.8 str Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.9 The split method and lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
8.10 Problem Solving Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.10.1 Print characters in a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.10.2 Build a new string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
8.10.3 Count characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
8.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
v
8.12 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
9 Files 89
9.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
9.2 Processing things from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
9.3 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
9.4 Files as a Unifying Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.5 Common File Reading Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.5.1 Read a whole file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.5.2 Read in all lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
9.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
9.7 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
10 Iteration: part 1 99
10.1 Multiple assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
10.2 Updating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
10.3 Abbreviated assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
10.4 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
10.5 Tracing a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
10.6 Counting digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.7 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
10.8 Common Iteration Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
10.8.1 Basic While Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
10.8.2 External Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
10.8.3 Read one line at a time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
10.8.4 Doctest escape character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
10.9 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
10.10Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
vi
11.9.1 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.9.2 Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
11.9.3 Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
11.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
11.11Laboratory Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
vii
14.9 Strings and lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
14.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
14.11Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
viii
17.6 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
17.7 Laboratory exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
ix
22 Case study 2 Part 1 227
22.1 Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
22.2 The Initial Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
22.3 Opening a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
22.4 Saving to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
22.5 Adding a Command Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
22.6 Laboratory Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Index 249
x
License
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free
Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with
the Invariant Sections being “Foreword”, “Preface”, “Contributor List”, and “Notes on the Otago Edition”,
with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section
entitled ”GNU Free Documentation License”.
xi
xii
Foreword
By David Beazley
As an educator, researcher, and book author, I am delighted to see the completion of this book. Python is a
fun and extremely easy-to-use programming language that has steadily gained in popularity over the last
few years. Developed over ten years ago by Guido van Rossum, Python’s simple syntax and overall feel
is largely derived from ABC, a teaching language that was developed in the 1980’s. However, Python was
also created to solve real problems and it borrows a wide variety of features from programming languages
such as C++, Java, Modula-3, and Scheme. Because of this, one of Python’s most remarkable features is its
broad appeal to professional software developers, scientists, researchers, artists, and educators.
Despite Python’s appeal to many different communities, you may still wonder why Python? or why teach
programming with Python? Answering these questions is no simple task—especially when popular opin-
ion is on the side of more masochistic alternatives such as C++ and Java. However, I think the most direct
answer is that programming in Python is simply a lot of fun and more productive.
When I teach computer science courses, I want to cover important concepts in addition to making the
material interesting and engaging to students. Unfortunately, there is a tendency for introductory pro-
gramming courses to focus far too much attention on mathematical abstraction and for students to become
frustrated with annoying problems related to low-level details of syntax, compilation, and the enforcement
of seemingly arcane rules. Although such abstraction and formalism is important to professional software
engineers and students who plan to continue their study of computer science, taking such an approach
in an introductory course mostly succeeds in making computer science boring. When I teach a course, I
don’t want to have a room of uninspired students. I would much rather see them trying to solve interesting
problems by exploring different ideas, taking unconventional approaches, breaking the rules, and learning
from their mistakes. In doing so, I don’t want to waste half of the semester trying to sort out obscure syn-
tax problems, unintelligible compiler error messages, or the several hundred ways that a program might
generate a general protection fault.
One of the reasons why I like Python is that it provides a really nice balance between the practical and
the conceptual. Since Python is interpreted, beginners can pick up the language and start doing neat
things almost immediately without getting lost in the problems of compilation and linking. Furthermore,
Python comes with a large library of modules that can be used to do all sorts of tasks ranging from web-
programming to graphics. Having such a practical focus is a great way to engage students and it allows
them to complete significant projects. However, Python can also serve as an excellent foundation for intro-
ducing important computer science concepts. Since Python fully supports procedures and classes, students
can be gradually introduced to topics such as procedural abstraction, data structures, and object-oriented
programming—all of which are applicable to later courses on Java or C++. Python even borrows a number
of features from functional programming languages and can be used to introduce concepts that would be
covered in more detail in courses on Scheme and Lisp.
In reading Jeffrey’s preface, I am struck by his comments that Python allowed him to see a higher level of
success and a lower level of frustration and that he was able to move faster with better results. Although
these comments refer to his introductory course, I sometimes use Python for these exact same reasons in
xiii
advanced graduate level computer science courses at the University of Chicago. In these courses, I am
constantly faced with the daunting task of covering a lot of difficult course material in a blistering nine
week quarter. Although it is certainly possible for me to inflict a lot of pain and suffering by using a
language like C++, I have often found this approach to be counterproductive—especially when the course
is about a topic unrelated to just programming. I find that using Python allows me to better focus on the
actual topic at hand while allowing students to complete substantial class projects.
Although Python is still a young and evolving language, I believe that it has a bright future in education.
This book is an important step in that direction.
David Beazley University of Chicago Author of the Python Essential Reference
xiv
Preface
By Jeffrey Elkner
This book owes its existence to the collaboration made possible by the Internet and the free software move-
ment. Its three authors—a college professor, a high school teacher, and a professional programmer—have
yet to meet face to face, but we have been able to work closely together and have been aided by many
wonderful folks who have donated their time and energy to helping make this book better.
We think this book is a testament to the benefits and future possibilities of this kind of collaboration, the
framework for which has been put in place by Richard Stallman and the Free Software Foundation.
In 1999, the College Board’s Advanced Placement (AP) Computer Science exam was given in C++ for the
first time. As in many high schools throughout the country, the decision to change languages had a direct
impact on the computer science curriculum at Yorktown High School in Arlington, Virginia, where I teach.
Up to this point, Pascal was the language of instruction in both our first-year and AP courses. In keeping
with past practice of giving students two years of exposure to the same language, we made the decision
to switch to C++ in the first-year course for the 1997-98 school year so that we would be in step with the
College Board’s change for the AP course the following year.
Two years later, I was convinced that C++ was a poor choice to use for introducing students to computer
science. While it is certainly a very powerful programming language, it is also an extremely difficult lan-
guage to learn and teach. I found myself constantly fighting with C++’s difficult syntax and multiple ways
of doing things, and I was losing many students unnecessarily as a result. Convinced there had to be a
better language choice for our first-year class, I went looking for an alternative to C++.
I needed a language that would run on the machines in our GNU/Linux lab as well as on the Windows and
Macintosh platforms most students have at home. I wanted it to be free software, so that students could
use it at home regardless of their income. I wanted a language that was used by professional programmers,
and one that had an active developer community around it. It had to support both procedural and object-
oriented programming. And most importantly, it had to be easy to learn and teach. When I investigated
the choices with these goals in mind, Python stood out as the best candidate for the job.
I asked one of Yorktown’s talented students, Matt Ahrens, to give Python a try. In two months he not only
learned the language but wrote an application called pyTicket that enabled our staff to report technology
problems via the Web. I knew that Matt could not have finished an application of that scale in so short
a time in C++, and this accomplishment, combined with Matt’s positive assessment of Python, suggested
that Python was the solution I was looking for.
xv
Finding a textbook
Having decided to use Python in both of my introductory computer science classes the following year, the
most pressing problem was the lack of an available textbook.
Free documents came to the rescue. Earlier in the year, Richard Stallman had introduced me to Allen
Downey. Both of us had written to Richard expressing an interest in developing free educational materials.
Allen had already written a first-year computer science textbook, How to Think Like a Computer Scientist.
When I read this book, I knew immediately that I wanted to use it in my class. It was the clearest and most
helpful computer science text I had seen. It emphasized the processes of thought involved in programming
rather than the features of a particular language. Reading it immediately made me a better teacher.
How to Think Like a Computer Scientist was not just an excellent book, but it had been released under a GNU
public license, which meant it could be used freely and modified to meet the needs of its user. Once I
decided to use Python, it occurred to me that I could translate Allen’s original Java version of the book
into the new language. While I would not have been able to write a textbook on my own, having Allen’s
book to work from made it possible for me to do so, at the same time demonstrating that the cooperative
development model used so well in software could also work for educational materials.
Working on this book for the last two years has been rewarding for both my students and me, and my
students played a big part in the process. Since I could make instant changes whenever someone found
a spelling error or difficult passage, I encouraged them to look for mistakes in the book by giving them a
bonus point each time they made a suggestion that resulted in a change in the text. This had the double
benefit of encouraging them to read the text more carefully and of getting the text thoroughly reviewed by
its most important critics, students using it to learn computer science.
For the second half of the book on object-oriented programming, I knew that someone with more real
programming experience than I had would be needed to do it right. The book sat in an unfinished state
for the better part of a year until the open source community once again provided the needed means for its
completion.
I received an email from Chris Meyers expressing interest in the book. Chris is a professional program-
mer who started teaching a programming course last year using Python at Lane Community College in
Eugene, Oregon. The prospect of teaching the course had led Chris to the book, and he started helping out
with it immediately. By the end of the school year he had created a companion project on our Website at
http://openbookproject.net called Python for Fun and was working with some of my most advanced
students as a master teacher, guiding them beyond where I could take them.
#include <iostream.h>
void main()
{
cout << "Hello, world." << endl;
}
xvi
in the Python version it becomes:
Even though this is a trivial example, the advantages of Python stand out. Yorktown’s Computer Science I
course has no prerequisites, so many of the students seeing this example are looking at their first program.
Some of them are undoubtedly a little nervous, having heard that computer programming is difficult to
learn. The C++ version has always forced me to choose between two unsatisfying options: either to explain
the #include, void main(), {, and } statements and risk confusing or intimidating some of the students
right at the start, or to tell them, “Just don’t worry about all of that stuff now; we will talk about it later,”
and risk the same thing. The educational objectives at this point in the course are to introduce students to
the idea of a programming statement and to get them to write their first program, thereby introducing them
to the programming environment. The Python program has exactly what is needed to do these things, and
nothing more.
Comparing the explanatory text of the program in each version of the book further illustrates what this
means to the beginning student. There are thirteen paragraphs of explanation of “Hello, world!” in the
C++ version; in the Python version, there are only two. More importantly, the missing eleven paragraphs
do not deal with the “big ideas” in computer programming but with the minutia of C++ syntax. I found this
same thing happening throughout the book. Whole paragraphs simply disappear from the Python version
of the text because Python’s much clearer syntax renders them unnecessary.
Using a very high-level language like Python allows a teacher to postpone talking about low-level details
of the machine until students have the background that they need to better make sense of the details. It
thus creates the ability to put “first things first” pedagogically. One of the best examples of this is the way
in which Python handles variables. In C++ a variable is a name for a place that holds a thing. Variables
have to be declared with types at least in part because the size of the place to which they refer needs to be
predetermined. Thus, the idea of a variable is bound up with the hardware of the machine. The powerful
and fundamental concept of a variable is already difficult enough for beginning students (in both computer
science and algebra). Bytes and addresses do not help the matter. In Python a variable is a name that refers
to a thing. This is a far more intuitive concept for beginning students and is much closer to the meaning of
“variable” that they learned in their math courses. I had much less difficulty teaching variables this year
than I did in the past, and I spent less time helping students with problems using them.
Another example of how Python aids in the teaching and learning of programming is in its syntax for
functions. My students have always had a great deal of difficulty understanding functions. The main
problem centers around the difference between a function definition and a function call, and the related
distinction between a parameter and an argument. Python comes to the rescue with syntax that is nothing
short of beautiful. Function definitions begin with the keyword def, so I simply tell my students, “When
you define a function, begin with def, followed by the name of the function that you are defining; when you
call a function, simply call (type) out its name.” Parameters go with definitions; arguments go with calls.
There are no return types, parameter types, or reference and value parameters to get in the way, so I am
now able to teach functions in less than half the time that it previously took me, with better comprehension.
Using Python has improved the effectiveness of our computer science program for all students. I see a
higher general level of success and a lower level of frustration than I experienced during the two years
I taught C++. I move faster with better results. More students leave the course with the ability to cre-
ate meaningful programs and with the positive attitude toward the experience of programming that this
engenders.
xvii
Building a community
I have received email from all over the globe from people using this book to learn or to teach programming.
A user community has begun to emerge, and many people have been contributing to the project by sending
in materials for the companion Website at http://openbookproject.net/pybiblio
With the continued growth of Python, I expect the growth in the user community to continue and acceler-
ate. The emergence of this user community and the possibility it suggests for similar collaboration among
educators have been the most exciting parts of working on this project for me. By working together, we
can increase the quality of materials available for our use and save valuable time. I invite you to join our
community and look forward to hearing from you. Please write to me at [email protected].
Jeffrey Elkner
Arlington Public Schools
Arlington, Virginia
xviii
Contributor List
To paraphrase the philosophy of the Free Software Foundation, this book is free like free speech, but not
necessarily free like free pizza. It came about because of a collaboration that would not have been possible
without the GNU Free Documentation License. So we would like to thank the Free Software Foundation
for developing this license and, of course, making it available to us.
We would also like to thank the more than 100 sharp-eyed and thoughtful readers who have sent us sug-
gestions and corrections over the past few years. In the spirit of free software, we decided to express our
gratitude in the form of a contributor list. Unfortunately, this list is not complete, but we are doing our best
to keep it up to date. It was also getting too large to include everyone who sends in a typo or two. You
have our gratitude, and you have the personal satisfaction of making a book you found useful better for
you and everyone else who uses it. New additions to the list for the 2nd edition will be those who have
made on-going contributions.
If you have a chance to look through the list, you should realize that each person here has spared you and
all subsequent readers from the confusion of a technical error or a less-than-transparent explanation, just
by sending us a note.
Impossible as it may seem after so many corrections, there may still be errors in this book. If you should
stumble across one, we hope you will take a minute to contact us. The email address is [email protected].
Substantial changes made due to your suggestions will add you to the next version of the contributor list
(unless you ask to be omitted). Thank you!
Otago Editions
• Sandy Garner for many many suggestions and improvements including many new exercises.
• Special thanks to all the lab demonstrators in COMP150 that have made suggestions for improving
the book.
• Raymond Scurr
• Michael Albert
• Matthew Jenkins
• Xiangfei Jia
• Thomas Harper
• Paul McCarthy
xix
Second Edition
• A special thanks to the students in Jeff’s Computer Science class at the HB-Woodlawn program dur-
ing the 2007-2008 school year: James Crowley, Joshua Eddy, Eric Larson, Brian McGrail, and Iliana
Vazuka. Your willingness to serve as beta-testers of the new chapters as they were completed, and to
endure frequent changes in response to your feedback have proved invaluable. Thanks to you this is
truly a student tested text.
• Aldric Giacomoni pointed out an error in our definition of the Fibonacci sequence in Chapter 5.
• Roger Sperberg sent in several spelling corrections and pointed out a twisted piece of logic in Chapter
3.
• Adele Goldberg sat down with Jeff at PyCon 2007 and gave him a list of suggestions and corrections
from throughout the book.
• Carl LaCombe pointed out that we incorrectly used the term “commutative” in chapter 6 where “sym-
metric” was the correct term.
• Alessandro Montanile sent in corrections for errors in the code examples and text in chapters 3, 12,
15, 17, 18, 19, and 20.
• Michael Vogt reported an indentation error in an example in chapter 6, and sent in a suggestion for
improving the clarity of the shell vs. script section in chapter 1.
First Edition
• Lloyd Hugh Allen sent in a correction to Section 8.4.
• Jonah Cohen wrote the Perl scripts to convert the LaTeX source for this book into beautiful HTML.
• Michael Conlon sent in a grammar correction in Chapter 2 and an improvement in style in Chapter 1,
and he initiated discussion on the technical aspects of interpreters.
• Courtney Gleason and Katherine Smith wrote horsebet.py, which was used as a case study in an
earlier version of the book. Their program can now be found on the website.
• Lee Harr submitted more corrections than we have room to list here, and indeed he should be listed
as one of the principal editors of the text.
• James Kaylin is a student using the text. He has submitted numerous corrections.
xx
• Eddie Lam has sent in numerous corrections to Chapters 1, 2, and 3. He also fixed the Makefile so
that it creates an index the first time it is run and helped us set up a versioning scheme.
• David Mayo pointed out that the word “unconsciously” in Chapter 1 needed to be changed to “sub-
consciously”.
• Matthew J. Moelter has been a long-time contributor who sent in numerous corrections and sugges-
tions to the book.
• Simon Dicon Montford reported a missing function definition and several typos in Chapter 3. He also
found errors in the increment function in Chapter 13.
• Kevin Parks sent in valuable comments and suggestions as to how to improve the distribution of the
book.
• David Pool sent in a typo in the glossary of Chapter 1, as well as kind words of encouragement.
• Robin Shaw pointed out an error in Section 13.1, where the printTime function was used in an example
without being defined.
• Paul Sleigh found an error in Chapter 7 and a bug in Jonah Cohen’s Perl script that generates HTML
from LaTeX.
• Craig T. Snydal is testing the text in a course at Drew University. He has contributed several valuable
suggestions and corrections.
• Ian Thomas and his students are using the text in a programming course. They are the first ones to test
the chapters in the latter half of the book, and they have make numerous corrections and suggestions.
• Peter Winstanley let us know about a longstanding error in our Latin in Chapter 3.
• Chris Wrobel made corrections to the code in the chapter on file I/O and exceptions.
• Moshe Zadka has made invaluable contributions to this project. In addition to writing the first draft
of the chapter on Dictionaries, he provided continual guidance in the early stages of the book.
• Christoph Zwerschke sent several corrections and pedagogic suggestions, and explained the differ-
ence between gleich and selbe.
• James Mayer sent us a whole slew of spelling and typographical errors, including two in the contrib-
utor list.
• Angel Arnal is part of an international team of translators working on the Spanish version of the text.
He has also found several errors in the English version.
• Tauhidul Hoque and Lex Berezhny created the illustrations in Chapter 1 and improved many of the
other illustrations.
xxi
• Dr. Michele Alzetta caught an error in Chapter 8 and sent some interesting pedagogic comments and
suggestions about Fibonacci and Old Maid.
• Andy Mitchell caught a typo in Chapter 1 and a broken example in Chapter 2.
• Kalin Harvey suggested a clarification in Chapter 7 and caught some typos.
• Christopher P. Smith caught several typos and is helping us prepare to update the book for Python
2.2.
• David Hutchins caught a typo in the Foreword.
• Gregor Lingl is teaching Python at a high school in Vienna, Austria. He is working on a German
translation of the book, and he caught a couple of bad errors in Chapter 5.
• Julie Peters caught a typo in the Preface.
xxii
Course Information
People
• Brendan McCane (Course Coordinator): [email protected]
Course Structure
The ability to program relies heavily on mastering certain skills and techniques. These skills are cumula-
tive in the sense that mastering skills further on in the material requires that you’ve fully mastered prior
skills. Therefore, the structure of COMP150 is based on a mastery model. In this model, students progress
at their own pace and move on to the next topic only when they’ve mastered the previous topic. As a
consequence, COMP150 does not follow the single paced Lecture/Laboratory model of other papers. This
book provides almost all the course content and it is supported by podcasts of the various lessons. Students
must familiarise themselves with the content prior to attending their labs. Since COMP150 is self-paced,
different students will get to different parts of the book by the end of the semester. This is perfectly fine. As
a minimum, we expect all students to complete up to and including Lesson 15, but you should be aiming
to get as far along as you can.
Assessment
There are 8 mastery progressions and two practical tests. The progressions are worth 60% in total and the
practical tests are worth 20% each. The progressions are pass/fail. You cannot sit the next progression until
you successfully complete the previous one. You can resit the progressions as many times as you like, but
only once per day. If you pass a progression, you get all the marks available for that progression. If you fail
a progression, you don’t get any marks (but you can resit until you pass).
Each of the progressions are summarised in the table below:
Progression Marks(%) Num Questions Topic
1 4 2 Assignment and Operations (Lessons 1 and 2)
2 8 2 Functions and User Input (Lessons 3 and 4)
3 8 2 Conditionals and Fruitful Functions (Lessons 5 and 6)
4 8 2 Modules and Strings 1 (Lessons 7 and 8)
5 8 2 Files (Lesson 9)
6 8 2 Iteration: while loop (Lesson 10)
7 8 1 Iteration: nested loops (Lesson 11)
8 8 1 Problems using a combination of techniques (all Lessons)
xxiii
The practical tests will be run in the labs under exam conditions. They will include questions very similar
to the progressions. The main difference is you will only get one chance to sit these tests. Both practical tests
will include 4 questions each worth 5%. Practical test 1 will include material up to and including Chapter 8
and will likely run in week 7. Practical test 2 will include material from Chapter 9 onwards and will run in
week 13.
There is no final exam for this paper.
Wednesday at 2pm: topics of relevance to labs, progressions, practical tests and programming in gen-
eral.
• Laboratories
You will be streamed into two scheduled laboratories each week. Each lab is two hours long. The
streaming lists will be posted on the noticeboards near the Computer Science Main Office in the
Owheo Building (133 Union Street East) and on evision.
Lab attendance is compulsory. Failure to attend labs means you will fail COMP150 (regardless
of actual marks). Labs start in the second half of week 1. Allowing for Good Friday and Anzac
Day, everyone can attend at least 23 labs. To pass COMP150 you must attend at least 20 labs. Lab
attendance will be recorded based on time logged into a lab machine. You must be logged into a lab
machine for at least 75 minutes during your streamed lab to be recorded as having attended that lab.
You are welcome to come to the lab at any other time, but priority will be given to those with sched-
uled labs.
• Eating is not allowed in the lab. Drinking from sipper-top bottles is ok.
• Installing your own software on our machines is forbidden.
• Copying software installed on lab machines is forbidden.
• Students streamed to a particular lab session will be given priority for seating and demonstrator time.
• Files not related to your Computer Science course(s) should not be stored in your Computer Science
Home directory. You should be aware that Computer Science Systems Administrators can inspect the
contents of your home directory at any time.
• Students must behave in a sensible, responsible and adult manner whilst in the lab.
• Please ensure that your behaviour, music or conversation is not adversely affecting others trying to
use the lab.
http://www.cs.otago.ac.nz/resreg/index.html
xxiv
Blackboard
COMP150 will be using Blackboard. You can log-on to Blackboard at:
https://blackboard.otago.ac.nz/
xxv
xxvi
Lesson 1
The goal of this paper is twofold: to teach you how to program in Python; and to teach you to think like
a computer scientist. It is almost impossible to become a competent programmer without also learning
how to think like a computer scientist. This way of thinking combines some of the best features of math-
ematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages
to denote ideas (specifically computations). Like engineers, they design things, assembling components
into systems and evaluating trade-offs among alternatives. Like scientists, they observe the behaviour of
complex systems, form hypotheses, and test predictions.
If you aren’t interested in becoming a computer scientist, but just want to gain some programming skills,
then this is the perfect paper for you. In this paper we are going to focus on practical programming skills
suitable for solving smallish programming problems. Problems that you will often encounter if you use a
computer regularly.
The single most important skill for a computer scientist is problem solving. Problem solving is the ability
to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it
turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills.
That’s why this chapter is called, “The way of the program.”
On one level, you will be learning to program, a useful skill by itself. On another level, you will use
programming as a means to an end. As we go along, that end will become clearer.
The programming language you will be learning is Python. Python is an example of a high-level language;
other high-level languages you might have heard of are C++, PHP, and Java.
As you might infer from the name high-level language, there are also low-level languages, sometimes
referred to as machine languages or assembly languages. Loosely speaking, computers can only execute
programs written in low-level languages. Thus, programs written in a high-level language have to be
processed before they can run. This extra processing takes some time, which is a small disadvantage of
high-level languages.
But the advantages are enormous. First, it is much easier to program in a high-level language. Programs
written in a high-level language take less time to write, they are shorter and easier to read, and they are more
likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds
of computers with few or no modifications. Low-level programs can run on only one kind of computer and
have to be rewritten to run on another.
1
Due to these advantages, almost all programs are written in high-level languages. Low-level languages are
used only for a few specialized applications.
Two kinds of applications process high-level languages into low-level languages: interpreters and compil-
ers. An interpreter reads a high-level program and executes it, meaning that it does what the program says.
It processes the program a little at a time, alternately reading lines and performing computations.
A compiler reads the program and translates it into a low-level program, which can then be run.
In this case, the high-level program is called the source code, and the translated program is called the
object code or the executable. Once a program is compiled, you can execute it repeatedly without further
translation.
Many modern languages use both processes. They are first compiled into a lower level language, called
byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but
because of the way programmers interact with it, it is usually considered an interpreted language.
There are two ways to use the Python interpreter: shell mode and script mode. In shell mode, you type Python
statements into the Python shell and the interpreter immediately prints the result.
In this course we will be using an IDE (Integrated Development Environment) called IDLE. When you first
start IDLE it will open an interpreter window.1
The first few lines identify the version of Python being used as well as a few other messages; you can safely
ignore the lines about the firewall. Next there is a line identifying the version of IDLE. The last line starts
with >>>, which is the Python prompt. The interpreter uses the prompt to indicate that it is ready for
instructions.
If we type print 1 + 1 the interpreter will reply 2 and give us another prompt.2
>>> print 1 + 1
2
>>>
Alternatively, you can write a program in a file and use the interpreter to execute the contents of the file.
Such a file is called a script. For example, we used the text editor in IDLE (but we could have used any text
1 You can also run the python interpreter by just entering the command python in a terminal. To exit from the interpreter type
statement with syntax print(1+1). In Python 2.x, print(1+1) will still work, but it is actually printing a tuple and is not a simple
print statement. Tuples are discussed in Lecture 15.
2
editor) to create a file named firstprogram.py with the following contents:
print 1 + 1
By convention, files that contain Python programs have names that end with .py. To execute the program
we have to invoke the interpreter and tell it the name of the script:
$ python firstprogram.py
2
This example shows Python being run from a terminal (with $ representing the Unix prompt). In other de-
velopment environments, the details of executing programs may differ. IDLE simplifies the whole process
by presenting interpreter windows and a text editor within the same application. You can run a script in
IDLE by either choosing Run → Run Module or pressing F5. Most programs are more interesting than this
one. The examples in this book use both the Python interpreter and scripts. You will be able to tell which is
intended since shell mode examples (i.e. entering lines directly into the interpreter) will always start with
the Python prompt, >>>. Working in shell mode is convenient for testing short bits of code because you get
immediate feedback. Think of it as scratch paper used to help you work out problems. Anything longer
than a few lines should be put into a script so it can be saved for future use.
input: Get data from the keyboard, a file, or some other device.
output: Display data on the screen or send data to a file or other device.
math: Perform basic mathematical operations like addition and multiplication.
conditional execution: Check for certain conditions and execute the appropriate sequence of statements.
repetition: Perform some action repeatedly, usually with some variation.
Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no matter how com-
plicated, is made up of instructions that look more or less like these. Thus, we can describe programming
as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are
simple enough to be performed with one of these basic instructions.
That may be a little vague, but we will come back to this topic later when we talk about algorithms.
3
Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors. It is useful
to distinguish between them in order to track them down more quickly.
Syntax errors Python can only execute a program if the program is syntactically correct; otherwise, the
process fails and returns an error message. Syntax refers to the structure of a program and the rules
about that structure. For example, in English, a sentence must begin with a capital letter and end with
a period. this sentence contains a syntax error. So does this one
For most readers, a few syntax errors are not a significant problem, which is why we can read the
poetry of e. e. cummings without spewing error messages. Python is not so forgiving. If there is a
single syntax error anywhere in your program, Python will print an error message and quit, and you
will not be able to run your program. During the first few weeks of your programming career, you
will probably spend a lot of time tracking down syntax errors. As you gain experience, though, you
will make fewer syntax errors and find them faster.
Runtime errors The second type of error is a runtime error, so called because the error does not appear
until you run the program. These errors are also called exceptions because they usually indicate that
something exceptional (and bad) has happened. Runtime errors are rare in the simple programs you
will see in the first few chapters, so it might be a while before you encounter one.
Semantic errors The third type of error is the semantic error. If there is a semantic error in your program,
it will run successfully, in the sense that the computer will not generate any error messages, but it will
not do the right thing. It will do something else. Specifically, it will do what you told it to do.
The problem is that the program you wrote is not the program you wanted to write. The meaning of
the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires you
to work backward by looking at the output of the program and trying to figure out what it is doing.
4
1.5 Formal and natural languages
Natural languages are the languages that people speak, such as English, Spanish, and French. They were
not designed by people (although people try to impose some order on them); they evolved naturally.
Formal languages are languages that are designed by people for specific applications. For example, the
notation that mathematicians use is a formal language that is particularly good at denoting relationships
among numbers and symbols. Chemists use a formal language to represent the chemical structure of
molecules. And most importantly:
Programming languages are formal languages that have been designed to express computations.
Formal languages tend to have strict rules about syntax. For example, 3+3=6 is a syntactically correct
mathematical statement, but 3=+6$ is not. H2 O is a syntactically correct chemical name, but 2 Zz is not.
Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the basic elements of the
language, such as words, numbers, and chemical elements. One of the problems with 3=+6$ is that $ is
not a legal token in mathematics (at least as far as we know). Similarly, 2 Zz is not legal because there is no
element with the abbreviation Zz. The second type of syntax rule pertains to the structure of a statement—
that is, the way the tokens are arranged. The statement 3=+6$ is structurally illegal because you can’t place
a plus sign immediately after an equal sign. Similarly, molecular formulas have to have subscripts after the
element name, not before.
When you read a sentence in English or a statement in a formal language, you have to figure out what the
structure of the sentence is (although in a natural language you do this subconsciously). This process is
called parsing. For example, when you hear the sentence, “The brown dog barked”, you understand that
the brown dog is the subject and barked is the verb. Once you have parsed a sentence, you can figure out
what it means, or the semantics of the sentence. Assuming that you know what a dog is and what it means
to bark, you will understand the general implication of this sentence.
Although formal and natural languages have many features in common—tokens, structure, syntax, and
semantics—there are many differences:
ambiguity: Natural languages are full of ambiguity, which people deal with by using contextual clues and
other information. Formal languages are designed to be nearly or completely unambiguous, which
means that any statement has exactly one meaning, regardless of context.
redundancy: In order to make up for ambiguity and reduce misunderstandings, natural languages employ
lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more
concise.
literalness: Natural languages are full of idiom and metaphor. If someone says, “The penny dropped”,
there is probably no penny and nothing dropped. Formal languages mean exactly what they say.
People who grow up speaking a natural language—everyone—often have a hard time adjusting to formal
languages. In some ways, the difference between formal and natural language is like the difference between
poetry and prose, but more so:
Poetry: Words are used for their sounds as well as for their meaning, and the whole poem together creates
an effect or emotional response. Ambiguity is not only common but often deliberate.
Prose: The literal meaning of words is more important, and the structure contributes more meaning. Prose
is more amenable to analysis than poetry but still often ambiguous.
Programs: The meaning of a computer program is unambiguous and literal, and can be understood entirely
by analysis of the tokens and structure.
5
Here are some suggestions for reading programs (and other formal languages). First, remember that formal
languages are much more dense than natural languages, so it takes longer to read them. Also, the structure
is very important, so it is usually not a good idea to read from top to bottom, left to right. Instead, learn to
parse the program in your head, identifying the tokens and interpreting the structure. Finally, the details
matter. Little things like spelling errors and bad punctuation, which you can get away with in natural
languages, can make a big difference in a formal language.
This is an example of a print statement, which doesn’t actually print anything on paper. It displays a value
on the screen. In this case, the result is the words
Hello, World!
The quotation marks in the program mark the beginning and end of the value; they don’t appear in the
result.
Some people judge the quality of a programming language by the simplicity of the Hello, World! program.
By this standard, Python does about as well as is possible.
1.7 Glossary
algorithm: A general process for solving a category of problems.
byte code: An intermediate language between source code and object code. Many modern languages first
compile source code into byte code and then interpret the byte code with a program called a virtual
machine.
compile: To translate a program written in a high-level language into a low-level language all at once, in
preparation for later execution.
debugging: The process of finding and removing any of the three kinds of programming errors.
formal language: Any one of the languages that people have designed for specific purposes, such as repre-
senting mathematical ideas or computer programs; all programming languages are formal languages.
high-level language: A programming language like Python that is designed to be easy for humans to read
and write.
6
low-level language: A programming language that is designed to be easy for a computer to execute; also
called machine language or assembly language.
natural language: Any one of the languages that people speak that evolved naturally.
object code: The output of the compiler after it translates the program.
parse: To examine a program and analyze the syntactic structure.
portability: A property of a program that can run on more than one kind of computer.
print statement: An instruction that causes the Python interpreter to display a value on the screen.
problem solving: The process of analysing a problem, finding a solution, and expressing the solution.
program: a sequence of instructions that specifies to a computer actions and computations to be performed.
Python shell: An interactive user interface to the Python interpreter. The user of a Python shell types
commands at the prompt (>>>), and presses the return key to send these commands immediately to
the interpreter for processing.
runtime error: An error that does not occur until the program has started to execute but that prevents the
program from continuing.
script: A program stored in a file (usually one that will be interpreted).
semantic error: An error in a program that makes it do something other than what the programmer in-
tended.
semantics: The meaning of a program.
source code: A program in a high-level language before being compiled.
syntax: The structure of a program.
syntax error: An error in a program that makes it impossible to parse (and therefore impossible to inter-
pret).
token: One of the basic elements of the syntactic structure of a program, analogous to a word in a natural
language.
7
1.8.1 Logging-on
If this is the first time you have used a Computer Science laboratory your username will usually3 be your first
initial followed by your last name. For example Billie Blogs has the username bblogs, and Edwin Heisenberg
has the username eheisenberg. Your password will be your id number as shown on your student id card.
Your first action should be to change your password. To do this click on your username at top-right of the
computer screen. Choose “Users and Groups Preferences”, then click on the “Change Password ...” button,
and fill out the required entry boxes. Your new password must follow the following convention:
When you leave the lab remember to log-out. To do this click on the Apple menu (top-left of the Desktop)
and choose “Log-out <your username>”
In the rest of this book, the action of selecting menu items will be denoted Apple → Log Out.
1.8.3 Menus
An application’s menus are always displayed in the Menu bar at the top of the Desktop. The default set of
menus, as shown, are the Finder’s menus. In addition to the familiar sounding menus, there is an Apple
menu and a Finder menu. The Apple menu is always there and always has the same contents; explore the
contents of the Apple menu, especially the About This Mac, System Preferences and the Dock options.
The next menu is the Finder menu. Finder is actually an application, it is the one which manages your
Desktop. The options on the Finder menu and in fact all the rest of the menus are currently specific to the
Finder application. When a different application is active the menus in the Menu bar will be specific to that
application.
• The left part of the Dock contains shortcuts (aliases in Mac-talk) to applications.
• You can remove application aliases from the Dock by simply dragging them to the Desktop and re-
leasing them.
3 if the name is not unique, a slightly different one will be created — ask Nick to check.
8
Figure 1.1: The Mac OS X Desktop
• To add an application to the Dock drag its icon from the Application folder to the Dock, see below.
• An application that is running has a small blue/white dot beneath it.
• Any application that is running will appear in the Dock, even if it doesn’t usually have an alias there.
• The right-hand side of the Dock contains the Trash and any windows or documents you have col-
lapsed (minimised) and aliases to folders.
• If the item is an application that is not already running it will start the application.
• If the item is an application that is already running it will become the active application. Note that an
application window may not be displayed in which case you will need to select File → New or similar.
• If the application was ‘hidden’ it will be unhidden.
• If the application had been collapsed it will be restored.
9
Figure 1.2: A Finder Window
The buttons along the top (from the left) allow you to: navigate back and forth (like in a web browser),
modify the view options, burn folders and files to CD and make new folders. The left column lists place,
drives and locations you can navigate to and explore. The most useful locations for you will be your home
drive and the Applications folder. Your home drive probably won’t show up in the sidebar by default. You
should enable it by Finder → Preferences → Sidebar → username.
1.8.7 Applications
On a Mac, most of the GUI (Graphical User Interface) applications are installed in the Applications folder.
• Open a Finder window and click on the Applications folder, it is also in the Favorites group. A list of
all the applications installed on your machine is displayed.
• Scroll down the list until you find the Firefox application icon, drag the icon to the Dock to create an
alias to the Firefox application.
• Scroll down further until you find the Utilities folder. If you are viewing the directory in list mode,
click the little triangle to expand the list, if in icon mode, double click on the Utilities folder. Scroll
4 The server is actually a RAID array (Redundant Array of Inexpensive Disks) rather than a single server.
10
Figure 1.3: IDLE starts with an interpreter window open.
• Drag Terminal to the Dock to create an alias for it as well. Terminal starts a shell session.
Remember, you can remove unwanted items from the Dock by simply dragging them to the Desktop and
releasing them.
1.8.8 IDLE
In COMP150 you will be using a development environment (also called an IDE - Integrated Development
Environment) called IDLE. IDLE is a very simple IDE and is ideal for our purposes. Start IDLE by clicking
on the IDLE shortcut icon in the Dock that you created moments ago. You should see a window that looks
like Figure 1.3 on page 11. By default IDLE starts with an interpreter window open. Click on the window
to give it focus. Notice the menu bar changes and now contains Python menus; this is because IDLE is a
Python application.
To open an editor window for writing scripts either choose File → New Window or use the Command-N
shortcut. Open an editor window now.
11
Figure 1.4: IDLE with both interpreter and editor windows.
or Home drive by simply clicking and dragging the appropriate folder. There is a folder for this lab. Try
dragging the Lab1 folder from coursework/COMP150/coursefiles150 to your Desktop now.
1.9 Blackboard
Many papers at Otago make use of Blackboard, an online course management tool that provides staff and
students with communication and file sharing tools as well as a host of other features. COMP150 will
use Blackboard for making class announcements and for discussion boards. You should check Blackboard
every time you come to the lab. To log-on to Blackboard:
• Locate the MyPapers group of links and click on the COMP150 link. You will be taken to the An-
nouncements page. This is where all important course-related messages will be posted.
12
• Click on the Discussion Board link. We have created two Discussion Boards, one for questions
about Python or any other part of the course, the other for any suggestions or requests you have for
COMP150.
• There is at least one posting on each board, read them and reply as appropriate.
Get a demonstrator to review your work before you leave the lab.
1. “this is a syntactically incorrect sentence” has two syntax errors. What are they?
2. Write an English sentence with understandable semantics but incorrect syntax. If English is not your
first language, feel free to use the language of your choice for this question and the next.
3. “Three of his legs were both the same.” is an example of a sentence with correct syntax but some
semantic errors, that is it is not clear what it means. Think up another sentence that is syntactically ok
but has semantic (meaning) errors.
4. If you haven’t already done so, start IDLE. Type 2 + 3 at the prompt and then hit return. The symbols +
and - stand for plus and minus as they do in mathematics. The asterisk (*) is the symbol for multiplication,
and ** is the symbol for exponentiation (so 5**2 means 5 to the power of 2).
Python evaluates any expression you type in, prints the result, and then prints another prompt. Experiment
by entering different expressions and recording what is printed by the Python interpreter.
Expression Answer
1.
2.
3.
4.
5.
6.
7.
What happens if you use / (the division operator)? Are the results what you expect? Explain. Make
sure you try divisions that would result in a non-integer result such as 5/2. 5
Expression Answer
1.
2.
3.
4.
5. 10/7
6. 10/4
7. 10/5
5 This is another thing that has changed with Python 3.x. The / operator returns a float (a number with a fractional part) in Python
13
Show your work to a demonstrator here to make sure you are on the right track.
5. Type print "hello" at the prompt. Python executes this statement, which has the effect of printing
the letters h-e-l-l-o. Notice that the quotation marks that you used to enclose the string are not part of
the output.
Now type print ’"hello"’ and describe and explain your result.
6. Start an editor in IDLE (File → New Window, or Command-N) and type print "hello" into the editor
window, which is currently called Untitled. Save your script (File → Save, or Command-S) by first changing to
your home directory in the Save Dialog Box, then creating a New Folder called Lab1, and finally changing
the name untitled to hello.py before clicking Save. Once you have done this run it by either pushing F5
or clicking Run → Run Module. Write down and explain what happens in the prompt window below:
7. Type print cheese without the quotation marks into the interpreter. The output will look some-
thing like this:
This is a run-time error; specifically, it is a NameError, and even more specifically, it is an error because
the name cheese is not defined. If you don’t know what that means yet, you will soon.
8. Still in the interpreter, type 1 2 (with a space in between) and then hit return. Python tries to evaluate
the expression, but it can’t because the expression is not syntactically legal. Instead, it prints the error
message:
In many cases, Python indicates where the syntax error occurred, but it is not always right, and it
doesn’t give you much information about what is wrong. So, for the most part, the burden is on you
to learn the syntax rules. In this case, Python is complaining because there is no operator between the
numbers.
Write down three more examples of statements that will produce error messages when you enter
them at the Python prompt. Explain why each example is not valid Python syntax.
9. Whenever an expression is typed at the Python prompt, it is evaluated and the result is printed on the line
below. "This is a test..." is an expression, which evaluates to "This is a test..." (just like
the expression 42 evaluates to 42). In a script, however, evaluations of expressions are not sent to the
program output, so it is necessary to explicitly print it.
Type "This is a test..." at the prompt and hit enter. Record what happens.
10. Now open a new file (File → New Window, or Command-N) and type "This is a test..." into the
Untitled document.
Save your file in the Lab1 folder (you can quickly change to it using the drop down list) as test.py.
What happens when you run this script (Run → Run Module, or F5)?
14
print "This is a test..."
Get a demonstrator to review your work before you leave the lab.
15
16
Lesson 2
>>> print 4
4
If you are not sure what type a value has, the interpreter can tell you.
Strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal
point belong to a type called float, because these numbers are represented in a format called floating-point.
>>> type(3.2)
<type ’float’>
What about values like “17” and “3.2”? They look like numbers, but they are in quotation marks like strings.
>>> type("17")
<type ’str’>
>>> type("3.2")
<type ’str’>
17
They’re strings. Strings in Python can be enclosed in either single quotes (’) or double quotes (”):
Double quoted strings can contain single quotes inside them, as in "What’s your name?", and single
quoted strings can have double quotes inside them, as in ’The cat said "Meow!"’.
When you type a large integer, you might be tempted to use commas between groups of three digits, as in
1,000,000. Resist the temptation as the commas mean something completely different in Python... which
we will look at later in the course.
2.2 Variables
One of the most powerful features of a programming language is the ability to manipulate variables. A
variable is a name that refers to a value. The assignment statement creates new variables and assigns them
values:
This example makes three assignments. The first assigns the string "What’s your name?" to a new
variable named message. The second assigns the integer 17 to n, and the third assigns the floating-point
number 3.14159 to pi. The assignment operator, =, should not be confused with an equals sign (even
though it uses the same character). Assignment operators link a name, on the left hand side of the operator,
with a value, on the right hand side. This is why you will get an error if you enter:
>>> 17 = n
A common way to represent variables on paper is to write the name with an arrow pointing to the variable’s
value. This kind of figure is called a state diagram because it shows what state each of the variables is in
(think of it as the variable’s state of mind). This diagram shows the result of the assignment statements:
18
The print statement also works with variables.
In each case the result is the value of the variable. Variables also have types; again, we can ask the interpreter
what they are.
>>> type(message)
<type ’str’>
>>> type(n)
<type ’int’>
>>> type(pi)
<type ’float’>
The type of a variable is the type (or kind) of value it refers to.
76trombones is illegal because it does not begin with a letter. more$ is illegal because it contains an
illegal character, the dollar sign. But what’s wrong with class? It turns out that class is one of the Python
keywords. Keywords define the language’s rules and structure, and they cannot be used as variable names.
Python has thirty-one keywords:
You might want to keep this list handy. If the interpreter complains about one of your variable names and
you don’t know why, see if it is on this list.
19
2.4 Statements
A statement is an instruction that the Python interpreter can execute. We have seen two kinds of statements:
print and assignment. When you type a statement on the command line, Python executes it and displays
the result, if there is one. The result of a print statement is a value. Assignment statements don’t produce
a visible result. A script usually contains a sequence of statements. If there is more than one statement, the
results appear one at a time as the statements execute. For example, the script
print 1
x = 2
print x
The evaluation of an expression produces a value, which is why expressions can appear on the right hand side
of assignment statements. A value all by itself is a simple expression, and so is a variable.
>>> 17
17
>>> x
2
Confusingly, evaluating an expression is not quite the same thing as printing a value.
>>> message = "What’s your name?"
>>> message
"What’s your name?"
>>> print message
What’s your name?
When the Python shell displays the value of an expression, it uses the same format you would use to enter a
value. In the case of strings, that means that it includes the quotation marks. But the print statement prints
the value of the expression, which in this case is the contents of the string. In a script, an expression all by
itself is a legal statement, but it doesn’t do anything. The script
17
3.2
"Hello, World!"
1 + 1
20
produces no output at all. How would you change the script to display the values of these four expressions?
20 + 32
hour - 1
hour * 60 + minute
minute / 60
5 ** 2
(5 + 9) * (15 - 7)
The symbols +, -, *, /, have the usual mathematical meanings. The symbol, **, is the exponentiation
operator. The statement, 5 ** 2, means 5 to the power of 2, or 5 squared in this case. Python also uses
parentheses for grouping, so we can force operations to be done in a certain order just like we can in
mathematics.
When a variable name appears in the place of an operand, it is replaced with its value before the operation
is performed. Addition, subtraction, multiplication, and exponentiation all do what you expect, but you
might be surprised by division. The following operation has an unexpected result:
>>> minute = 59
>>> minute / 60
0
The value of minute is 59, and 59 divided by 60 is 0.98333, not 0. The reason for the discrepancy is that
Python is performing integer division1 . When both of the operands are integers, the result must also be
an integer, and by convention, integer division always rounds down, even in cases like this where the next
integer is very close. A possible solution to this problem is to calculate a percentage rather than a fraction:
Again the result is rounded down, but at least now the answer is approximately correct. Another alternative
is to use floating-point division. We’ll see in Section 4.4 how to convert integer values and variables to
floating-point values.
21
>>> quotient = 7 / 3
>>> print quotient
2
>>> remainder = 7 % 3
>>> print remainder
1
So 7 divided by 3 is 2 with 1 left over. The modulus operator turns out to be surprisingly useful. For
example, you can check whether one number is divisible by another—if x % y is zero, then x is divisible
by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the
right-most digit of x (in base 10). Similarly x % 100 yields the last two digits.
1. Parentheses have the highest precedence and can be used to force an expression to evaluate in the or-
der you want. Since expressions in parentheses are evaluated first, 2*(3-1) is 4, and (1+1)**(5-2)
is 8. You can also use parentheses to make an expression easier to read, as in (minute*100)/60,
even though it doesn’t change the result.
2. Exponentiation has the next highest precedence, so 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not
27.
3. Multiplication and Division have the same precedence, which is higher than Addition and Subtraction,
which also have the same precedence. So 2*3-1 yields 5 rather than 4, and 2/3-1 is -1, not 1 (re-
member that in integer division, 2/3=0).
Operators with the same precedence are evaluated from left to right. So in the expression minute*100/60,
the multiplication happens first, yielding 5900/60, which in turn yields 98. If the operations had been
evaluated from right to left, the result would have been 59*1, which is 59, which is wrong. Similarly, in
evaluating 17-4-3, 17-4 is evaluated first.
If in doubt, use parentheses.
message - 1
"Hello" / 123
message * "Hello"
"15" + 2
Interestingly, the + operator does work with strings, although it does not do exactly what you might expect.
For strings, the + operator represents concatenation, which means joining the two operands by linking them
end-to-end. For example:
22
fruit = "banana"
baked_good = " nut bread"
print fruit + baked_good
The output of this program is banana nut bread. The space before the word nut is part of the string,
and is necessary to produce the space between the concatenated strings. The * operator also works on
strings; it performs repetition. For example, "Fun"*3 is "FunFunFun". One of the operands has to be a
string; the other has to be an integer. On one hand, this interpretation of + and * makes sense by analogy
with addition and multiplication. Just as 4*3 is equivalent to 4+4+4, we expect "Fun"*3 to be the same as
"Fun"+"Fun"+"Fun", and it is. On the other hand, there is a significant way in which string concatenation
and repetition are different from integer addition and multiplication. Can you think of a property that
addition and multiplication have that string concatenation and repetition do not?
2.10 Glossary
value: A number or string (or other thing to be named later) that can be stored in a variable or computed
in an expression.
type: A set of values. The type of a value determines how it can be used in expressions. So far, the types
you have seen are integers (type int), floating-point numbers (type float), and strings (type string).
type function: A function which returns the type of the given argument, for example:
int: A Python data type that holds positive and negative whole numbers.
float: A Python data type which stores floating-point numbers. Floating-point numbers are stored internally
in two parts: a base and an exponent. When printed in the standard format, they look like decimal num-
bers. Beware of rounding errors when you use floats, and remember that they are only approximate
values.
assignment statement: A statement that assigns a value to a name (variable). To the left of the assignment
operator, =, is a name. To the right of the assignment operator is an expression which is evaluated
by the Python interpreter and then assigned to the name. The difference between the left and right
hand sides of the assignment statement is often confusing to new programmers. In the following
assignment:
n = n + 1
n plays a very different role on each side of the =. On the right it is a value and makes up part of the
expression which will be evaluated by the Python interpreter before assigning it to the name on the
left.
23
assignment operator: = is Python’s assignment operator, which should not be confused with the mathe-
matical comparison operator using the same symbol.
state diagram: A graphical representation of a set of variables and the values to which they refer.
variable name: A name given to a variable. Variable names in Python consist of a sequence of letters (a..z,
A..Z, and ) and digits (0..9) that begins with a letter. In best programming practice, variable names
should be chosen so that they describe their use in the program, making the program self documenting.
keyword: A reserved word that is used by the compiler to parse a program; you cannot use keywords like
if, def, and while as variable names.
statement: An instruction that the Python interpreter can execute. Examples of statements include the
assignment statement and the print statement.
expression: A combination of variables, operators, and values that represents a single result value.
evaluate: To simplify an expression by performing the operations in order to yield a single value.
operator: A special symbol that represents a simple computation like addition, multiplication, or string
concatenation.
operand: One of the values on which an operator operates.
modulus operator: An operator, denoted with a percent sign (%), that works on integers and floats and
yields the remainder when one number is divided by another.
integer division: An operation that divides one integer by another and yields an integer. Integer divi-
sion yields only the whole number of times that the numerator is divisible by the denominator and
discards any remainder.
rules of precedence: The set of rules governing the order in which expressions involving multiple opera-
tors and operands are evaluated.
24
2.11 Laboratory exercises
1. Create a Folder in your COMP150 Directory called Lab2 to store the files you create during this lab.
2. Enter the following statements into the interpreter and note which ones produce an error:
>>> n = 7
>>> 7 = n
>>> n = 5 + 2
>>> n = 5 + n
>>> n + 5 = n
>>> n = y
3. Enter the following print statements into the interpreter and note which ones produce an error:
>>> print n
>>> print n + 5
>>> print n + 5 + 2
>>> print n = 5
>>> print 7 + 5
>>> print n, 7+5, n+7
4. Look at the following lines of code and predict what will be printed out. Use a python script to check
your prediction.
n = 2
print n + 2
print "n is ", n
n = 2
n = n + 2
print "n is ", n
5. Take the sentence: Jack is a dull boy. Show how you would use the interpreter to store each word in a separate
variable, then print out the sentence on one line using print.
6. Repeat exercise 5, this time using a script (a script is another name for a saved Python file).
Show your work to a demonstrator here to make sure you are on the right track.
7. Add parentheses to the expression 6*1-2 to change its value from 4 to -6.
25
8. Try to evaluate the following numerical expressions in your head, and put the answer beside each one, then
use the Python interpreter to check your results:
>>> 5 % 2
>>> 9 % 5
>>> 15 % 12
>>> 12 % 15
>>> 6 % 6
>>> 0 % 7
>>> 7 % 0
What happened with the last example? Why? If you were able to correctly anticipate the computer’s
response in all but the last one, it is time to move on. If not, take time now to make up examples of your
own. Explore the modulus operator until you are confident you understand how it works.
9. Start the Python interpreter and enter bruce + 4 at the prompt. This will give you an error:
Get a demonstrator to review your work before you leave the lab.
26
Lesson 3
Functions: part 1
>>> print 17 + 3
20
In reality, the addition has to happen before the printing, so the actions aren’t actually happening at the
same time. The point is that any expression involving numbers, strings, and variables can follow print:
You can also put arbitrary expressions on the right-hand side of an assignment statement:
This ability may not seem impressive now, but you will see other examples where composition makes it
possible to express complex computations neatly and concisely.
Warning: There are limits on where you can use certain expressions. For example, the left-hand side of an
assignment statement has to be a variable name, not an expression. So, the following is illegal:
27
under fifty predefined functions (of which we’ll be only using about a dozen) and the simplest way to use
this prewritten code is via function calls.12
The syntax of a function call is simply
FUNCTION NAME(ARGUMENTS)
Not all functions take an argument, and some take more than one (in which case the arguments are sepa-
rated by commas).
You have already seen an example of a function call:
The name of the function is type, and it displays the type of a value or variable. The value or variable,
which is called the argument of the function, has to be enclosed in parentheses. It is common to say that a
function “takes” an argument and “returns” a result. The result is called the return value.
We can assign the return value to a variable:
Another useful function is len. It takes a Python sequence as an argument. The only Python sequence we
have met so far is a string. A string is a sequence of characters. For a string argument, len returns the
number of characters the string contains.
The len function can only be used on sequences. Trying to use it on a number, for example, results in an
error.
>>> len(3)
TypeError: object of type ’int’ has no len()
1 Not everything that appears to be a function call actually is, but you don’t need to worry about that in this course. For the sake of
simplicity we’ll say that everything that looks like a function is a function.
2 There is a full list of built-in functions at http://docs.python.org/2/library/functions.html
28
The ’list of parameters’ is where the arguments supplied to the function end up. You will see more of this
later.
You can make up any names you want for the functions you create, except that you can’t use a name that is
a Python keyword. The list of parameters specifies what information, if any, you have to provide in order
to use the new function.
There can be any number of statements inside the function, but they have to be indented from the def. In
the examples in this book, we will use the standard indentation of four spaces3 . IDLE automatically indents
compound statements for you. Function definitions are the first of several compound statements we will
see, all of which have the same pattern:
In a function definition, the keyword in the header is def, which is followed by the name of the function
and a list of parameters enclosed in parentheses. The parameter list may be empty, or it may contain any
number of parameters. In either case, the parentheses are required. The first couple of functions we are
going to write have no parameters, so the syntax looks like this:
def new_line():
print
This function is named new line. The empty parentheses indicate that it has no parameters (that is it takes
no arguments). Its body contains only a single statement, which outputs a newline character. (That’s what
happens when you use a print command without any arguments.)
Defining a new function does not make the function run. To do that we need a function call. Function calls
contain the name of the function to be executed followed by a list of values, called arguments, which are
assigned to the parameters in the function definition. Our first examples have an empty parameter list, so
the function calls do not take any arguments. Notice, however, that the parentheses are required in the function
call :
First line.
Second line.
The extra space between the two lines is a result of the new line() function call. What if we wanted more
space between the lines? We could call the same function repeatedly:
a tab, but each file must use the same indentation scheme throughout. A common error is to mix 4 spaces and tabs, as these look the
same in an editor.
29
new_line()
print "Second Line."
Or we could write a new function named three lines that prints three new lines:
def three_lines():
new_line()
new_line()
new_line()
This function contains three statements, all of which are indented by four spaces. Since the next statement
is not indented, Python knows that it is not part of the function. You should notice a few things about this
program:
• You can call the same procedure repeatedly. In fact, it is quite common and useful to do so.
• You can have one function call another function; in this case three lines calls new line.
So far, it may not be clear why it is worth the trouble to create all of these new functions. Actually, there are
a lot of reasons, but this example demonstrates two:
1. Creating a new function gives you an opportunity to name a group of statements. Functions can
simplify a program by hiding a complex computation behind a single command and by using English
words in place of arcane code.
2. Creating a new function can make a program smaller by eliminating repetitive code. For example, a
short way to print nine consecutive new lines is to call three lines three times.
Pulling together the code fragments from the previous section into a script named functions.py, the
whole program looks like this:
def new_line():
print
def three_lines():
new_line()
new_line()
new_line()
This program contains two function definitions: new line and three lines. Function definitions get
executed just like other statements, but the effect is to create the new function. The statements inside the
function do not get executed until the function is called, and the function definition generates no output.
As you might expect, you have to create a function before you can execute it. In other words, the function
definition has to be executed before the first time it is called.
30
3.4 Flow of execution
In order to ensure that a function is defined before its first use, you have to know the order in which state-
ments are executed, which is called the flow of execution. Execution always begins at the first statement
of the program. Statements are executed one at a time, in order from top to bottom. Function definitions
do not alter the flow of execution of the program, but remember that statements inside the function are
not executed until the function is called. Although it is not common, you can define one function inside
another. In this case, the inner definition isn’t executed until the outer function is called.
Function calls are like a detour in the flow of execution. Instead of going to the next statement, the flow
jumps to the first line of the called function, executes all the statements there, and then comes back to pick
up where it left off. That sounds simple enough, until you remember that one function can call another.
While in the middle of one function, the program might have to execute the statements in another function.
But while executing that new function, the program might have to execute yet another function! Fortu-
nately, Python is adept at keeping track of where it is, so each time a function completes, the program picks
up where it left off in the function that called it. When it gets to the end of the program, it terminates.
It is usually not helpful to read a program from top to bottom. In python programs the top part of a
file is almost always used for function definitions and it is not necessary to read those until you want to
understand what a particular function does. The bottom part of a python file is often called the main
program. This part can be recognised because it is often not indented. It is easier to understand a program
by following the flow of execution starting at the beginning of the main program.
>>> abs(5)
5
>>> abs(-5)
5
In this example, the arguments to the abs function are 5 and -5.
Some functions take more than one argument. For example the built-in function pow takes two arguments,
the base and the exponent. Inside the function, the values that are passed get assigned to variables called
parameters.
>>> pow(2, 3)
8
>>> pow(3, 2)
9
31
>>> round(1.23456789)
1.0
>>> round(1.5)
2.0
>>> round(1.23456789, 2)
1.23
>>> round(1.23456789, 3)
1.235
Another built-in function that takes more than one argument is max.
The function max can be sent any number of arguments, separated by commas, and will return the max-
imum value sent. The arguments can be either simple values or expressions. In the last example, 503 is
returned, since it is larger than 33, 125, and 1.
Here is an example of a user-defined function that has a parameter:
def print_twice(some_variable_name):
print some_variable_name, some_variable_name
This function takes a single argument and assigns it to the parameter named some variable name. The
value of the parameter (at this point we have no idea what it will be) is printed twice, followed by a newline.
The name some variable name was chosen to suggest that the name you give a parameter is up to you,
but in general, you want to choose something more descriptive than some variable name.
In a function call, the value of the argument is assigned to the corresponding parameter in the function def-
inition. In effect, it is as if some variable name = "Spam" is executed when print twice("Spam") is
called; some variable name = 5 is executed when print twice(5) is called; and some variable name
= 3.14159 is executed when print twice(3.14159) is called. Any type of argument that can be
printed can be sent to print twice. In the first function call, the argument is a string. In the second,
it’s an integer. In the third, it’s a float.
As with built-in functions, we can use an expression as an argument for print twice:
>>> print_twice("Spam"*4)
SpamSpamSpamSpam SpamSpamSpamSpam
"Spam"*4 is first evaluated to ’SpamSpamSpamSpam’, which is then passed as an argument to print twice.
>>> print_twice(abs(-7))
7 7
32
>>> print_twice(max(3, 1, abs(-11), 7))
11 11
In the first example, abs(-7) evaluates to 7, which then becomes the argument to print twice. In the
second example we have two levels of composition, since abs(-11) is first evaluated to 11 before max(3,
1, 11, 7) is evaluated to 11 and print twice(11) then displays the result.
We can also use a variable as an argument:
Notice something very important here. The name of the variable we pass as an argument (saying) has
nothing to do with the name of the parameter (some variable name). It doesn’t matter what the argu-
ment is called; here in print twice, we call everybody some variable name.
3.7 Comments
As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense,
and it is often difficult to look at a piece of code and figure out what it is doing, or why. For this reason,
it is a good idea to add notes to your programs to explain in natural language what the program is doing.
These notes are called comments, and they are marked with the # symbol:
In this case, the comment appears on a line by itself. You can also put comments at the end of a line:
Everything from the # to the end of the line is ignored—it has no effect on the program. The message is
intended for the programmer or for future programmers who might use this code. In this case, it reminds
the reader about the ever-surprising behaviour of integer division.
3.8 Glossary
function: A named sequence of statements that performs some useful operation. Functions may or may
not take parameters and may or may not produce a result.
function definition: A statement that creates a new function, specifying its name, parameters, and the
statements it executes.
Python sequence: Several data structures in Python are collectively known as “sequences”. They are struc-
tures for storing a collection of values in a specific order. For example, a string is a sequence of char-
acters.
compound statement: A statement that consists of two parts:
1. header - which begins with a keyword determining the statement type, and ends with a colon.
2. body - containing one or more statements indented the same amount from the header.
33
The syntax of a compound statement looks like this:
keyword expression :
statement
statement ...
header: The first part of a compound statement. Headers begin with a keyword and end with a colon (:).
body: The second part of a compound statement. The body consists of a sequence of statements all in-
dented the same amount from the beginning of the header. The standard amount of indentation used
within the Python community is 4 spaces.
flow of execution: The order in which statements are executed during a program run.
parameter: A name used inside a function to refer to the value passed as an argument.
argument: A value provided to a function when the function is called. This value is assigned to the corre-
sponding parameter in the function.
abs function: Returns the absolute value of the argument.
pow function: Returns the first argument raised to the power of the second one. pow(x,y) is equivalent
to x**y.
round function: Rounds a number and returns the floating point value rounded to n-digits digits after the
decimal point.
max function: Returns the largest argument (if given more than one), or the largest item in a sequence (if
given a single sequence).
>>> max(3, 8, 2, 1)
8
>>> max("hello")
'o'
composition: The ability to combine simple expressions and statements into compound statements and
expressions in order to represent complex computations concisely.
function composition: Using the output from one function call as the input to another.
comment: Information in a program that is meant for other programmers (or anyone reading the source
code) and has no effect on the execution of the program.
main program: The part at the bottom of a file that is usually not indented. It is easier to understand a
program by following the flow of executing starting from the main program.
34
3.9 Laboratory exercises
1. Using IDLE, create a Python script named functions.py. Type the definition of the function
new line from the lecture at the top of the file, and then add the definition of the function three lines
below it. Add a main routine at the bottom of the script (and not indented) which is a call to
three lines(). Don’t forget to add the parentheses when you call the function. Run the program.
2. Add a function to the file called nine lines that uses three lines to print nine blank lines.
Change the call in the main routine to be a call to nine lines() rather than three lines().
3. Now add a function named clear screen that prints out exactly forty blank lines. Change the main
routine call again to call clear screen to check that it works as expected.
4. What would happen if nine lines was above three lines in the script? Why?
5. Start a new script more functions.py and add a function called powers(n) that prints out the
first 5 powers of a given number.
>>> powers(6)
The first 5 powers of 6 are: 1 6 36 216 1296
>>> powers(10)
The first 5 powers of 10 are: 1 10 100 1000 10000
You can test your script by running it and then calling your functions from the Python interpreter.
Show your work to a demonstrator here to make sure you are on the right track.
6. Add another function to more functions.py called score summary which takes 3 arguments,
representing scores from 3 judges. The function should determine and display the bottom, top, and
average score. Call this function from the interpreter, sending it different data e.g.
7. The results (in the question above) are listed without any way of identifying who they refer to. Make
the output more meaningful by adding a name string to the parameter list (and supplying a name
argument when you call the function).
35
8. Write reject and accept functions which can produce acceptance or rejection form letters. The signature
should be in a separate function, used by both the reject and accept functions.
>>> reject("Bill")
Dear Bill
I am sorry to inform you that you do not have the job
Yours sincerely
Humphrey Hopalong
>>> reject("Amanda")
Dear Amanda
I am sorry to inform you that you do not have the job
Yours sincerely
Humphrey Hopalong
>>> accept("Vicki")
Dear Vicki
I am pleased to inform you that you have the job
Yours sincerely
Humphrey Hopalong
9. Recall that min() and max() take a ’sequence’ as an argument and that strings are ’sequences’ of
characters. Write a function show first which takes a string argument and prints the minimum
value in the string. You can test your script from the interpreter or by calling the function in from
the script itself (in the main section of the script). Try it with ”dog”, xylophone”, ”elephant”,
”Elephant”
10. Try to complete up to level seven on the game you played in the previous labs.
Get a demonstrator to review your work before you leave the lab.
36
Lesson 4
Functions: part 2
Remember the print twice function from the previous lecture? We are going to use it again, but we will
change the parameter name to something a bit more sensible.
def print_twice(phrase):
print phrase, phrase
This function takes two arguments, concatenates them and stores the result in a local variable joined. It
then calls print twice with joined as the argument. print twice prints the value of the argument,
twice. We can call the function with two strings:
When print joined twice terminates, the variable joined is destroyed. If we try to print it, we get an
error:
When you create a local variable inside a function, it only exists inside that function, and you cannot use it
outside. Parameters are also local. For example, outside the function print twice, there is no such thing
as phrase. If you try to use it, Python will complain. Similarly, part1 and part2 do not exist outside
print joined twice
37
4.2 Stack diagrams
To keep track of which variables can be used where, it is sometimes useful to draw a stack diagram. Like
state diagrams, stack diagrams show the value of each variable, but they also show the function to which
each variable belongs. Each function is represented by a frame. A frame is a box with the name of a
function beside it and the parameters and variables of the function inside it. The stack diagram for the
previous example looks like this:
The order of the stack shows the flow of execution. print twice was called by print joined twice,
and print joined twice was called by main , which is a special name for the topmost function. When
you create a variable outside of any function, it belongs to main . Each parameter refers to the same
value as its corresponding argument. So, part1 has the same value as line1, part2 has the same value
as line2, and phrase has the same value as joined.
If an error occurs during a function call, Python prints the name of the function, and the name of the
function that called it, and the name of the function that called that, all the way back to the top most
function. To see how this works, we create a Python script named stacktrace.py that looks like this:
1 def print_twice(phrase):
2 print phrase, phrase
3 print joined
4
5 def print_joined_twice(part1, part2):
6 joined = part1 + part2
7 print_twice(joined)
8
9 line1 = "Happy birthday, "
10 line2 = "to you."
11 print_joined_twice(line1, line2)
We’ve added the statement, print joined inside the print twice function, but joined is not defined
there. Running this script will produce an error message like this:
38
This list of functions is called a traceback. It tells you what program file the error occurred in, and what
line, and what functions were executing at the time. It also shows the line of code that caused the error.
Notice the similarity between the traceback and the stack diagram. It’s not a coincidence. In fact, another
common name for a traceback is a stack trace.
4.3 Input
Remember at the start of the course we said that programs are made up of instructions which only perform
a handful of operations1 . The first two of these operations were input and output. Input involves getting
data from the keyboard, a file, or some other device. Output is concerned with displaying data on the
screen or sending data to a file or other device.
We have already done quite a bit of displaying data on the screen using the print command, which fits
into the category of output. We are now going to look at one of the most basic input tasks: getting keyboard
input.
There are two built-in functions in Python for getting keyboard input, input and raw input2 :
$ python tryinput.py
Please enter your name: Arthur, King of the Britons
Arthur, King of the Britons
Enter a numerical expression: 7 * 3
21
Each of these functions allows a prompt (a message to the user) to be given between the parentheses as an
argument to the function. The result returned by raw input is always a string. But the result returned
from input can be any valid Python type.
>>> n = raw_input()
hello
>>> n
’hello’
>>> type(n)
<type ’str’>
>>> n = input()
1.5 + 2
>>> n
3.5
>>> type(n)
<type ’float’>
39
4.4 Type conversion
Each Python type comes with a built-in function that attempts to convert values of another type into that
type. The int(ARGUMENT) function, for example, takes any value and converts it to an integer, if possible,
or complains otherwise:
>>> int("32")
32
>>> int("Hello")
ValueError: invalid literal for int() with base 10: ’Hello’
The int function can also convert floating-point values to integers, but note that it truncates the fractional
part:
>>> int(-2.3)
-2
>>> int(3.99999)
3
>>> int("42")
42
>>> int(1.0)
1
>>> float(32)
32.0
>>> float("3.14159")
3.14159
>>> float(1)
1.0
It may seem odd that Python distinguishes the integer value 1 from the floating-point value 1.0. They may
represent the same number, but they belong to different types as they are represented differently inside the
computer.
The str(ARGUMENT) function converts any argument to type string:
>>> str(32)
’32’
>>> str(3.14149)
’3.14149’
>>> str(True)
’True’
>>> str(true)
NameError: name ’true’ is not defined
The str(ARGUMENT) function will work with any value and convert it into a string. Note: True is a
predefined value in Python; true is not. We will see more of True in lecture 5.
4.5 Glossary
local variable: A variable defined inside a function. A local variable can only be used inside its function.
40
stack diagram: A graphical representation of a stack of functions, their variables, and the values to which
they refer.
frame: A box in a stack diagram that represents a function call. It contains the local variables and parame-
ters of the function.
traceback: A list of the functions that are executing, printed when a runtime error occurs. A traceback is
also commonly referred to as a stack trace, since it lists the functions in the order in which they are
stored in the runtime stack.
def print_param(x):
print x
x = "Hello"
print_param("Goodbye")
print x
Draw a stack diagram showing the value of each function’s variables at the moment that the print state-
ment in print param is called.
def change_param(x):
x = "Goodbye"
x = "Hello"
change_param(x)
print x
Run the script and record the output of the program in a text file.
41
3. Try to figure out what the output of the program below will be.
one = "fish"
two = "and"
three = "chips"
print_all(one, two, three)
print_reverse(one, two, three)
print_all(three, two, one)
print_add(one, two, three)
print_add_reverse(one, two, three)
print_all(1, 2, 3)
print_reverse(1, 2, 3)
print_add(1, 2, 3)
print_add_reverse(1, 2, 3)
Put your answer in a text file, and then type the code into a Python script and run it to check that you are
right.
Draw the stack diagram at the moment print add reverse(one, two, three) is called.
Run the script to see what it does. Simplify the script by identifying the repeated code and placing it in a
function. This is called ’encapsulation’.
42
5. Create a script called convert.py which uses the function raw input to read a string from the keyboard.
Attempt to convert the string to a float using float(x) and also to an integer using int(x). Print out the
resulting float and integer. Test the script with the following input:
10
103
1e12
0.000001
blah
Show your work to a demonstrator here to make sure you are on the right track.
6. Try to complete up to level nine on the game you played in the previous labs.
7. So far we have used the ’,’ to print more than one item at a time. In the following exercises this
will prove problematic, in particular adding the full stop in the correct place when using a ’,’ always
introduces a space. Try using ’+’ to concatenate the various strings instead. Note: concatenation only
works with strings so you will need to cast any non-string values.
>>> a = "one"
>>> b = "two"
>>> c = 3
>>> print a, b, c
one two 3
>>> print a + b + str(c)
onetwo3
8. Create a script called film critic.py. In it define a function rate movie that takes an argument movie
and asks the user to give it a rating out of five. The function should produce output similar to:
9. Define a function that takes two arguments, name and age. It should produce output similar to:
Get a demonstrator to review your work before you leave the lab.
43
44
Lesson 5
Conditionals
In order to write useful programs, we almost always need the ability to check conditions and change the
behaviour of the program accordingly. Conditional statements give us this ability. We use this idea of con-
ditional execution very often in our everyday lives. For example, when leaving the house in the morning,
you might habitually “execute” the following:
Or, if you live in Dunedin, you probably just take your coat and umbrella every day.
In Python, the simplest form of a conditional statement is the if statement :
if weather == "windy":
print "take your coat"
The part after the if keyword and before the colon is a boolean expression and is called the condition.
A boolean expression is simply an expression that evaluates to True or False. If the condition is true,
then the indented statement gets executed. If the condition is false then the indented statement is skipped
and the flow of control jumps to the statement immediately following the if block. The syntax for an if
statement looks like this:
if BOOLEAN EXPRESSION:
STATEMENTS
As with the function definition from Lecture 3 and other compound statements, the if statement consists
of a header and a body. The header begins with the keyword if followed by a boolean expression and ends
with a colon (:). The indented statements that follow are called a block. The first unindented statement
marks the end of the block. A statement block inside a compound statement is called the body of the
statement. Each of the statements inside the body are executed in order if the boolean expression evaluates
to True. The entire block is skipped if the boolean expression evaluates to False. There is no limit on the
number of statements that can appear in the body of an if statement, but there has to be at least one.
45
5.2 Boolean values and expressions
The Python type for storing true and false values is called bool, named after the British mathematician,
George Boole. George Boole created Boolean algebra , which is the basis of all modern computer arithmetic.
There are only two boolean values : True and False. Capitalization is important, since true and false
are not boolean values in Python.
>>> type(True)
<type ’bool’>
>>> type(true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ’true’ is not defined
>>> 5 == 5
True
>>> 5 == 6
False
In the first statement, the two operands are equal, so the expression evaluates to True; in the second state-
ment, 5 is not equal to 6, so we get False. The == operator is one of the comparison operators; the others
are:
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
Although these operations are probably familiar to you, the Python symbols are different from the math-
ematical symbols. A common error is to use a single equal sign (=) instead of a double equal sign (==).
Remember that = is an assignment operator and == is a comparison operator. Also, there is no such thing
as =< or =>.
if x==5 or 6:
print 'x is 5 or 6'
In fact this boolean expression is always True. What is probably intended is:
46
x y z Boolean Expression Value
"windy" "rainy" - x=="windy"
"windy" "rainy" - y=="rainy"
"windy" "rainy" - (x=="windy") and (y=="rainy")
"windy" "sunny" - (x=="windy") and (y=="rainy")
"windy" "sunny" - (x=="windy") or (y=="rainy")
True - - x
True True - x or y
True False - x and y
True False True x and (y or z)
10 5 7 (x<y) or (y<z)
10 5 7 (x<y) and (y<z)
10 5 7 (x<y) and (y<x)
10 5 7 (x<y) or (y<z)
10 10 7 (x<y) or (y<z)
10 10 7 ((x<y) or (y<z)) and ((x<20) and (y>5))
if x==5 or x==6:
print 'x is 5 or 6'
Although the former if statement sounds natural to a human, it is almost always the wrong thing to do.
Make sure you are explicit with all the subparts of your Boolean expressions.
if x % 2 == 0:
print x, "is even"
else:
print x, "is odd"
If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a
message to that effect. If the condition is false, the second set of statements is executed. Since the condition
must be true or false, exactly one of the alternatives will be executed. The alternatives are called branches ,
because they are branches in the flow of execution.
As an aside, if you need to check the parity (evenness or oddness) of numbers often, you might wrap this
code in a function:
def print_parity(x):
if x % 2 == 0:
print x, "is even"
else:
print x, "is odd"
47
For any value of x, print parity displays an appropriate message. When you call it, you can provide
any integer expression as an argument.
>>> print_parity(17)
17 is odd.
>>> y = 41
>>> print_parity(y+1)
42 is even.
if x < y:
print x, "is less than", y
elif x > y:
print x, "is greater than", y
else:
print x, "and", y, "are equal"
elif is an abbreviation of else if. Again, exactly one branch will be executed. There is no limit to the
number of elif statements but only a single (and optional) else statement is allowed and it must be the
last branch in the statement:
if choice == 'a':
function_a()
elif choice == 'b':
function_b()
elif choice == 'c':
function_c()
else:
print "Invalid choice."
Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true,
the corresponding branch executes, and the statement ends. Even if more than one condition is true, only
the first true branch executes.
if x == y:
print x, "and", y, "are equal"
else:
if x < y:
print x, "is less than", y
else:
print x, "is greater than", y
48
The outer conditional contains two branches. The first branch contains a simple output statement. The
second branch contains another if statement, which has two branches of its own. Those two branches
are both output statements, although they could have been conditional statements as well. Although the
indentation of the statements makes the structure apparent, nested conditionals become difficult to read
very quickly. A nested conditional can always be rewritten as a chained conditional and vice versa. What
to use is partly a matter of choice, but in general you should use the structure that is easiest to understand.
Logical operators often provide a way to simplify nested conditional statements. For example, we can
rewrite the following code using a single conditional:
if 0 < x:
if x < 10:
print "x is a positive single digit."
The print statement is executed only if we make it past both the conditionals, so we can use the and opera-
tor:
These kinds of conditions are common, so Python provides an alternative syntax that is similar to mathe-
matical notation:
This condition is semantically the same as the compound boolean expression and the nested conditional.
>>> str(True)
’True’
>>> str(true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ’true’ is not defined
>>> bool(1)
True
>>> bool(0)
False
>>> bool("Banana")
True
>>> bool("")
False
>>> bool(3.14159)
True
>>> bool(0.0)
False
>>> bool("False") # "False" is a non-empty string, cf. bool("Banana")
49
True
badWeather = "False"
if badWeather:
print "Take your coat"
will always print "Take your coat" (perfect for Dunedin perhaps).
5.8 Glossary
boolean value: There are exactly two boolean values: True and False. Boolean values result when a boolean
expression is evaluated by the Python interpreter. They have type bool.
boolean expression: An expression that is either true or false.
bool function: Converts the argument to a boolean.
comparison operator: One of the operators that compares two values: ==, !=, >, <, >=, and <=.
logical operator: One of the operators that combines boolean expressions: and, or, and not.
conditional statement: A statement that controls the flow of execution depending on some condition. In
Python the keywords if, elif, and else are used for conditional statements.
condition: The boolean expression in a conditional statement that determines which branch is executed.
chained conditional: A conditional branch with more than two possible flows of execution. In Python
chained conditionals are written with if ... elif ... else statements.
nesting: One program structure within another, such as a conditional statement inside a branch of another
conditional statement.
50
5.9 Laboratory exercises
1. Look at the following expressions. What do you think the results would be? Enter the expressions in
to the Python interpreter to check your predictions.
True or False
True and False
not(False) and True
not(False or True)
not(False and True)
False or not(True)
True and True
True or True
False or False
Analyze these results. In words, explain the behaviour of the and and or operators.
2.
if x < y:
print x, "is less than", y
elif x > y:
print x, "is greater than", y
else:
print x, "and", y, "are equal"
Encapsulate (wrap) this code in a function called compare(x, y). Call compare three times: one
each where the first argument is less than, greater than, and equal to the second argument and check
that it prints the appropriate message.
3. A script contains the following code:
secret = 55
guess = input("Guess a number between 50 and 100: ")
guess_right(secret, guess)
Write the function named guess right. Use if, else and != to print out a message reporting
whether or not the secret number was guessed correctly. Your output for a correct and an incorrect
guess could look like this:
4. Write a function named is divisible by 3 that takes a single integer as an argument and prints
“This number is divisible by three.” if the argument is evenly divisible by 3 and “This number is not
divisible by three.” otherwise. Hint: remember the mod operator % returns 0 if the first argument is
divisible by the second (so 6%3 is 0).
Now write a similar function named is divisible by 5.
5. Write a function called is divisible that takes two arguments. The function should print whether
the first is divisible by the second. Example outputs might be:
51
>>> is_divisible(3, 2)
3 is not divisible by 2
>>> is_divisible(6, 3)
6 is divisible by 3
Show your work to a demonstrator here to make sure you are on the right track.
6. Write a function water state which takes one parameter that represents a temperature in degrees
centigrade. If the temperature is greater than 100, print ”Steam”, less than 0, print”Ice”, otherwise
print ”Liquid”.
7. Write a function which asks a yes/no question of the user. If the user types any variation of Y, y, Yes,
yes, or YES print ”You chose Yes” otherwise print ”You chose No”
8. Write a script that allows users to select one of three functions to run by doing the following:
(a) Make a new, empty script called choice.py.
(b) Add three functions (function a, function b and function c) to the file. When called each
function should just print out a message saying it was called. For example:
def function_a():
print "function_a was called"
(d) Run your script and try calling it from the interpreter with different arguments. For example:
>>> dispatch(’a’)
(e) Add some code to the main routine of the script that gets the input from the user (use raw input())
and then calls dispatch.
9. Write a function which takes 3 integer arguments and prints ”equal” if any of the arguments is equal
to any other argument.
>>> any_equal(1, 2, 3)
No inputs match.
>>> any_equal(1, 2, 1)
At least two inputs are the same.
>>> any_equal(1, 1, 3)
At least two inputs are the same.
>>> any_equal("ant", "ant", 3)
At least two inputs are the same.
>>> any_equal("ant", "ant", "ant")
At least two inputs are the same.
52
10. Write a function next level which takes 6 scores as arguments. A student needs to have a total
score of 200 from the 5 best subjects in order to move on. Write a body for the function which discards
the lowest score and totals the rest, printing ”Pass” or ”Repeat” as appropriate. Hint: remember the
min() function.
11. Try to finish the game (there are twelve levels) that you have been playing in previous labs.
12. Extension Exercise: Write a function that takes 3 parameters and prints them out in order. For example:
>>> print_in_order(5, 6, 4)
In order: 4, 5, 6
13. Extension Exercise: Write a function find middle that takes 3 parameters and prints the middle one.
For example:
>>> print_middle(5, 6, 4)
The middle value is 5
14. Extension Exercise: To better understand boolean expressions, it is helpful to construct truth tables.
Two boolean expressions are logically equivalent if they have the same truth table. The following
Python function show truth table prints out the truth table for any boolean expression in two
variables p and q:
boolean ex.py:
def show_truth_table(expression):
print " p q " + expression
length = len(" p q " + expression)
print length * "-"
There is a new function in the above code: eval evaluates its argument as a python expression. You
can use eval to evaluate arbitrary strings. Let’s find out more about boolean expressions. Copy this
program to a file named boolean ex.py. Run boolean ex.py from IDLE, then use the interpreter
window to call show truth table with various strings. For example:
Use the show truth table function with the following boolean expressions, recording the truth
table produced each time:
53
(a) not(p or q)
(b) p and q
def adams():
answer = 42
guess = input("What is the answer to the ultimate question? ")
if guess < answer:
print "No silly, that's too small."
adams()
elif guess > answer:
print "You dope, that's too big."
adams()
else:
print "Well done. Wonder what the question is?"
Get a demonstrator to review your work before you leave the lab.
54
Lesson 6
Fruitful functions
The return statement allows you to terminate the execution of a function before you reach the end. One
reason to use it is if you detect an error condition:
def print_square_root(x):
if x < 0:
print "Warning: cannot take square root of a negative number."
return
result = x**0.5
print "The square root of x is", result
The function print square root has a parameter named x. The first thing it does is check whether x is
less than 0, in which case it displays an error message and then uses return to exit the function. The flow
of execution immediately returns to the caller, and the remaining lines of the function are not executed.
The built-in functions we have used, such as abs, pow, round and max, have produced results. Calling each
of these functions generates a value, which we usually assign to a variable or use as part of an expression.
biggest = max(3, 7, 2, 5)
x = abs(3 - 11) + 10
So far, none of the functions we have written has returned a value, they have printed values. In this lecture,
we are going to write functions that return values, which we will call fruitful functions, for want of a better
name. The first example is area of circle, which returns the area of a circle with the given radius:
55
def area_of_circle(radius):
if radius < 0:
print "Warning: radius must be non-negative"
return
We have seen the return statement before, but in a fruitful function the return statement includes a re-
turn value. This statement means: Return immediately from this function and use the following expression
as a return value. The expression provided can be arbitrarily complicated, so we could have written this
function more concisely:
def area_of_circle(radius):
if radius < 0:
print "Warning: radius must be non-negative"
return
On the other hand, temporary variables like area often make debugging easier. Sometimes it is useful to
have multiple return statements, one in each branch of a conditional. For instance, we have already seen
the built-in abs, now we see how to write our own:
def absolute_value(x):
if x < 0:
return -x
else:
return x
Since these return statements are in an alternative conditional, only one will be executed. As soon as
one is executed, the function terminates without executing any subsequent statements. Another way to
write the above function is to leave out the else and just follow the if condition by the second return
statement.
def absolute_value(x):
if x < 0:
return -x
return x
Think about this version and convince yourself it works the same as the first one. Code that appears any
place the flow of execution can never reach, is called dead code. In a fruitful function, it is a good idea to
ensure that every possible path through the program hits a return statement. The following version of
absolute value fails to do this:
def absolute_value(x):
if x < 0:
return -x
elif x > 0:
return x
This version is not correct because if x happens to be 0, neither condition is true, and the function ends
without hitting a return statement. In this case, the return value is a special value called None:
56
>>> print absolute_value(0)
None
>>> type(None)
<type ’NoneType’>
All Python functions return None whenever they do not return another value. So the earlier functions we
wrote that didn’t have a return statement were actually returning values, we just never checked.
The first step is to consider what a distance function should look like in Python. In other words, what are
the inputs (parameters) and what is the output (return value)? In this case, the two points are the inputs,
which we can represent using four parameters. The return value is the distance, which is a floating-point
value. Already we can write an outline of the function:
Obviously, this version of the function doesn’t compute distances; it always returns zero. But it is syntacti-
cally correct, and it will run, which means that we can test it before we make it more complicated.
To test the new function, we call it with sample values:
>>> distance(1, 2, 4, 6)
0.0
We chose these values so that the horizontal distance equals 3 and the vertical distance equals 4; that way,
the result is 5 (the hypotenuse of a 3-4-5 triangle). When testing a function, it is useful to know the right
57
answer. At this point we have confirmed that the function is syntactically correct, and we can start adding
lines of code. After each incremental change, we test the function again. If an error occurs at any point, we
know where it must be—in the last line we added.
A logical first step in the computation is to find the differences x2 − x1 and y2 − y1 . We will store those
values in temporary variables named dx and dy and print them.
If the function is working, the outputs should be 3 and 4. If so, we know that the function is getting the
right parameters and performing the first computation correctly. If not, there are only a few lines to check.
Next we compute the sum of squares of dx and dy:
Notice that we removed the print statements we wrote in the previous step. Code like that is called scaf-
folding because it is helpful for building the program but is not part of the final product. Again, we would
run the program at this stage and check the output (which should be 25). Finally, using the fractional
exponent 0.5 to find the square root, we compute and return the result:
If that works correctly, you are done. Otherwise, you might want to print the value of result before the
return statement. When you start out, you should add only a line or two of code at a time. As you gain more
experience, you might find yourself writing and debugging bigger chunks. Either way, the incremental
development process can save you a lot of debugging time.
The key aspects of the process are:
1. Start with a working program and make small incremental changes. At any point, if there is an error,
you will know exactly where it is.
2. Use temporary variables to hold intermediate values so you can output and check them.
3. Once the program is working, you might want to remove some of the scaffolding or consolidate
multiple statements into compound expressions, but only if it does not make the program difficult to
read.
58
6.4 Composition
As you should expect by now, you can call one function from within another. This ability is called compo-
sition. As an example, we’ll write a function that takes two points, the center of the circle and a point on
the perimeter, and computes the area of the circle. Assume that the center point is stored in the variables
xc and yc, and the perimeter point is in xp and yp. The first step is to find the radius of the circle, which
is the distance between the two points. Fortunately, we’ve just written a function, distance, that does just
that, so now all we have to do is use it:
The second step is to find the area of a circle with that radius and return it. Again we will use one of our
earlier functions:
result = area_of_circle(radius)
We called this function area of circle two points to distinguish it from the area of circle func-
tion defined earlier. There can only be one function with a given name within a given module. The tem-
porary variables radius and result are useful for development and debugging, but once the program is
working, we can make it more concise by composing the function calls:
The name of this function is is divisible. It is common to give boolean functions names that sound
like yes/no questions. is divisible returns either True or False to indicate whether the x is or is not
divisible by y. We can make the function more concise by taking advantage of the fact that the condition
of the if statement is itself a boolean expression. We can return it directly, avoiding the if statement
altogether:
59
This session shows the new function in action:
>>> is_divisible(6, 4)
False
>>> is_divisible(6, 3)
True
if is_divisible(x, y):
print "x is divisible by y"
else:
print "x is not divisible by y"
if is_divisible(x, y) == True:
print "x is divisible by y"
else:
print "x is not divisible by y"
6.6 Glossary
fruitful function: A function that yields a return value.
return value: The value provided as the result of a function call.
incremental development: A program development plan intended to simplify debugging by adding and
testing only a small amount of code at a time.
scaffolding: Code that is used during program development but is not part of the final version.
boolean function: A function that returns a boolean value.
composition (of functions): Calling one function from within the body of another, or using the return
value of one function as an argument to the call of another.
60
6.7 Laboratory exercises
1. Write a function called is even(n) that takes an integer as an argument and returns True if the
argument is an even number and False if it is odd .
2. Now write the function is odd(n) that returns True when n is odd and False otherwise. Finally,
modify is odd so that it uses a call to is even to determine if its argument is an odd integer.
3. Write a function called is factor which returns True if the first argument is a factor of the second.
That is, is factor(3, 12) is True whereas is factor(5, 12) is False.
4. Write a function called hypotenuse that returns the length of the hypotenuse of a right angled trian-
gle given the length of the other two sides as parameters. Here’s an initial definition:
def hypotenuse(a,b):
return 0.0
Show your work to a demonstrator here to make sure you are on the right track.
5. Copy the area of circle function definition from Section 6.2. Now write a function called area of snowman
that takes as arguments three numbers representing the radii of the head, middle and bottom sections
of a snowman. Return the area of the snowman using calls to area of circle to calculate the result.
6. Make a copy of your 3 judges program from exercise 6 on page 35. Adapt the function so it returns
the average score. Adapt the function calls so each value is stored in a variable. Write a statement in
the main routine which reports the highest average score.
7. Make a copy of your next level function from exercise 10 on page 53. Adapt the function so it
returns the best 5 total. Adapt the function calls so each value is stored in a variable. Write a statement
in the main routine which reports the highest best 5 score.
8. Make a copy of your water state function from the exercise 6 on page 52. Adapt the function so
it returns the descriptive string. The main routine should collect the temperature from the user, then
display the state e.g. H2O at 300 degrees will be steam.
9. Python functions return None whenever they do not return anything else. Using the two functions
below show that this statement is true.
def func1():
print "Function 1"
return
def func2():
print "Function 2"
10. If you didn’t complete all the levels of the game in the previous lab try to finish it this time.
11. Write a function that presents a menu as follows:
61
Depending on the user’s choice, ask for the appropriate inputs, call an appropriate function to get the
correct area and display the result sensibly.
12. Open film critic.py that you wrote in Exercise 8, page 43. Add a function called get stars
that takes a number and returns either “BOMB” ( if the argument is zero), a string of up to four stars
(depending on the argument), “MUST SEE” (if the argument is 5), or “?”. Call this function inside
rate movie instead of just printing the numerical rating.
13. Extension Exercise: Make a copy of your find middle program from exercise 13 on page 53. Adapt
the function so it returns the middle value. The main routine should collect 3 integers from the user
then display the middle number.
14. Extension Exercise: Recall that the equation for a line can be written as y = mx+c, where m is the slope
of the line and c is the y-intercept. For a line that passes through two points, (x1 , y1 ) and (x2 , y2 ), we
have the following identities:
y2 − y1
m =
x2 − x1
c = y1 − mx1
(a) Write a function slope(x1, y1, x2, y2) that returns the slope of the line through the points
(x1, y1) and (x2, y2).
(b) Write a function intercept(x1, y1, x2, y2) that returns the y-intercept of the line through
the points (x1, y1) and (x2, y2).
Get a demonstrator to review your work before you leave the lab.
62
Lesson 7
This chapter primarily deals with a development methodology called Test Driven Development (TDD).
Before we get to TDD though, we need a couple of extra parts of python: modules and triple quoted
strings.
1.
import math
print math.cos(math.pi/2.0)
This statement imports every function from the math module which are then accessed using dot
notation.
2.
from math import cos, pi
print cos(pi/2.0)
This statement imports only the definitions of cos and pi from the math library. Nothing else is
imported.
63
3.
from math import *
print cos(pi/2.0)
This statement also imports everything from the math module, the difference being that dot notation
is not needed to access module members.
It is more common to use the first or second alternative than the last. The first has the advantage of avoiding
naming conflicts (two different modules defining cos for example), at the cost of lengthier function calls.
The second has the opposite advantages and disadvantages.
We can use the help function to see the functions and data contained within modules. The keyword
module contains a single function, iskeyword, which as its name suggests is a boolean function that
returns True if a string passed to it is a keyword:
The data item, kwlist contains a list of all the current keywords in Python:
Triple quoted strings can contain both single and double quotes inside them:
64
Finally, triple quoted strings can span multiple lines:
def is_divisible_by_2_or_5(n):
"""
>>> is_divisible_by_2_or_5(8)
True
"""
if __name__ == "__main__":
import doctest
doctest.testmod()
The last three lines are what make doctest run. Put them in the main routine of any file that includes
doctests. Running the script will produce the following output:
**********************************************************************
File "myfunctions.py", line 3, in __main__.is_divisible_by_2_or_5
Failed example:
is_divisible_by_2_or_5(8)
Expected:
True
Got nothing
**********************************************************************
1 items had failures:
1 of 1 in __main__.is_divisible_by_2_or_5
***Test Failed*** 1 failures.
This is an example of a failing test. The test says: if you call is divisible by 2 or 5(8) the result
1 There are four underscores in name , two at the start and two at the end.
65
should be True. Since is divisible by 2 or 5 as written doesn’t return anything at all, the test fails,
and doctest tells us that it expected True but got nothing.
We can make this test pass by returning True:
def is_divisible_by_2_or_5(n):
"""
>>> is_divisible_by_2_or_5(8)
True
"""
return True
if __name__ == "__main__":
import doctest
doctest.testmod()
If we run it now, there will be no output, which indicates that the test passed. Note again that the doctest
string must be placed immediately after the function definition header in order to run. To see more detailed
output, add the keyword argument verbose=True to the testmod method call
doctest.testmod(verbose=True)
and run the module again. This will produce output showing the result of running the tests and whether
they pass or not.
Trying:
is_divisible_by_2_or_5(8)
Expecting:
True
ok
1 items had no tests:
__main__
1 items passed all tests:
1 tests in __main__.is_divisible_by_2_or_5
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
While the test passed, our test suite is clearly inadequate, since is divisible by 2 or 5 will now return
True no matter what argument is passed to it. Here is a completed version with a more complete test suite
and code that makes the tests pass:
66
def is_divisible_by_2_or_5(n):
"""
>>> is_divisible_by_2_or_5(8)
True
>>> is_divisible_by_2_or_5(7)
False
>>> is_divisible_by_2_or_5(5)
True
>>> is_divisible_by_2_or_5(9)
False
"""
return n % 2 == 0 or n % 5 == 0
if __name__ == "__main__":
import doctest
doctest.testmod()
Even if you don’t know how to solve a given sub-problem, and you can’t think of how to split it further, at
least you have a better chance of discovering how to solve a simpler problem (either by asking someone, or
asking Google for example).
There are also simple rules to follow when you are developing your own test cases. You should include:
• typical input
• atypical input (e.g. input at the extreme edge of allowable input)
• incorrect or invalid input
• the simplest type of input (if that makes sense)
• the most complicated type of input (if that makes sense)
67
• enough tests so that each line of code is exercised at least once in your test set (this is called test
coverage).
• keep top level statements, including function calls, together in the main routine of the program
• use Test-driven development to develop your programs (this is not part of PEP 8, but is more a phi-
losophy of software development).
7.7 Glossary
unit testing: An automatic procedure used to validate that individual units of code are working properly.
Python has doctest built in for this purpose.
module: A file containing Python definitions and statements intended for use in other Python programs.
The contents of a module are made available to the other program by using the import statement.
standard library: A library is a collection of software used as tools in the development of other software.
The standard library of a programming language is the set of such tools that are distributed with the
core programming language. Python comes with an extensive standard library.
import statement: A statement which makes the objects contained in a module available for use. There
are three forms for the import statement. Using a hypothetical module named mymod containing
functions f1 and f2, and variables v1 and v2, examples of these three forms include:
import mymod
and:
68
and:
namespace: A syntactic container providing a context for names so that the same name can reside in dif-
ferent namespaces without ambiguity. In Python, modules, classes, functions and methods all form
namespaces.
naming collision: A situation in which two or more names in a given namespace cannot be unambiguously
resolved. Using
import mymodule
instead of
dot operator: The dot operator (.) permits access to attributes and functions of a module.
docstring A string constant on the first line of a function or module definition (and as we will see later, in
class and method definitions as well). Docstrings provide a convenient way to associate documenta-
tion with code. Docstrings are also used by the doctest module for automated testing.
69
7.8 Laboratory exercises
All of the exercises below should be added to a file named doctest ex.py that contains the following in
the main routine:
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
After completing each exercise in turn, run the program to confirm that the doctests for your new function
pass.
When you are finished add your completed function with the doctests to doctest ex.py and con-
firm that the doctests pass.
Show your work to a demonstrator here to make sure you are on the right track.
3. Write a body for the function definition of fahrenheit to celsius designed to return the integer
value of the nearest degree Celsius for a given temperature in Fahrenheit. Use your favourite web
70
search engine to find the equation for doing the conversion if you don’t already know it. (Hint: you
may want to make use of the built-in function, round. Try typing help(round) in a Python shell
and experimenting with round until you are comfortable with how it works.)
def fahrenheit_to_celsius(t):
"""
>>> fahrenheit_to_celsius(212)
100
>>> fahrenheit_to_celsius(32)
0
>>> fahrenheit_to_celsius(-40)
-40
>>> fahrenheit_to_celsius(36)
2
>>> fahrenheit_to_celsius(37)
3
>>> fahrenheit_to_celsius(38)
3
>>> fahrenheit_to_celsius(39)
4
"""
4. Add a function body for celsius to fahrenheit to convert from Celsius to Fahrenheit.
def celsius_to_fahrenheit(t):
"""
>>> celsius_to_fahrenheit(0)
32
>>> celsius_to_fahrenheit(100)
212
>>> celsius_to_fahrenheit(-40)
-40
>>> celsius_to_fahrenheit(12)
54
>>> celsius_to_fahrenheit(18)
64
>>> celsius_to_fahrenheit(-48)
-54
"""
5. Extension Exercise: Write a function to convert kilograms to pounds. Devise a sensible test suite for
your function.
6. Extension Exercise: Write a function that takes the width, height and depth of a cuboid and returns
its surface area. It should return -1 if an argument is negative. Devise a sensible test suite for your
function.
71
7. Extension Exercise: Write a function that returns the volume of a square based pyramid. It should take as
arguments the length of a bottom edge and the height. It should return -1 if either parameter is less than 0.
Your function should pass the doctests below.
Get a demonstrator to review your work before you leave the lab.
72
Lesson 8
Strings part 1
The expression fruit[1] selects character number 1 from fruit. The variable letter refers to the result.
When we display letter, we get a surprise:
The first letter of “banana” is not a, unless you are a computer scientist. For perverse reasons, computer
scientists always start counting from zero. The 0th letter of “banana” is b. The first letter is a, and the second
letter is n. If you want the 0th letter of a string, you just put 0, or any expression with the value 0, in the
brackets:
>>> letter = fruit[0]
>>> print letter
b
The expression in brackets is called an index. An index specifies a member of an ordered list, in this case
the list of characters in the string. The index indicates which one you want, hence the name. It can be any
integer expression.
8.2 Length
The len function returns the number of characters in a string:
73
>>> fruit = "banana"
>>> len(fruit)
6
To get the last letter of a string, you might be tempted to try something like this:
length = len(fruit)
last = fruit[length] # ERROR!
That won’t work. It causes the runtime error IndexError: string index out of range. The reason
is that there is no 6th letter in “banana”. Since we started counting at zero, the six letters are numbered 0 to
5. To get the last character, we have to subtract 1 from length:
length = len(fruit)
last = fruit[length-1]
Alternatively, we can use negative indices, which count backward from the end of the string. The expression
fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.
The operator [n:m] returns the part of the string from the nth character to the mth character, including the
first but excluding the last. If you find this behaviour counterintuitive it might make more sense if you
imagine the indices pointing between the characters, as in the following diagram:
fruit ’ banana’
index 0 1 2 3 4 5 6
If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the
second index, the slice goes to the end of the string. Thus:
74
8.4 String comparison
The comparison operators work on strings. To see if two strings are equal:
if word == "banana":
print "Yes, we have no bananas!"
Other comparison operations are useful for putting words in alphabetical order:
You should be aware, though, that Python does not handle upper- and lowercase letters the same way that
people do. All the uppercase letters come before all the lowercase letters. As a result:
A common way to address this problem is to convert strings to a standard format, such as all lowercase,
before performing the comparison. A more difficult problem is making the program realize that zebras are
not fruit.
It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a
character in a string. For example:
Instead of producing the output Jello, world!, this code produces the runtime error TypeError:
’str’ object doesn’t support item assignment. Strings are immutable, which means you can’t
change an existing string. The best you can do is create a new string that is a variation on the original:
The solution here is to concatenate a new first letter onto a slice of greeting. This operation has no effect on
the original string.
Do you think the following will work?
75
8.6 Traversal and the for loop
Recall (on page 3) we said that all programming languages allowed you to perform a few basic operations:
get input, display output, do math, do conditional execution and then there was just one more thing. The
last thing we need to add to the list is repetition, the ability to loop through a set of statements repeatedly.
We will look at this in a lot more detail later (Lectures 10 and 11) but there is a special type of loop that
is particularly useful with strings (and other compound types) which is worth introducing while we are
looking at strings.
A lot of computations involve processing a string one character at a time. Often they start at the beginning,
select each character in turn, do something to it, and continue until the end. This pattern of processing is
called a traversal. Python provides a very useful language feature for traversing many compound types—
the for loop:
The above piece of code can be understood as an abbreviated version of an English sentence: “For each
character in the string fruit, print out the character”. The for loop is an example of an iterator: something
that visits or selects every element in a structure (in this case a string), usually in turn. The for loop also
works on other compound types such as lists and tuples, which we will look at later.
The following example shows how to use concatenation and a for loop to generate an abecedarian series.
Abecedarian refers to a series or list in which the elements appear in alphabetical order. For example, in
Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack,
Nack, Ouack, Pack, and Quack. This loop outputs these names in order:
prefixes = "JKLMNOPQ"
suffix = "ack"
for letter in prefixes:
print letter + suffix
Jack
Kack
Lack
Mack
Nack
Oack
Pack
Qack
Of course, that’s not quite right because Ouack and Quack are misspelled. You’ll fix this as an exercise
below.
76
>>> "p" in "apple"
True
>>> "i" in "apple"
False
>>> "ap" in "apple"
True
>>> "pa" in "apple"
False
Combining the in operator with string concatenation using +, we can write a function that removes all the
vowels from a string:
def remove_vowels(s):
# vowels contains all the letters we want to remove
vowels = "aeiouAEIOU"
s_without_vowels = ""
# scan through each letter in the input string
for letter in s:
# check if the letter is not in the disallowed list of letters
if letter not in vowels:
# the letter is allowed, add it to the result
s_without_vowels += letter
return s_without_vowels
77
The len function returns the length of the sequence which is given as an argument. The upper method
returns a new string which is the same as the string that it is called upon except that each character has
been converted to uppercase. In each case the original string remains unchanged.
An example of a method which needs an argument to operate on is the count method.
>>> my_str = "the quick brown fox jumps over the lazy dog."
>>> my_str.count("the")
2
>>> my_str.count("hello")
0
>>> my_str.count("e")
3
The count method returns the number of times the string given as an argument occurs within the string
that it is called upon. But what about the following:
>>> ms = "ahaha"
>>> ms.count("aha")
The str type contains useful methods that manipulate strings. To see what methods are available, use the
dir function with str as an argument.
>>> dir(str)
To find out more about an item in this list, we can use the help command:
>>> help(str.capitalize)
Help on method_descriptor:
capitalize(...)
S.capitalize() -> string
78
>>> s = "brendan"
>>> s.capitalize()
’Brendan’
str.find is a useful method for finding substrings in a string. To find out more about it, we can print out
its docstring, doc , which contains documentation on the function:
Return -1 on failure.
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
Return -1 on failure.
The parameters in square brackets are optional parameters. We can use str.find to find the location of a
character in a string:
Or a substring:
>>> fruit.find("na")
2
It also takes an additional argument that specifies the index at which it should start:
>>> fruit.find("na", 3)
4
And has a second optional parameter specifying the index at which the search should end:
>>> "bob".find("b", 1, 2)
-1
In this example, the search fails because the letter b does not appear in the index range from 1 to 2 (not
79
including 2).
As with strings, you can access a single element using square brackets containing the appropriate index
number:
>>> s = "The quick brown fox jumps over the lazy dog."
>>> words = s.split()
>>> print words
[’The’, ’quick’, ’brown’, ’fox’, ’jumps’, ’over’, ’the’, ’lazy’, ’dog.’]
By default the string is split on white space. What do you think s.split(’e’) would produce?
Just as we used the for loop to traverse a string one character at a time, we can now use it to traverse our
list one word at a time. The following function finds the length of the longest word in a string:
def find_longest_word_length(s):
words = s.split()
longest = 0
for word in words:
if len(word) > longest:
longest = len(word)
return longest
80
>>> words = ["qwerty", "type", "giraffe", "manic"]
>>> "qwerty" in words
True
>>> "asdfgh" in words
False
If I want to check that a string contains a certain word e.g. ’the’, why should I split the string first?
In this chapter and some of the following chapters, we’re going to look at common patterns that can be
used to solve certain classes of problems. These patterns are meant to be general but easily adapted to
the specific problem. Building up your vocabulary of patterns is an important aspect of becoming a good
programmer. Each of the following subsections is an example of a particular string pattern.
def print_a_string_pattern(s):
for ch in s:
# possible if statement here
print ch
def print_vowels(s):
for ch in s:
if ch in 'aeiouAEIOU':
print ch
def build_a_string_copy(s):
result = '' # initialise the resulting string to empty
for ch in s:
# possible if statement here
result += ch # add the new character to the end of the result
return result
81
def replace_vowels_with_X(s):
result = '' # initialise the resulting string to empty
for ch in s:
if ch in 'aeiouAEIOU':
result += 'X' # add an X instead of ch
else:
result += ch # add ch
return result
def count_characters(s):
count = 0 # initialise the number of characters to 0
for ch in s:
# possible if statement here
count += 1 # a new character
return count
For example, to count the number of times the letter a appears in a string:
def count_a_s(s):
count = 0
for ch in s:
if ch == 'a':
count += 1
return count
8.11 Glossary
compound data type: A data type in which the values are made up of components, or elements, that are
themselves values.
index: A variable or value used to select a member of an ordered set, such as a character from a string.
traverse: To iterate through the elements of a set, performing a similar operation on each.
slice: A part of a string (substring) specified by a range of indices. More generally, a subsequence of any
sequence type in Python can be created using the slice operator (sequence [start:stop]).
immutable: A compound data types whose elements can not be assigned new values.
method: A function that is attached to a particular type and can be accessed via dot notation. For example
‘‘monday’’.upper()
82
8.12 Laboratory exercises
1. Replace the ’?’ to achieve the shown output.
2. Write a function on many lines that takes a string as an argument and uses a for loop to print the
letter one per line. For example:
>>> on_many_lines("banana")
b
a
n
a
n
a
>>> saying.upper()
>>> saying.count("e")
>>> saying[0].isupper()
>>> saying[:2].isalpha()
Show your work to a demonstrator here to make sure you are on the right track.
4. Modify:
prefixes = "JKLMNOPQ"
suffix = "ack"
for letter in prefixes:
print letter + suffix
so that Ouack and Quack are spelled correctly. You will need to use an if statement inside the for
loop to decide if a different suffix is needed.
83
5. Encapsulate (or wrap):
fruit = "banana"
count = 0
for char in fruit:
if char == "a":
count += 1
print count
in a function named count letters, and generalise it so that it accepts the string and the letter as ar-
guments. When you are finished you will be able to call the function like this: count letters("banana",
"a") to count the ’a’s in ’banana’ or like this: count letters("fruit fly", "f") to count the
’f’s in ’fruit fly’ and so on.
6. Write a function only upper that takes a string as an argument and uses a for loop to print only the
uppercase letters. For example:
You will need to use the isupper method. See help(str.isupper)for more information.
7. Write a function that takes a string converts it to a binary string where vowels are replaced by 0 and
consonants are replaced by 1. The function should return the binary string. Here are the doctests:
def binary_string(s):
"""
>>> binary_string("Karen")
'10101'
>>> binary_string("Hello World!")
'10110 10111!'
"""
if __name__ == '__main__':
import doctest
doctest.testmod(verbose = True)
8. Write a function that takes a string and returns the total scrabble value of the letters. You may assume
the rack only contains uppercase letters or an underscore (which represents a blank and is worth
zero):
1 point E, A, I, O, N, R, T, L, S, U
2 points D, G
3 points B, C, M, P
4 points F, H, V, W, Y
5 points K
8 points J, X
10 points Q, Z
def scrabble_score(rack):
"""
>>> scrabble_score("AABBWOL")
84
14
>>> scrabble_score("QWERTYU")
22
>>> scrabble_score("QUIZ_ED")
25
"""
if __name__ == '__main__':
import doctest
doctest.testmod(verbose = True)
9. Write a function that returns a string with every xth character (starting at zero) followed by an aster-
isk.
if __name__ == '__main__':
import doctest
doctest.testmod(verbose = True)
10. Create a file named stringtools.py and put the following in it:
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Write a function body for it that will make it work as indicated by the doctests. Remember that the
strings (such as ’pple’) should be returned by the function and not printed by the function (and this is
the case for almost all doctest tested functions).
85
def reverse(s):
"""
>>> reverse("happy")
'yppah'
>>> reverse("Python")
'nohtyP'
>>> reverse("")
''
>>> reverse("P")
'P'
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Add a function body to reverse to make the doctests pass. There are two ways to do this exercise.
One way uses a for loop over the input string and adds each character to the start of the newly con-
structed string. The second way uses slicing.
12. Add mirror to stringtools.py.
def mirror(s):
"""
>>> mirror("good")
'gooddoog'
>>> mirror("yes")
'yessey'
>>> mirror("Python")
'PythonnohtyP'
>>> mirror("")
''
>>> mirror("a")
'aa'
"""
Write a function body for it that will make it work as indicated by the doctests. Hint: use reverse from
the previous exercise to create a second string.
13. Extension Exercise: Write a function remove duplicates which takes a string argument and returns
a string which is the same as the argument except only the first occurrence of each letter is present.
Make your function case sensitive, and ensure that it passes the doctests below.
def remove_duplicates(strng):
"""
>>> remove_duplicates("apple")
'aple'
>>> remove_duplicates("Mississippi")
'Misp'
>>> remove_duplicates("The quick brown fox jumps over the lazy dog")
'The quick brown fx jmps v t lazy dg'
"""
14. Extension Exercise: Convert a date read from the user, given in DD/MM/YYYY format into written
format. For example, here’s a run of how the program might work:
86
Enter a date in DD/MM/YY format: 16/7/2003
Output: 16th July, 2003
There are two problems to solve here: converting a numeric month into the name for the month, and
getting the correct suffix to the day (st, nd, rd, th). Both problems can be solved with judicious use of
lists.
Get a demonstrator to review your work before you leave the lab.
87
88
Lesson 9
Files
Remember at the start of the course we said that programs are made up of instructions which only perform
a handful of operations1 . The first two of these operations were input and output. Input involves getting
data from the keyboard, a file, or some other device. Output is concerned with displaying data on the
screen or sending data to a file or other device.
We have already seen how to generate output to the screen and get input from the keyboard. In this lecture
we are going to look at getting input from a file and generating output to a file.
9.1 Files
While a program is running, its data is stored in random access memory (RAM). RAM is fast and inexpensive,
but it is also volatile, which means that when the program ends, or the computer shuts down, data in RAM
disappears. To make data available the next time you turn on your computer and start your program, you
have to write it to a non-volatile storage medium, such as a hard drive, USB drive, or optical disk. Data on
non-volatile storage media is stored in named locations called files. By reading and writing files, programs
can save information between program runs.
Working with files is a lot like working with a notebook. To use a notebook, you have to open it. When
you’re done, you have to close it. While the notebook is open, you can either write in it or read from it. In
either case, you know where you are in the notebook. You can read the whole notebook in its natural order
or you can skip around. All of this applies to files as well. To open a file, you specify its name and indicate
whether you want to read or write.
Opening a file creates a file object. In this example, the variable myfile refers to the new file object.
The open function takes two arguments. The first is the name of the file, and the second is the mode. Mode
"w" means that we are opening the file for writing. If there is no file named test.txt, it will be created.
If there already is one, it will be replaced by the file we are writing. When we print the file object, we see
the name of the file, the mode, and the location of the object in memory. To put data in the file we invoke
the write method on the file object:
89
>>> myfile.write("Now is the time")
>>> myfile.write("to close the file")
Closing the file tells the system that we are done writing and makes sure that everything actually gets
written to disk.
>>> myfile.close()
Now we can open the file again, this time for reading, and read the contents into a string. This time, the
mode argument is "r" for reading:
Actually the second argument is optional, since the default mode when opening a file is "r". So we could
have just done:
If we try to open a file for reading that doesn’t exist, we get an error:
Not surprisingly, the read method of a file reads data from the file. With no arguments, it reads the entire
contents of the file into a single string:
There is no space between time and to because we did not write a space between the strings.
The read method can also take an argument that indicates how many characters to read:
If not enough characters are left in the file, read returns the remaining characters. When we get to the end
of the file, read returns the empty string:
>>>
90
Mary had a little lamb
little lamb little lamb
Mary had a little lamb
its fleece was white as snow
and everywhere that Mary went
Mary went Mary went
everywhere that Mary went
the lamb was sure to go
We can start by opening the file and reading its contents into a string.
Those funny little \n characters which you can see in text string represent newlines in the file. They il-
lustrate the very important distinction between representation and presentation. Pretty much all computer
programs try to separate out these concepts. Clearly, it would be difficult to use the same representation
and presentation for a newline since newlines are not visible in the usual sense. Rather, they are examples
of a control character, which goes back to the days of teletypes and typewriters, that instructs the device
to do something other than print. In the case of newlines, the instruction is to go to the next line before
resuming printing. Python still needs ways of representing these control characters, and it adopts the same
notation as many other programming languages where control characters are specified by backslashes. If
you want to explicitly see how a string is represented in Python, you can use the function repr:
>>> a = ’hello\nthere’
>>> a
’hello\nthere’
>>> print a
hello
there
>>> print repr(a)
’hello\nthere’
Now that we have the entire file in text string we can use split to get a list of words from the string.
A list is another python sequence type and has many properties similar to a string. A list is designated
by comma separated values of any type surrounded by square braces. We will see more of lists in Lecture
13, but for now just think of them as a sequence of values. Once we have a list, we can use the len function
to tell us how many items are in the list. The number of items in the list will be exactly the same as the
number of words in the file.
91
39
What if we wanted to create a new file which contained a poem about John instead of Mary? We already
have the poem about Mary in text string so all we need to do is change every occurrence of Mary to
John and then write the new poem to a file.
9.3 Directories
Files on non-volatile storage media are organized by a set of rules known as a file system. File systems
are made up of files and directories, which are containers for both files and other directories. When you
create a new file by opening it and writing, the new file goes in the current directory (wherever you were
when you ran the program). Similarly, when you open a file for reading, Python looks for it in the current
directory.
If you open a python interpreter with IDLE, the current working directory may not be where you think. To
find out what directory we’re currently in, we need to use the os module which holds information about
various aspects of the operating system. Here is what happens on my Linux machine:
>>> import os
>>> os.getcwd()
’/home/cshome/m/mccane/’
/home/cshome/m/mccane/ is Brendan’s home directory and happens to be where I started IDLE from.
If you want to open a file somewhere else, you have to specify the path to the file, which is the name of the
directory (or folder) where the file is located:
This example opens a file named words that resides in a directory named dict, which resides in share,
which resides in usr, which resides in the top-level directory of the system, called /. It then reads in each
line into a list using readlines, and prints out the first 5 elements from that list. You cannot use / as part of
a filename; it is reserved as a delimiter between directory and filenames. The file /usr/share/dict/words
should exist on unix based systems, and contains a list of words in alphabetical order.
Alternatively, you can change the current working directory using the method os.chdir(DIRECTORYNAME),
where DIRECTORYNAME is a string indicating the new directory. For example, to change the current work-
ing directory to your home directory, try the following:
>>> import os
92
>>> os.chdir(os.path.expanduser("˜"))
’/home/cshome/m/mccane’
>>> os.getcwd()
’/home/cshome/m/mccane’
import urllib2
pagefile = urllib2.urlopen("http://www.cs.otago.ac.nz")
page = pagefile.read()
print page
Which results in output that looks a bit like the following (well, the first part does):
</head>
...
Note that urllib2.urlopen behaves very similar to open, and pagefile acts just like a file opened for
reading. Essentially, this is pretty much what a web-browser does. It goes and gets a web page (as a file),
and then displays the result. What distinguishes most web-browsers though is how it does the displaying
- we won’t go into that here.
Now that we have the whole page in a string, we can start to do some pretty interesting things with it. For
example, we might want to split the string into it’s constituent words:
OK, so perhaps not so interesting yet, but we might be interested in only those words which have the string
“http” in them:
2 urllib2 is a Python 2.x module. In Python 3.x, the functionality is split over several libraries. The simplest is urllib.request
93
>>> for word in words:
... if "http" in word:
... print word
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
xmlns="http://www.w3.org/1999/xhtml"
http-equiv="content-type"
href="http://www.otago.ac.nz">
href="http://www.cs.otago.ac.nz/student/papers.php">here</a>.
href="http://www.ict.org.nz/index.php/04112009_ict-skills-shortage-confirmed">the
href="http://www.otago.ac.nz">University
That’s not quite what I’d like to extract. What I really want is just the string with the “http” bit in it. I’m
going to cheat a bit here and redo my split just for the current purpose3 . Here’s the new code:
Now the only strings printed are those which are urls. A program that can scan a web page and extract the
urls could be useful for: crawling the web, building a search engine, downloading just your favourite web
pages without having to use a browser, or any number of things.
The simplest pattern is to read the whole file into a string, and then apply whatever string pattern is relevant
to the problem. The pattern looks like this:
3 Python also has a library for parsing (that’s kind of what we’re doing here) web pages. It’s called HTMLParser. In general, it
would be better to make use of HTMLParser, but that would overcomplicate matters at the moment.
94
filename = 'words.txt' # or whatever the filename is
wordfile = open(filename) # first open the file
text_str = wordfile.read() # read entire file into string text_str
wordfile.close() # close the file
for ch in text_str:
if ch not in 'aeiouAEIOU':
print ch, # the trailing comma suppresses a newline
The variable all lines in this example is a list of strings. We haven’t officially looked at lists yet. For
now you can think of them as behaving a little like a string, except each element in the list can be any value,
whereas the elements in a string are characters. Note that the readlines method keeps the ’
n’ character at the end of each line.
9.6 Glossary
volatile memory: Memory which requires an electrical current to maintain state. The main memory or RAM
of a computer is volatile. Information stored in RAM is lost when the computer is turned off.
non-volatile memory: Memory that can maintain its state without power. Hard drives, flash drives, and
optical disks (CD or DVD) are each examples of non-volatile memory.
file: A named entity, usually stored on a hard drive, floppy disk, or CD-ROM, that contains a stream of
characters.
mode: A distinct method of operation within a computer program. Files in Python can be opened in one
of three modes: read (”r”), write (”w”), and append (”a”).
directory: A special type of file that contains other files (and directories).
95
path: A sequence of directory names that specifies the exact location of a file.
text file: A file that contains printable characters organized into lines separated by newline characters.
list: A sequence of values separated by commas and surrounded by square braces. E.g. [1, 2, 3] or
[’comma’, ’separated’, ’values’].
control character: A non-printing character that can be part of a string and is specified by a backslash
followed by an actual character. They are instructions for how to print out strings. For example the
newline control character \n tells Python to go to the next line before continuing to print.
2. Write a function which takes a filename as its argument and returns a string with all \n characters
replaced by the ” ” character:
>>> one_line("words.txt")
’I have a bunch_of red roses_’
Show your work to a demonstrator here to make sure you are on the right track.
3. Write a function which takes 2 arguments, a filename and an integer (called target). The function
should return the word at the target position. Doctests:
>>> find_word("words.txt", 0)
'I'
>>> find_word("words.txt", 1)
'have'
4. Write a function that takes 2 arguments, a filename and a string and returns True if the string appears
in the file.
5. Write a function which takes 3 arguments, a filename and 2 strings (called target1 and target2). The
function should return True if the target words are both found in the file.
6. Extend your program from the first question so that it asks the user for a file to print, and then prints
out the contents of that file.
7. Extend your program so that it first prints out all the files in the current working directory before
asking for a file to print. Hint: use the os.listdir(".") function to list the files in the current
directory (don’t forget to import os first).
96
Show your work to a demonstrator here to make sure you are on the right track.
8. Modify your program (perhaps create a copy of it), so that it asks the user for two file names and
prints out which file is longer (has more characters).
9. Modify your program so that it prints out which of the two files has more words.
10. Put the code from Section 9.4 into a script and run it to see that it works. Now extend the program so
that it asks the user for a web-page to open, then opens it and prints all the links in that page.
11. Extension Exercise: Extend your answer from Question 7 so that if the requested file is a directory it
will list the contents of the directory instead of printing the contents of a file. Make use of the function
os.path.isdir to tell if a given file is a directory.
12. Extension Exercise: Modify your program from Question 10 so that it lists all links in the web page,
then prints all the links in one of those web pages as well.
13. Extension Exercise: Adapt film critic.py from Exercise 12 and instead of printing the program’s
output to the screen, write it to a new file called new file.txt. Make sure the output still contains
line breaks in the appropriate places.
14. Extension Exercise: Write a program that reads in a file and calculates the average word length. The
code at the end of Section 9.4 in the book is a good starting place.
Get a demonstrator to review your work before you leave the lab.
97
98
Lesson 10
Iteration: part 1
bruce = 5
print bruce,
bruce = 7
print bruce
The output of this program is 5 7, because the first time bruce is printed, his value is 5, and the second
time, his value is 7. The comma at the end of the first print statement suppresses the newline after the
output, which is why both outputs appear on the same line. Here is what multiple assignment looks like
in a state diagram:
5
bruce
7
With multiple assignment it is especially important to distinguish between an assignment operation and a
statement of equality. Because Python uses the equal sign (=) for assignment, it is tempting to interpret a
statement like a = b as a statement of equality. It is not! First, equality is symmetric and assignment is not.
For example, in mathematics, if a = 7 then 7 = a. But in Python, the statement a = 7 is legal and 7 = a is
not. Furthermore, in mathematics, a statement of equality is always true. If a = b now, then a will always
equal b. In Python, an assignment statement can make two variables equal, but they don’t have to stay that
way:
a = 5
b = a # a and b are now equal
a = 3 # a and b are no longer equal
The third line changes the value of a but does not change the value of b, so they are no longer equal.
(In some programming languages, a different symbol is used for assignment, such as <- or :=, to avoid
confusion.)
99
10.2 Updating variables
One of the most common forms of multiple assignment is an update, where the new value of the variable
depends on the old.
x = x + 1
This means get the current value of x, add one, and then update x with the new value. If you try to update
a variable that doesn’t exist, you get an error, because Python evaluates the expression on the right side of
the assignment operator before it assigns the resulting value to the name on the left:
>>> x = x + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ’x’ is not defined
Before you can update a variable, you have to initialize it, usually with a simple assignment:
>>> x = 0
>>> x = x + 1
>>>
>>> count = 0
>>> count += 1
>>> count
1
>>> count += 1
>>> count
2
>>>
count += 1 is an abbreviation for count = count + 1. The increment value does not have to be 1:
>>> n = 2
>>> n += 5
>>> n
7
>>>
Python also allows the abbreviations -=, *=, /=, and %=:
>>> n = 2
>>> n *= 5
>>> n
10
>>> n -= 4
100
>>> n
6
>>> n /= 2
>>> n
3
>>> n %= 2
>>> n
1
You can almost read the while statement as if it were English. It means, while n is greater than 0, continue
displaying the value of n and then reducing the value of n by 1. When you get to 0, display the word
Blastoff! More formally, here is the flow of execution for a while statement:
The body consists of all of the statements below the header with the same indentation.
This type of flow is called a loop because the third step loops back around to the top. Notice that if the
condition is false the first time through the loop, the statements inside the loop are never executed. The
body of the loop should change the value of one or more variables so that eventually the condition becomes
false and the loop terminates. Otherwise the loop will repeat forever, which is called an infinite loop. An
endless source of amusement for computer scientists is the observation that the directions on shampoo –
Lather, rinse, repeat – are an infinite loop.
In the case of countdown, we can prove that the loop terminates because we know that the value of n is
finite, and we can see that the value of n gets smaller each time through the loop, so eventually we have to
get to 0. In other cases, it is not so easy to tell:
def collatz_sequence(n):
while n != 1:
print n,
if n % 2 == 0: # n is even
n = n / 2
else: # n is odd
n = n * 3 + 1
101
The condition for this loop is n != 1, so the loop will continue until n is 1, which will make the condition
false. Each time through the loop, the program outputs the value of n and then checks whether it is even
or odd. If it is even, the value of n is divided by 2. If it is odd, the value is replaced by n * 3 + 1 . For
example, if the starting value (the argument passed to collatz sequence) is 3, the resulting sequence
is 3, 10, 5, 16, 8, 4, 2, 1. Since n sometimes increases and sometimes decreases, there is no obvious proof
that n will ever reach 1, or that the program terminates. For some particular values of n, we can prove
termination. For example, if the starting value is a power of two, then the value of n will be even each time
through the loop until it reaches 1. The previous example ends with such a sequence, starting with 16.
Particular values aside, the interesting question is whether we can prove that this program terminates for
all values of n. So far, no one has been able to prove it or disprove it!
To write effective computer programs a programmer needs to develop the ability to trace the execution of a
computer program. Tracing involves simulating the computer and following the flow of execution through
a sample program run, recording the state of all variables and any output the program generates after each
instruction is executed. To understand this process, let’s trace the call to collatz sequence(3) from the
previous section. At the start of the trace, we have a local variable, n (the parameter), with an initial value
of 3. Since 3 is not equal to 1, the while loop body is executed. 3 is printed and 3 % 2 == 0 is evaluated.
Since it evaluates to False, the else branch is executed and 3 * 3 + 1 is evaluated and assigned to n.
To keep track of all this as you hand trace a program, make a column heading on a piece of paper for each
variable created as the program runs and another one for output. Our trace so far would look something
like this:
n output
--- ------
3 3
10
Since 10 != 1 evaluates to True, the loop body is again executed, and 10 is printed. 10 % 2 == 0 is
true, so the if branch is executed and n becomes 5. By the end of the trace we have:
n output
--- ------
3 3
10 10
5 5
16 16
8 8
4 4
2 2
1
Tracing can be a bit tedious and error prone (that’s why we get computers to do this stuff in the first place!),
but it is an essential skill for a programmer to have. From this trace we can learn a lot about the way our
code works. We can observe that as soon as n becomes a power of 2, for example, the program will require
log2 (n) executions of the loop body to complete. We can also see that the final 1 will not be printed as
output.
102
10.6 Counting digits
The following function counts the number of decimal digits in a positive integer expressed in decimal
format:
def num_digits(n):
count = 0
while n > 0:
count = count + 1
n = n / 10
return count
A call to num digits(710) will return 3. Trace the execution of this function call to convince yourself
that it works. This function demonstrates another pattern of computation called a counter . The variable
count is initialized to 0 and then incremented each time the loop body is executed. When the loop exits,
count contains the result – the total number of times the loop body was executed, which is the same as
the number of digits. If we wanted to only count digits that are either 0 or 5, adding a conditional before
incrementing the counter will do the trick:
def num_zero_and_five_digits(n):
count = 0
while n > 0:
digit = n % 10
if digit == 0 or digit == 5:
count = count + 1
n = n / 10
return count
10.7 Tables
One of the things loops are good for is generating tabular data. Before computers were readily available,
people had to calculate logarithms, sines and cosines, and other mathematical functions by hand. To make
that easier, mathematics books contained long tables listing the values of these functions. Creating the tables
was slow and boring, and they tended to be full of errors. When computers appeared on the scene, one of
the initial reactions was, “This is great! We can use the computers to generate the tables, so there will be no
errors.” That turned out to be true (mostly) but shortsighted. Soon thereafter, computers and calculators
were so pervasive that the tables became obsolete. Well, almost. For some operations, computers use tables
of values to get an approximate answer and then perform computations to improve the approximation. In
some cases, there have been errors in the underlying tables, most famously in the table the Intel Pentium
used to perform floating-point division.
Although a log table is not as useful as it once was, it still makes a good example of iteration. The following
program outputs a sequence of values in the left column and 2 raised to the power of that value in the right
column:
x = 1
while x < 13:
print x, "\t", 2**x
x += 1
103
The string ’\t’ represents a tab character. The backslash character in ’\t’ indicates the beginning of an
escape sequence . Escape sequences are used to represent invisible characters like tabs and newlines. The
sequence \n represents a newline. An escape sequence can appear anywhere in a string; in this example,
the tab escape sequence is the only thing in the string. How do you think you represent a backslash in a
string?
As characters and strings are displayed on the screen, an invisible marker called the cursor keeps track of
where the next character will go. After a print statement, the cursor normally goes to the beginning of the
next line. The tab character shifts the cursor to the right until it reaches one of the tab stops. Tabs are useful
for making columns of text line up, as in the output of the previous program:
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
Because of the tab characters between the columns, the position of the second column does not depend on
the number of digits in the first column.
All while loops look like this. Most importantly, you must ensure that there is a chance the condition will
change through each iteration, otherwise, there could be an infinite loop (the program keeps going until the
heat death of the universe).
104
keep_looping = True
while keep_looping:
# read data from somewhere
if <data is valid>:
keep_looping = False
# do something with data
For example, to get a user to enter a mobile phone number and check that the number is valid, we might
do the following:
number_not_valid = True
while number_not_valid:
mobile_number = raw_input('Enter your mobile phone number: ')
if len(mobile_number)==10 and mobile_number.isdigit():
number_not_valid = False
else:
print 'There is an error in your number.'
print 'your mobile number is: ', mobile_number
def guess_number(number):
guess = int(raw_input('Guess a number between 1 and 10: '))
while guess != number:
guess = int(raw_input('Wrong. Guess again: '))
print 'Congratulations, you guessed right.'
guess_number(5)
If you also wanted to count the number of guesses needed, you could use a counting pattern:
def guess_number(number):
guess = int(raw_input('Guess a number between 1 and 10: '))
num_guesses = 1
while guess != number:
guess = int(raw_input('Wrong. Guess again: '))
num_guesses += 1
print 'Congratulations, you guessed right in ', num_guesses, 'guesses.'
guess_number(5)
Another file pattern involves reading one line at a time and doing something with each line as you go. This
is especially useful if the file is very long as there is no need to read the whole file into memory, or you want
to search for a specific thing in a file, and you don’t want to read the whole file.
105
filename = 'words.txt' # or whatever the filename is
wordfile = open(filename) # first open the file
one_line = wordfile.readline() # reads a single line from the file
while one_line:
# do something with oneline
one_line = wordfile.readline() # read the next line
wordfile.close() # close the file
def ret_test(s):
"""
>>> ret_test("dog")
'dog\\ndog'
"""
return s + '\n' + s
def prt_test(s):
"""
>>> prt_test("cat")
cat
cat
"""
print s + "\n" + s
10.9 Glossary
multiple assignment: Making more than one assignment to the same variable during the execution of a
program.
initialization (of a variable): To initialize a variable is to give it an initial value, usually in the context of
multiple assignment. Since in Python variables don’t exist until they are assigned values, they are
initialized when they are created. In other programming languages this is not the case, and variables
can be created without being initialized, in which case they have either default or garbage values.
106
increment Both as a noun and as a verb, increment means to increase by 1.
decrement Decrease by 1.
iteration: Repeated execution of a set of programming statements.
loop: A statement or group of statements that execute repeatedly until a terminating condition is satisfied.
107
10.10 Laboratory exercises
There are a lot of exercises for this lesson. If you feel like you’ve mastered the concepts, then there is no
need to do all of them. However, they are all good practice for the mastery progressions and the practical
test.
When starting to write loops it can be easy to make a mistake and end up in an infinite loop. If you find
yourself in this situation you can exit from it by pressing Ctrl-C.
1. Write a function which will take a string parameter and return a string with all the vowels replaced
by “ ”.
Make a text file with a series of words e.g. Hubble bubble toil and trouble.
In the main routine, write code to open the file, read the contents to a string, split the string into a list
of “words”, and use a for loop to send each word to the function you wrote. For example:
H_bbl_
b_bbl_
t__l
_nd
tr__bl_
2. (a) Write a function which will take a string parameter and returns that string with a “$” sign before
it and “,000” after it. Make a text file and populate it with a series of integers e.g. 35 15 353 1 30
54 44 501.
In the main routine, write code to open the file, read the contents to a string, split the string into a
list of ”words”, use a for loop to send each word to the function you wrote, and print the result.
(b) Add K to some of the integers of the text file e.g. 35K 15 353K 1K 30 54K 44K 501. Adapt the
function so that if a “K” is part of the number, “,000” is appended as before after removing the
“K”. If “K” is not part of the number, then simply return the number. For example, a file with
the above contents would result in:
$35,000
$15
$353,000
$1,000
$30
$54,000
$44,000
$501
3. Recall num digits from page 103. What will num digits(0) return? Modify it to return 1 for this
case. Modify num digits so that it works correctly with any integer value. Type the following into
a script.
def num_digits(n):
"""
>>> num_digits(12345)
5
>>> num_digits(0)
1
>>> num_digits(-12345)
5
108
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Add your function body to num digits and confirm that it passes the doctests, by running it.
Show your work to a demonstrator here to make sure you are on the right track.
4. Add the following to your script.
def num_even_digits(n):
"""
>>> num_even_digits(123456)
3
>>> num_even_digits(2468)
4
>>> num_even_digits(1357)
0
>>> num_even_digits(2)
1
>>> num_even_digits(20)
2
"""
Write a body for num even digits and run it to check that it works as expected.
5. Add the following to your program:
def print_digits(n):
"""
>>> print_digits(13789)
9 8 7 3 1
>>> print_digits(39874613)
3 1 6 4 7 8 9 3
>>> print_digits(213141)
1 4 1 3 1 2
"""
Write a body for print digits so that it passes the given doctests. In this case you can use the print
statement rather than a return statement if you like. Using print with a comma at the end will stop print
from moving to a new line.
6. Write a function which takes two arguments, a string and a target letter. Determine and return the
encrypted word. The encrypted word is made up of the characters after each target letter, in order.
Here are the doctests:
"""
>>> decode("I want no more to endure the names I am called by many people", 'n')
'today'
>>> decode("Hungry hares in lairs","r")
'yes'
>>> decode("Turn around dear","r")
'no'
109
"""
To pass the 3rd doctest, you will need to check that the search has not gone on past the end of the
string.
7. Write a function which takes two arguments, a filename and an integer representing line-length. Print
the text of the file out in lines of the length specified. In the output below, the spaces are specified
using the ’ ’ character so you can see them, but they should be actual spaces in the doctest in your
code. Ignore end-of-line characters (\n). How will you deal with the last line? Here are example
doctests:
"""
>>> line_length("words.txt",7)
I_have_
a_bunch
of_red_
roses
"""
123,15,2014
4264,25,1996
2014,22,1998
4721,16,2014
Each line of a file contains 3 items, ID, age and year. Each item is separated by a comma. Read the file
and display the data separated by space. Describe the data with a header row. The doctests are:
"""
>>> id_format("id_file.txt")
ID age year
123 15 2014
4264 25 1996
2014 22 1998
4721 16 2014
"""
9. Use the same file from the previous question. Read the file. Count how many lines match the target
year (last item). The doctests are:
"""
>>> id_format3("id_file.txt",2014)
2
"""
10. Write a function which takes a filename as an argument. Print the first word of each line in the file.
Doctest example:
"""
>>> print_first_word("words.txt")
I of
"""
110
Show your work to a demonstrator here to make sure you are on the right track.
11. Write a function which takes a filename as an argument. For each line in the file, print the first word,
and then print a ”w” for any other word. Doctest example:
"""
>>> print_line3("words.txt")
I w w w
of w w
"""
12. Write a function which takes a filename and a character as arguments. Print any line which begins
with the character. (Create your own file. Don’t worry about a doctest.)
13. For this question, create a file called fishy.txt that contains the lines:
One Fish
Two Fish
Red Fish
Blue Fish
(a) Print the file with the newline character removed from every odd-numbered line. For example:
>>> compacta("fishy.txt")
One fishTwo fish
Red fishBlue fish
(b) Create a function that removes the newline character from every odd-numbered line (you may
already have one from the previous question). Return a string with all the output in one string.
Here is a doctest:
"""
>>> compactb("fishy.txt")
'One fishTwo fish\\nRed fishBlue fish\\n'
"""
14. Create a file called numbers.txt that has an integer on every line. Modify the last few lines of your
script from Questions 3: comment out the doctest lines; and add lines which read data from a file:
if __name__ == "__main__"
# import doctest
# doctest.testmod(verbose=True)
numfile = open("numbers.txt")
line = numfile.readline()
while line:
print line,
line = numfile.readline()
numfile.close()
Unlike in the last chapter, when we read an entire file in at once using read or readlines, here we
are reading one line at a time using the readline method. When we reach the end of the file then
while line: will evaluate to False and our loop will finish. Also notice the “,” at the end of the
print statement to prevent an extra newline from being added after each line.
111
Make the changes above to your script and run it to confirm that it prints out the contents of the file
that gets read. Note that the file (numbers.txt) should be in the same directory that your script file
is saved in.
15. Add code to your script to call num digits on each line of ’numbers.txt’ so that instead of print-
ing each line of the file it prints out the number of digits found on each line of the file. Hint: Remember
what type of data readline returns and what type of data num digits requires.
16. Write and doctest a function add all which takes 2 integer parameters and returns the sum of all the
integers between (and including) the first and second integer e.g. add all(4,6) would return 15
(the sum 4 + 5 + 6 ). Think about what doctests might be useful.
17. Extension Exercise: Open a new script file and add the following:
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Write a function, is prime, which takes a single integer argument and returns True when the ar-
gument is a prime number and False otherwise. Add doctests to your function as you develop
it.
18. Extension Exercise: Write a function that prints all the prime numbers less than a given number:
def prime_numbers(n):
"""
>>> prime_numbers(10)
2 3 5 7
>>> prime_numbers(20)
2 3 5 7 11 13 17 19
"""
Get a demonstrator to review your work before you leave the lab.
112
Lesson 11
Iteration: part 2
In this lesson we look at nested loops. An illustrative example of nested loops is printing out two-dimensional
tables. A two-dimensional table is a table where you read the value at the intersection of a row and a col-
umn. A multiplication table is a good example. The code below prints a multiplication table that goes up
to 3 × 3:
def multiplication_table(limit):
row = 1
while row <= limit:
column = 1
while column <= limit:
print str(row * column) + "\t",
column += 1
print
row += 1
multiplication_table(3)
Nested loops can certainly be quite challenging to read (and write) but see if you can follow what is hap-
pening. Notice that we have placed a while loop inside another. The outer loop counts row from 1 through
to limit (in this case 3). Each time we enter the outer loop, the inner loop counts column from 1 through to
limit (in this case 3).
113
If we trace the program we get (each row of the following table represents a change in the output. Values
of the variables row and column are also shown):
row column total output
1 1 1
1 2 1 2
1 3 1 2 3
2 1 1 2 3
2
2 2 1 2 3
2 4
2 3 1 2 3
2 4 6
3 1 1 2 3
2 4 6
3
3 2 1 2 3
2 4 6
3 6
3 3 1 2 3
2 4 6
3 6 9
That was a lot of code to produce in one go. Let’s start over with a much more incremental and sensible
approach. Let’s say you want to print a multiplication table for the values from 1 to 6. A good way to start
is to write a loop that prints the multiples of 2, all on one line:
i = 1
while i <= 6:
print 2*i, " ",
i += 1
print
The first line initializes a variable named i, which acts as a counter or loop variable . As the loop executes,
the value of i increases from 1 to 6. When i is 7, the loop terminates. Each time through the loop, it
displays the value of 2*i, followed by three spaces. Again, the comma in the print statement suppresses
the newline. After the loop completes, the second print statement starts a new line. The output of the
program is:
2 4 6 8 10 12
Encapsulation is the process of wrapping a piece of code in a function, allowing you to take advantage of all
the things functions are good for. You have already seen two examples of encapsulation: print parity
in Lecture 5; and is divisible in Lecture 6. Generalization means taking something specific, such as
printing the multiples of 2, and making it more general, such as printing the multiples of any integer. This
function encapsulates the previous loop and generalizes it to print multiples of n:
114
def print_multiples(n):
i = 1
while i <= 6:
print n*i, "\t",
i += 1
print
To encapsulate, all we had to do was add the first line, which declares the name of the function and the
parameter list. To generalize, all we had to do was replace the value 2 with the parameter n. If we call this
function with the argument 2, we get the same output as before. With the argument 3, the output is:
3 6 9 12 15 18
4 8 12 16 20 24
By now you can probably guess how to print a multiplication table—by calling print multiples repeat-
edly with different arguments. In fact, we can use another loop:
i = 1
while i <= 6:
print_multiples(i)
i += 1
Notice how similar this loop is to the one inside print multiples. All we did was replace the print
statement with a function call. The output of this program is a multiplication table:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
This is an example of a nested loop where one loop is nested inside another. In this case, the inner loop is
defined inside a function, and that is often useful for understanding what is going on, but is not necessary.
Nested loops are discussed more below in Section 11.9.1.
def print_mult_table():
i = 1
while i <= 6:
print_multiples(i)
i += 1
This process is a common development plan. We develop code by writing lines of code outside any func-
tion, or typing them in to the interpreter. When we get the code working, we extract it and wrap it up in a
115
function. This development plan is particularly useful if you don’t know how to divide the program into
functions when you start writing. This approach lets you design as you go along.
The value of i in print mult table goes from 1 to 6. In the diagram it happens to be 3. The next time
through the loop it will be 4. Each time through the loop, print mult table calls print multiples
with the current value of i as an argument. That value gets assigned to the parameter n. Inside print multiples,
the value of i goes from 1 to 6. In the diagram, it happens to be 2. Changing this variable has no effect on
the value of i in print mult table.
It is common and perfectly legal to have different local variables with the same name. In particular, names
like i and j are used frequently as loop variables. If you avoid using them in one function just because you
used them somewhere else, you will probably make the program harder to read.
def print_mult_table(high):
i = 1
while i <= high:
print_multiples(i)
i += 1
We replaced the value 6 with the parameter high. If we call print mult table with the argument 7, it
displays:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
116
5 10 15 20 25 30
6 12 18 24 30 36
7 14 21 28 35 42
This is fine, except that we probably want the table to be square—with the same number of rows and
columns. To do that, we add another parameter to print multiples to specify how many columns the
table should have. Just to be annoying, we call this parameter high, demonstrating that different functions
can have parameters with the same name (just like local variables). Here’s the whole program:
def print_mult_table(high):
i = 1
while i <= high:
print_multiples(i, high)
i += 1
Notice that when we added a new parameter, we had to change the first line of the function (the function
heading), and we also had to change the place where the function is called in print mult table. As
expected, this program generates a square seven-by-seven table:
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49
When you generalize a function appropriately, you often get a program with capabilities you didn’t plan.
For example, you might notice that, because ab = ba, all the entries in the table appear twice. You could save
ink by printing only half the table. To do that, you only have to change one line of print mult table.
Change
print_multiples(i, high)
to
print_multiples(i, i)
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
117
7 14 21 28 35 42 49
11.6 Functions
A few times now, we have mentioned all the things functions are good for. By now, you might be wondering
what exactly those things are. Here are some of them:
1. Giving a name to a sequence of statements makes your program easier to read and debug.
2. Dividing a long program into functions allows you to separate parts of the program, debug them in
isolation, and then compose them into a whole.
3. Facilitating the use of iteration.
4. Producing reusable code; as well defined functions are often useful in many different situations.
By repeatedly applying this formula until the better approximation is equal to the previous one, we can
write a function for computing the square root:
def sqrt_newton(n):
eps = 1e-8
approx = n/2.0
better = (approx + n/approx)/2.0
while abs(better-approx) > eps:
approx = better
better = (approx + n/approx)/2.0
return approx
Try calling this function with 25 as an argument to confirm that it returns 5.0.
11.8 Algorithms
Newton’s method is an example of an algorithm : it is a mechanical process for solving a category of
problems (in this case, computing square roots). It is not easy to define an algorithm. It might help to
start with something that is not an algorithm. When you learned to multiply single-digit numbers, you
probably memorized the multiplication table. In effect, you memorized 100 specific solutions. That kind
of knowledge is not algorithmic. But if you were lazy, you probably cheated by learning a few tricks. For
1 If s is the square root of n, then s and n/s are the same, so even for a number x which isn’t the square root, the average of x and
118
example, to find the product of n and 9, you can write n − 1 as the first digit and 10 − n as the second digit.
This trick is a general solution for multiplying any single-digit number by 9. That’s an algorithm! Similarly,
the techniques you learned for addition with carrying, subtraction with borrowing, and long division are
all algorithms. One of the characteristics of algorithms is that they do not require any intelligence to carry
out. They are mechanical processes in which each step follows from the last according to a simple set of
rules.
Executing algorithms does not require any great intelligence but does require great attention to detail,
and that’s why computers are so good at it. On the other hand, the process of designing algorithms is
interesting, intellectually challenging, and a central part of what we call programming. Some of the things
that people do naturally, without difficulty or conscious thought, are the hardest to express algorithmically.
Understanding natural language is a good example. We all do it, but so far no one has been able to explain
how we do it, at least not in the form of an algorithm.
Unfortunately, there is no algorithm for designing algorithms! But there are methods that can make it easier.
Test-driven development is one of those. If you’re not sure how to solve a particular problem, try going
back to look at Section 7.4 to reboot your thinking.
Nested loops can be tricky, but the common pattern is that we need to enumerate a bunch of things, and for
each of those things, we need to enumerate a bunch of other things. For example, let’s say your grandpar-
ents want to write down all of their grandchildren. They might first list each of their children, and then for
each child, list each of their child’s children. The general pattern using a while loop looks something like
this:
# initialise outer loop condition
while <outer condition>:
# initialise inner loop condition
while <inner condition>:
# do some processing
# update variables related to <inner condition>
# update variables related to <outer condition>
Sometimes it easier to understand if the inner while loop is put into a function as we did above.
For example, if we want to find all factors of all the numbers from 1 to 100. First we decide how to find
all the factors of one number (this is the inner loop). The easiest way is to check all the numbers up to the
number of interest. This whole thing needs to be repeated for all the numbers up to 100 (the outer loop).
We end up with:
number = 1
while number <= 100: # check all numbers from 1 to 100
possible_factor = 1
print number, ":",
while possible_factor<number: # check all numbers up to number
if number % possible_factor == 0: # we've found a factor
print possible_factor,
possible_factor += 1 # update the condition variable
print
number += 1
119
Can you generalise this code to find only prime factors?
11.9.2 Counting
We’ve seen some counting patterns before and these keep coming up over and over again. The general
pattern for while loops is:
count = 0
while <condition>:
count = count + 1
# update variables associated with condition
For example, suppose you wanted a function to compute the number of years it would take to reach a
certain target if you invested some base amount of money into an account that compounded interest each
year. That function might look like:
11.9.3 Convergence
Newton’s sqrt algorithm is a good example of a convergence pattern. Convergence patterns occur very
frequently in numerical algorithms. The basic pattern is:
The trick is finding an update rule that brings you closer to the actual solution.
11.10 Glossary
tab: A special character that causes the cursor to move to the next tab stop on the current line.
newline: A special character that causes the cursor to move to the beginning of the next line.
120
cursor: An invisible marker that keeps track of where the next character will be printed.
escape sequence: An escape character, \, followed by one or more printable characters used to designate a
non-printable character.
encapsulate: To divide a large complex program into components (like functions) and isolate the compo-
nents from each other (by using local variables, for example).
generalize: To replace something unnecessarily specific (like a constant value) with something appropri-
ately general (like a variable or parameter). Generalization makes code more versatile, more likely to
be reused, and sometimes even easier to write.
development plan: A process for developing a program. In this chapter, we demonstrated a style of devel-
opment based on developing code to do simple, specific things and then encapsulating and general-
izing.
algorithm: A step-by-step process for solving a category of problems.
121
11.11 Laboratory Exercises
1. Write a program that prompts the user for some target string (e.g. “daffodils”), and a filename (e.g.
“mary.txt”). Open the file and read the contents using readlines (make sure the file exists - either
use an existing file or create a new one using a text editor). Print out the lines that match the target
string and count how many lines match.
Modularise your code, so that the search string is an input to a function.
2. Make a text file containing integer values. Create a program that: reads the contents of the file using
the read method; creates a list of all the numbers using the split method; uses a loop to compute
the sum of all the integers in the file.
Show your work to a demonstrator here to make sure you are on the right track.
3. (a) Create a function, called print powers, that takes two parameters, n and high, and prints the
values of n1 , n2 , · · · , nhigh on a single line.
(b) Create another function, called print powers table, that takes a single parameter, high,
and prints on the first line the result of 11 , 12 , · · · , 1high , then on the second line the result of
21 , 22 , · · · , 2high , and so on until the last line is printed as high1 , high2 , · · · , highhigh .
4. (a) Write a function, is prime, which takes a single integer argument, n, and returns True when
the argument is a prime number and False otherwise. Add doctests to your function as you
develop it. Use doctests to test your function. Hint: A simple method is to loop through all the
numbers from 2 to n/2 and check if any of the numbers divide n evenly (i.e. no remainder).
(b) Write a function that finds all the prime numbers less than a given number. Use doctests to test
your function.
5. Extension Exercise: Write a spell-checking program that reads in a text file, scans through every word
in the file and checks that it is spelled correctly. To do this you will probably also want to read in the
system dictionary: /usr/share/dict/words.
Get a demonstrator to review your work before you leave the lab.
122
Lesson 12
Strings part 2
The most concise and powerful way to format a string in Python is to use the string formatting method,
str.format. To see how this works, let’s start with a few examples:
The syntax for the string formatting operation looks like this:
"<FORMAT>".format(<VALUES>)
It begins with a format string which contains a sequence of characters and format fields. A format field is
enclosed in {}. The format method call contains a sequence of values, one per format field. The format field
may be empty, in which case a default conversion from the value type passed in to a string is performed (as
has happened in all the examples above)1 .
Format fields can seem quite complicated, but mostly they are very simple. We can optionally specify a
field number indicating which argument of the format method is to be placed where or we can use field
name arguments instead of using field numbers:
1 This is a change from Python 2.6 which requires that a field be numbered or named.
123
>>> n1 = 4
>>> n2 = 5
>>> "2**10 = {0} and {1} * {2} = {3}".format(2**10, n1, n2, n1*n2)
’2**10 = 1024 and 4 * 5 = 20’
>>> "2**10 = {0} and {2} * {1} = {3}".format(2**10, n1, n2, n1*n2)
’2**10 = 1024 and 5 * 4 = 20’
>>> "{first} * {second} = {product}".format(first=n1, second=n2, product=n1*n2)
’4 * 5 = 20’
Again, this looks daunting, but is actually straightforward once you understand the notation used. Here is
a brief description of some of the elements of the format specification:
To see a complete list, see the String Formatting Operations section of the Python Library Reference. Some
examples should illustrate the utility of string formatting. First, here is an attempt to line up columns with
using tab spaces.
i = 1
print "i\ti**2\ti**3\ti**5\ti**10\ti**20"
while i <= 10:
print i, "\t", i**2, "\t", i**3, "\t", i**5, "\t", i**10, "\t", i**20
i += 1
This program prints out a table of various powers of the numbers from 1 to 10. In its current form it relies
on the tab character (\t) to align the columns of values, but this breaks down when the values in the table
get larger than the 8 character tab width:
124
i i**2 i**3 i**5 i**10 i**20
1 1 1 1 1 1
2 4 8 32 1024 1048576
3 9 27 243 59049 3486784401
4 16 64 1024 1048576 1099511627776
5 25 125 3125 9765625 95367431640625
6 36 216 7776 60466176 3656158440062976
7 49 343 16807 282475249 79792266297612001
8 64 512 32768 1073741824 1152921504606846976
9 81 729 59049 3486784401 12157665459056928801
10 100 1000 100000 10000000000 100000000000000000000
One possible solution would be to change the tab width, but the first column already has more space than
it needs. The best solution would be to set the width of each column independently. As you may have
guessed by now, string formatting provides the solution:
i = 1
s = "{:ˆ4} {:ˆ5} {:ˆ6} {:ˆ8} {:ˆ13} {:ˆ21}"
print s.format("i", "i**2", "i**3", "i**5", "i**10", "i**20")
while i <= 10:
s = "{:<4} {:ˆ5} {:>6} {:<8} {:<13} {:ˆ21}"
print s.format(i, i**2, i**3, i**5, i**10, i**20)
i += 1
125
>>> x = 12.342354453463456456
>>> "{:5.1f}".format(x)
’ 12.3’
>>> "{:5.5f}".format(x)
’12.34235’
>>> "{:05.1f}".format(x)
’012.3’
>>> "{:5.1e}".format(x)
’1.2e+01’
>>> "{:5.1E}".format(x)
’1.2E+01’
>>> "{:015.1E}".format(x)
’000000001.2E+01’
>>> "{:>15.1E}".format(x)
’ 1.2E+01’
It is often helpful to examine a character or string and test whether it is upper- or lowercase, or whether it
is a character or a digit. The str type includes several methods for this functionality:
These methods are often very useful for checking for the correct format when reading in information from
a user. For example, when asking for someone’s name, it is often useful to check for capitilization:
126
12.3 A find function
In a sense, find is the opposite of the [] operator. Instead of taking an index and extracting the corre-
sponding character, it takes a character and finds the index where that character appears. If the character
is not found, the function returns -1. This is the first example we have seen of a return statement inside
a loop. If strng[index] == ch, the function returns immediately, breaking out of the loop prematurely.
If the character doesn’t appear in the string, then the program exits the loop normally and returns -1. This
pattern of computation is sometimes called a eureka traversal because as soon as we find what we are
looking for, we can cry Eureka! and stop looking.
To find the locations of the second or third occurrence of a character in a string, we can modify the find
function, adding a third parameter for the starting position in the search string:
The call find2("banana", "a", 2) now returns 3, the index of the second ”a” in ”banana”. Better still,
we can combine find and find2 using an optional parameter:
The call find("banana", "a", 2) to this version of find behaves just like find2, while in the call
find("banana", "a"), start will be set to the default value of 0. Adding another optional parameter
to find makes it search both forward and backward:
127
def find(strng, ch, start=0, step=1):
index = start
while 0 <= index < len(strng):
if strng[index] == ch:
return index
index += step
return -1
Passing in a value of -1 for step will make it search toward the beginning of the string instead of the end.
Note that we needed to check for a lower bound for index in the while loop as well as an upper bound to
accommodate this change.
12.5.1 Find
The find functions above are examples of the find pattern. They are common whenever we have a se-
quence of data from which we want to find something. A significant part of computer science involves
algorithms for efficiently finding things (this find function is not very efficient)! There are several variants
to the general pattern. Here is a slightly different variant to those above:
In the above function, data could be any sequence type (string, list, tuple).
12.6 Glossary
whitespace: Any of the characters that move the cursor without printing visible characters. The constant
string.whitespace contains all the white-space characters.
optional parameter: A parameter written in a function header with an assignment to a default value which
it will receive if no corresponding argument is given for it in the function call.
default value: The value given to an optional parameter if no argument for it is provided in the function
call.
128
12.7 Laboratory exercises
1. Try each of the following formatted string operations in a Python shell and record the results:
(a) "{} {:d} {:f}".format(5, 5, 5)
(b) "{:>.2f}".format(3)
3. Write a function that will convert all uppercase characters to lowercase and all lowercase to uppercase.
The function should pass the following doctests:
def upper_lower_swap(string):
"""
>>> upper_lower_swap("The rain in Spain")
'tHE RAIN IN sPAIN'
>>> upper_lower_swap("LaTeX is better than MSWord")
'lAtEx IS BETTER THAN mswORD'
"""
4. Write a function count apluses(filename) which reads lines from a file of numbers and returns
how many were in the range 90 to 100. Create your own text files to test your code.
Show your work to a demonstrator here to make sure you are on the right track.
5. (a) A bigram is a sequence of any two characters (for example, ’ch’). Write a function that will
count the number of times a particular bigram appears in a given string. This is an extension
of the count letters function from Exercise 5 in Section 8.12. The function should pass the
following doctests:
129
(b) Repeat the previous exercise, but this time for trigrams.
(c) Think about how you could generalise the previous two functions to a count ngrams function,
where n is the number of characters in the search string passed in. You do not have to implement
it, but it is worthwhile thinking about how you could do it.
6. Extension Exercise: Write a function sum of squares of digits that computes the sum of the squares of
the digits of an integer passed to it. For example, sum of squares of digits(987) should return 194,
since 9**2 + 8**2 + 7**2 == 81 + 64 + 49 == 194.
def sum_of_squares_of_digits(n):
"""
>>> sum_of_squares_of_digits(1)
1
>>> sum_of_squares_of_digits(9)
81
>>> sum_of_squares_of_digits(11)
2
>>> sum_of_squares_of_digits(121)
6
>>> sum_of_squares_of_digits(987)
194
"""
130
7. Extension Exercise: Create useful utilities for dealing with filenames:
(a) Remove the trailing filename extension from a string:
def remove_extension(name):
"""
>>> remove_extension("tst.txt")
'tst'
>>> remove_extension("tst.2.txt")
'tst.2'
"""
(b) Remove all leading pathname components, leaving only the extension:
def get_extension(name):
"""
>>> get_extension("tst.txt")
'txt'
>>> get_extension("/usr/dict/words.txt")
'txt'
>>> get_extension("tst.3.png")
'png'
"""
(c) Strip out the leading pathname components, leaving only the filename:
def strip_pathname(name):
"""
>>> strip_pathname("/usr/dict/words")
'words'
>>> strip_pathname("tst.txt")
'tst.txt'
"""
Get a demonstrator to review your work before you leave the lab.
131
132
Lesson 13
Lists part 1
A list is an ordered set of values, where each value is identified by an index. The values that make up a
list are called its elements . Lists are similar to strings, which are ordered sets of characters, except that the
elements of a list can have any type. Lists and strings—and other things that behave like ordered sets—are
called sequences .
There are several ways to create a new list; the simplest is to enclose the elements in square brackets
([ and ]):
The first example is a list of four integers. The second is a list of three strings. The elements of a list don’t
have to be the same type. The following list contains a string, a float, an integer, and another list:
A list within another list is said to be nested . Finally, there is a special list that contains no elements. It is
called the empty list, and is denoted []. Like numeric 0 values and the empty string, the empty list is false
in a boolean expression:
>>> if []:
... print "This is true."
... else:
... print "This is false."
...
This is false.
>>>
With all these ways to create lists, it would be disappointing if we couldn’t assign list values to variables or
pass lists as parameters to functions. We can:
133
>>> vocabulary = ["ameliorate", "castigate", "defenestrate"]
>>> numbers = [17, 5]
>>> empty = []
>>> print vocabulary, numbers, empty
[’ameliorate’, ’castigate’, ’defenestrate’] [17, 5] []
The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of
a string—the bracket operator ([] – not to be confused with an empty list). The expression inside the
brackets specifies the index. Remember that the indices start at 0:
>>> numbers[9-8]
5
>>> numbers[1.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
If you try to read or write an element that does not exist, you get a runtime error:
>>> numbers[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
If an index has a negative value, it counts backward from the end of the list:
>>> numbers[-1]
5
>>> numbers[-2]
17
>>> numbers[-3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
numbers[-1] is the last element of the list, numbers[-2] is the second to last, and numbers[-3] doesn’t
exist. It is common to use a loop variable as a list index.
134
horsemen = ["war", "famine", "pestilence", "death"]
i = 0
while i < 4:
print horsemen[i]
i += 1
This while loop counts from 0 to 4. When the loop variable i is 4, the condition fails and the loop terminates.
The body of the loop is only executed when i is 0, 1, 2, and 3. Each time through the loop, the variable i is
used as an index into the list, printing the ith element. This pattern of computation is called a list traversal.
i = 0
num = len(horsemen)
while i < num:
print horsemen[i]
i += 1
The last time the body of the loop is executed, i is len(horsemen) - 1, which is the index of the last
element. When i is equal to len(horsemen), the condition fails and the body is not executed, which is a
good thing, because len(horsemen) is not a legal index.
Although a list can contain another list, the nested list still counts as a single element. The length of this list
is 4:
["spam!", 1, ["Brie", "Roquefort", "Pol le Veq"], [1, 2, 3]]
>>> range(1, 5)
[1, 2, 3, 4]
The range function takes two arguments and returns a list that contains all the integers from the first to the
second, including the first but not the second. 1 There are two other forms of range. With a single argument,
it creates a list that starts at 0:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
If there is a third argument, it specifies the space between successive values, which is called the step size.
1 There is also a function called xrange that acts much like range. However, xrange does not actually construct the requested
list in memory, and will generate the numbers as needed. This is more memory efficient, especially when generating a lot of numbers.
135
This example counts from 1 to 10 by steps of 2:
If the step size is negative, then start must be greater than stop:
i = 0
while i < len(LIST):
VARIABLE = LIST[i]
BODY
i += 1
The for loop is more concise because we can eliminate the loop variable, i. Here is the horsemen while
loop written with a for loop:
It almost reads like English: For (every) horseman in (the list of) horsemen, print (the name of the)
horseman.
Any list expression can be used in a for loop:
The first example prints all the multiples of 3 between 0 and 19. The second example expresses enthusiasm
for various fruits.
Since lists are mutable, it is often desirable to traverse a list, modifying each of its elements. The following
squares all the numbers from 1 to 5:
136
numbers = [1, 2, 3, 4, 5]
Take a moment to think about range(len(numbers)) until you understand how it works. We are inter-
ested here in both the value and its index within the list, so that we can assign a new value to it.
This pattern is common enough that Python provides a nicer way to implement it:
numbers = [1, 2, 3, 4, 5]
enumerate generates both the index and the value associated with it during the list traversal. Try this
next example to see more clearly how enumerate works:
Since pestilence is a member of the horsemen list, the in operator returns True. Since debauchery is
not in the list, in returns False. We can use not in combination with in to test whether an element is not
a member of a list:
>>> "debauchery" not in horsemen
True
137
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times.
Unlike strings lists are mutable , which means we can change their elements. Using the bracket operator
on the left side of an assignment, we can update one of the elements:
The bracket operator applied to a list can appear anywhere in an expression. When it appears on the left
side of an assignment, it changes one of the elements in the list, so the first element of fruit has been
changed from "banana" to "pear", and the last from "quince" to "orange". An assignment to an
element of a list is called item assignment.
138
Item assignment does not work for strings:
We can also remove elements from a list by assigning the empty list to them:
And we can add elements to a list by squeezing them into an empty slice at the desired location:
Using slices to delete list elements can be awkward, and therefore error-prone. Python provides an alterna-
tive that is more readable. del removes an element from a list:
>>> a = ["one", "two", "three"]
>>> del a[1]
>>> a
[’one’, ’three’]
As you might expect, del handles negative indices and causes a runtime error if the index is out of range.
139
You can use a slice as an index for del:
As usual, slices select all the elements up to, but not including, the second index.
13.11 Glossary
list: A named collection of objects, where each object is identified by an index.
index: An integer variable or value that indicates an element of a list.
element: One of the values in a list (or other sequence). The bracket operator selects elements of a list.
sequence: Any of the data types that consist of an ordered set of elements, with each element identified by
an index.
nested list: A list that is an element of another list.
step size: The interval between successive elements of a linear sequence. The third (and optional argu-
ment) to the range function is called the step size. If not specified, it defaults to 1.
140
13.12 Laboratory exercises
1. (a) Write a function that takes a list as an argument and returns the 5th element if it exists and ”No
such element” if it doesn’t.
(b) Write a function that takes a list as an argument and returns the last two elements of the list.
(c) Write a function that takes a list as an argument and returns every second element of the list.
Note: the returned value should also be a list.
The three arguments to the range function are start, stop, and step, respectively. In this example,
start is greater than stop. What happens if start < stop and step < 0? Write a rule for the
relationships among start, stop, and step.
Show your work to a demonstrator here to make sure you are on the right track.
4. Write a function add lists(a, b) that takes two lists of numbers of the same length, and returns a new
list containing the sums of the corresponding elements of each. The original lists should remain unchanged.
141
5. Write a function mult lists(a, b) that takes two lists of numbers of the same length, and returns the
sum of the products of the corresponding elements of each.
def flatten_list(alist):
'''
>>> flatten_list([1,2,3])
[1, 2, 3]
>>> flatten_list([1, [2,3], [4, 5], 6])
[1, 2, 3, 4, 5, 6]
'''
Is your solution general enough to allow any level of nesting? If not, can you make it so? Hint: you
can use the type function to check if some variable is of a particular type. E.g. if type(el) is
list:.
Get a demonstrator to review your work before you leave the lab.
142
Lesson 14
Lists part 2
a = "banana"
b = "banana"
we know that a and b will refer to a string with the letters ‘‘banana’’. But we can’t tell whether they
point to the same string. There are two possible states:
a "banana" a
"banana"
b "banana" b
In one case, a and b refer to two different things that have the same value. In the second case, they refer to
the same thing. These things have names—they are called objects. An object is something a variable can
refer to. Every object has a unique identifier, which we can obtain with the id function. By printing the
identifier of a and b, we can tell whether they refer to the same object.
>>> id(a)
135044008
>>> id(b)
135044008
In fact, we get the same identifier twice, which means that Python only created one string, and both a and b
refer to it. If you try this, your actual id value will probably be different. Python does this because strings
are immutable and it is more space efficient to maintain only one copy of a string in memory.
Interestingly, lists behave differently. When we create two lists, we get two objects:
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> id(a)
135045528
>>> id(b)
135041704
143
So the state diagram looks like this:
a [ 1, 2, 3 ]
b [ 1, 2, 3 ]
a and b have the same value but do not refer to the same object.
14.2 Aliasing
Since variables refer to objects, if we assign one variable to another, both variables refer to the same object:
>>> a = [1, 2, 3]
>>> b = a
>>> id(a) == id(b)
True
a
[ 1, 2, 3 ]
b
Because the same list has two different names, a and b, we say that it is aliased. Changes made with one
alias affect the other:
>>> b[0] = 5
>>> print a
[5, 2, 3]
Although this behaviour can be useful, it is sometimes unexpected or undesirable. In general, it is safer to
avoid aliasing when you are working with mutable objects. Of course, for immutable objects, there’s no
problem. That’s why Python is free to alias strings when it sees an opportunity to economize.
>>> a = [1, 2, 3]
>>> b = a[:]
>>> print b
[1, 2, 3]
Taking any slice of a creates a new list. In this case the slice happens to consist of the whole list. Now we
are free to make changes to b without worrying about a:
>>> b[0] = 5
>>> print a
[1, 2, 3]
144
14.4 List parameters
Passing a list as an argument actually passes a reference to the list, not a copy of the list. Since lists are
mutable, changes made to the parameter change the argument as well. For example, the function below
takes a list as an argument and multiplies each element in the list by 2:
def double_stuff(a_list):
for index, value in enumerate(a_list):
a_list[index] = 2 * value
If we put double stuff in a file named double stuff.py, we can test it out like this:
The parameter a list and the variable things are aliases for the same object. The state diagram looks like
this:
double_stuff a_list
[2, 5, ’Spam’, 9.5]
__main__ things
Since the list object is shared by two frames, we drew it between them. If a function modifies a list parame-
ter, the caller sees the change.
Functions which take lists as arguments and change them during execution are called modifiers and the
changes they make are called side effects. A pure function does not produce side effects. It communicates
with the calling program only through parameters, which it does not modify, and a return value. Here is
double stuff written as a pure function:
def double_stuff(a_list):
new_list = []
for value in a_list:
new_list += [2 * value]
return new_list
145
This version of double stuff does not change its arguments:
To use the pure function version of double stuff to modify things, you would assign the return value
back to things:
If we print nested[3], we get [10, 20]. To extract an element from the nested list, we can proceed in
two steps:
>>> nested[3][1]
20
Bracket operators evaluate from left to right, so this expression gets the third element of nested and ex-
tracts the first element from it.
146
14.8 Matrices
Nested lists are often used to represent matrices. For example, the matrix:
1 2 3
4 5 6
7 8 9
matrix is a list with three elements, where each element is a row of the matrix. We can select an entire row
from the matrix in the usual way:
>>> matrix[1]
[4, 5, 6]
Or we can extract a single element from the matrix using the double-index form:
>>> matrix[1][1]
5
The first index selects the row, and the second index selects the column. Although this way of representing
matrices is common, it is not the only possibility. A small variation is to use a list of columns instead of a
list of rows.
Python has a command called list that takes a sequence type as an argument and creates a list out of its
elements.
>>> list("Crunchy Frog")
[’C’, ’r’, ’u’, ’n’, ’c’, ’h’, ’y’, ’ ’, ’F’, ’r’, ’o’, ’g’]
There is also a str command that takes any Python value as an argument and returns a string representa-
tion of it.
>>> str(5)
’5’
>>> str(None)
’None’
>>> str(list("nope"))
"[’n’, ’o’, ’p’, ’e’]"
147
As we can see from the last example, str can’t be used to join a list of characters together. To do this we
could use the string method join:
To go in the opposite direction, the split method breaks a string into a list of words. By default, any
number of whitespace characters is considered a word boundary:
An optional argument called a delimiter can be used to specify which characters to use as word boundaries.
The following example uses the string ai as the delimiter (which doesn’t appear in the result):
>>> song.split("ai")
[’The r’, ’n in Sp’, ’n...’]
14.10 Glossary
modifier: A function which changes its arguments inside the function body. Only mutable types can be
changed by modifiers.
side effect: A change in the state of a program made by calling a function that is not a result of reading the
return value from the function. Side effects can only be produced by modifiers.
pure function: A function which has no side effects. Pure functions only make changes to the calling
program through their return values.
delimiter: A character or string used to indicate where a string should be split.
aliases: Multiple variables that contain references to the same object.
clone: To create a new object that has the same value as an existing object. Copying a reference to an object
creates an alias but doesn’t clone the object.
148
14.11 Laboratory exercises
a = [1, 2, 3]
b = a[:]
b[0] = 5
Draw a state diagram for a and b before and after the third line is executed.
2.
song = "The rain in Spain..."
Describe the relationship between " ".join(song.split()) and song. Are they the same for all
strings? When would they be different?
4. Exercise 4 in Section 13.12 asked you to write a function to add two lists without modifying the original
lists. Suppose you had written the following function (with a little bit of test code):
list1 = [1,2,3]
list2 = [3,4,6]
list3 = add_lists(list1, list2)
print list1
print list2
print list3
What is the output of the print statements and why? This is an example of a side-effect. How can we write
our code to avoid side-effects?
149
Show your work to a demonstrator here to make sure you are on the right track.
5. A list contains pairs of items. The first of every pair is a string representing a surname / family name.
The second item is a list containing one or more first names. Write a function which takes a list of this
description as a parameter, and prints out all the full names. Here are some doctests:
def print_names(name_list):
"""
>>> print_names(["Brown",["Bob","Betty"],"Green",["Geoff","Gemma","Gail"]])
Bob Brown
Betty Brown
Geoff Green
Gemma Green
Gail Green
>>> print_names(["Carter",["Cathy"],"Downer",["Don"]])
Cathy Carter
Don Downer
"""
if __name__=="__main__":
import doctest
doctest.testmod(verbose=True)
6. A list contains pairs of items. The first of every pair is a string representing a surname / family name.
The second item is a list containing one or more first names. Write a function which takes a list of this
description as a parameter, and returns a list of full names. Here are some doctests:
def return_names(name_list):
"""
>>> return_names(["Brown",["Bob","Betty"],"Green",["Geoff","Gemma","Gail"]])
['Bob Brown', 'Betty Brown', 'Geoff Green', 'Gemma Green', 'Gail Green']
>>> return_names(["Carter",["Cathy"],"Downer",["Don"]])
['Cathy Carter', 'Don Downer']
"""
if __name__=="__main__":
import doctest
doctest.testmod(verbose=True)
7. Write a function myreplace(s, old, new) that returns a string with all occurrences of old replaced
with new in the string s. Assume that old has only one character:
Your solution should pass the doctests above, but don’t use the existing string replace method (make
use of loops instead).
150
8. Redo the previous exercise, but this time assume that old has two characters.
Your solution should pass the doctests above (again use loops). Instead of trying to solve the whole
problem, use incremental development with the following intermediate goals:
(b) loop through s and print all substrings that match old.
(c) loop through s and print all substrings of size 2, except replace those substrings that match old
with the string new.
9. Redo the previous exercise, except this time make no assumptions about the length of old.
10. Develop a function which returns a 0-filled matrix of given size. The function should pass the following
doctests:
def zeros(n,m):
"""
Return an n by m matrix of zeros. Each element should be unique.
>>> A = zeros(3,2)
>>> A
[[0, 0], [0, 0], [0, 0]]
>>> A[0][1] = 1
>>> A
[[0, 1], [0, 0], [0, 0]]
"""
11. Extension Exercise: Create a function called insert sorted that inserts a new element into a list in
order:
151
12. Extension Exercise Matrix multiplication. If you don’t know how to do matrix multiplication, spend some
time learning how to do it (the web is a good place to start - try
http://www.intmath.com/matrices-determinants/4-multiplying-matrices.php or
http://www.youtube.com/watch?v=qiLjdujcI2w&feature=related). Complete the body of the
matrix multiply 2x2 function below, so that it passes the doctests.
13. Extension Exercise Extend your solution to exercise 12 so that matrix multiply can accept matrices of any
size (assuming they are compatible). You will need three nested loops.
[1 4]
The matrix [2 5] is represented by this list [[1,4],[2,5],[3,6]]
[3 6]
>>> matrix_multiply([[1,2]],[[3],[4]])
[[11]]
>>> matrix_multiply([[1],[2]], [[3,4]])
[[3, 4], [6, 8]]
>>> matrix_multiply([[1,2,3],[4,5,6]], [[1,4],[2,5],[3,6]])
[[14, 32], [32, 77]]
>>> matrix_multiply([[1,2,3],[4,5,6]], [[1,4,7,10],[2,5,8,11],[3,6,9,12]])
[[14, 32, 50, 68], [32, 77, 122, 167]]
"""
14. Extension Exercise: Dealing with dates is a common problem with web-form processing (how many
times have you had to enter the date in a web form?). Read a date in from the user and convert it to
the standard date format: DD/MM/YYYY.
Users are likely to enter the date using several formats: E.g.
12/6/07
13-2-09
13 May 1996
152
This is quite a difficult problem. Start off with some of the simpler conversions, then add more as you
go. Think about what you should do if the user enters an invalid date.
Get a demonstrator to review your work before you leave the lab.
153
154
Lesson 15
>>> tup = 2, 4, 6, 8, 10
To create a tuple with a single element, we have to include the final comma:
Syntax issues aside, tuples support the same sequence operations as strings and lists. The index operator
selects an element from a tuple.
155
And the slice operator selects a range of elements:
>>> tup[1:3]
(’b’, ’c’)
But if we try to use item assignment to modify one of the elements of the tuple, we get an error:
Of course, even if we can’t modify the elements of a tuple, we can replace it with a different tuple:
Alternatively, we could first convert it to a list, modify it, and convert it back into a tuple:
The left side is a tuple of variables; the right side is a tuple of values. Each value is assigned to its respective
variable. All the expressions on the right side are evaluated before any of the assignments. This feature
makes tuple assignment quite versatile. Naturally, the number of variables on the left and the number of
values on the right have to be the same:
>>> a, b, c, d = 1, 2, 3
ValueError: need more than 3 values to unpack
156
Such statements can be useful shorthand for multiple assignment statements, but care should be taken that
it doesn’t make the code more difficult to read.
One example of tuple assignment that improves readibility is when we want to swap the values of two
variables. With conventional assignment statements, we have to use a temporary variable. For example, to
swap a and b:
temp = a
a = b
b = temp
If we have to do this often, such an approach becomes cumbersome. Python provides a form of tuple
assignment that solves this problem neatly:
a, b = b, a
Then we can assign the return value to a tuple with two variables:
a, b = swap(a, b)
In this case, there is no great advantage in making swap a function. In fact, there is a danger in trying to
encapsulate swap, which is the following tempting mistake:
swap(a, b)
then a and x are aliases for the same value. Changing x inside swap makes x refer to a different value,
but it has no effect on a in main . Similarly, changing y has no effect on b. This function runs without
producing an error message, but it doesn’t do what we intended. This is an example of a semantic error.
157
• Tuples are more efficient to use than lists. Admittedly, the difference is likely to be small for most
applications, but in some cases can be significant.
• Tuple assignment is just cool, since many programming languages won’t allow you to do this (note
the swap syntax above).
• In some situations, tuples can be used as dictionary keys (more on dictionaries in section 15.7).
• the tuple represents a value. For example, if you want to represent a point in two-dimensions, then
a tuple is probably what you want. The tuple (2,5) represents the point (2, 5) and that point will
never change its value (it’s a constant). This is similar to the notion of integers — you wouldn’t want
to change the value of the integer 2 to represent anything other than the number 2;
15.6 Sets
Strings, lists and tuples are sequence types which use integers as indices to access the multiple values
they contain. Another useful builtin type is the set. A set is an unordered collection of items with no
duplication. In this context, unordered simply means that sets are not indexed, but the set can be checked
for membership. Set objects also support basic mathematical set operations such as union, intersection
and difference. Sets can be constructed from any sequence type (string, list, tuple). Following is a quick
demonstration of sets:
158
>>> a = set("hello there")
>>> b = set("goodbye")
>>> a
set([’ ’, ’e’, ’h’, ’l’, ’o’, ’r’, ’t’])
>>> b
set([’b’, ’e’, ’d’, ’g’, ’o’, ’y’])
>>> len(a)
7
>>> len(b)
6
>>> "b" in a # set membership
False
>>> "b" in b
True
>>> a - b # characters in a that aren’t in b
set([’ ’, ’t’, ’r’, ’l’, ’h’])
>>> a | b # characters in either a or b
set([’ ’, ’b’, ’e’, ’d’, ’g’, ’h’, ’l’, ’o’, ’r’, ’t’, ’y’])
>>> a & b # characters in both a and b
set([’e’, ’o’])
>>> a ˆ b # characters in a or b but not in both
set([’ ’, ’b’, ’d’, ’g’, ’h’, ’l’, ’r’, ’t’, ’y’])
>>> set([3,2,2,3,1,4]) # no duplicates in a set
set([1, 2, 3, 4])
15.7 Dictionaries
Dictionaries have behaviours that are somewhere between the behaviour of sets and sequence types. On
the one hand, they can be indexed, but on the other hand they are unordered (the indicies need not be
consecutive. They are Python’s mapping type. They map keys which can be of any immutable type to
values which can be of any type.
As an example, we will create a dictionary to translate English words into Spanish. For this dictionary, the
keys are strings. One way to create a dictionary is to start with the empty dictionary and add key-value
pairs. The empty dictionary is denoted {}:
>>> eng2sp = {}
>>> eng2sp["one"] = "uno"
>>> eng2sp["two"] = "dos"
The first assignment creates a dictionary named eng2sp; the other assignments add new key-value pairs
to the dictionary. We can print the current value of the dictionary in the usual way:
The key-value pairs of the dictionary are separated by commas. Each pair contains a key and a value sep-
arated by a colon. The order of the pairs may not be what you expected. Python uses complex algorithms
to determine where the key-value pairs are stored in a dictionary. For our purposes we can think of this
ordering as unpredictable.
Another way to create a dictionary is to provide a list of key-value pairs using the same syntax as the
previous output:
159
>>> eng2sp = {"one": "uno", "two": "dos", "three": "tres"}
It doesn”t matter what order we write the pairs. The values in a dictionary are accessed with keys, not with
indices, so there is no need to care about ordering. Here is how we use a key to look up the corresponding
value:
>>> print eng2sp["two"]
’dos’
If someone buys all of the pears, we can remove the entry from the dictionary:
Or if we’re expecting more pears soon, we might just change the value associated with pears:
>>> inventory["pears"] = 0
>>> print inventory
{’oranges’: 525, ’apples’: 430, ’pears’: 0, ’bananas’: 312}
The len function also works on dictionaries; it returns the number of key-value pairs:
>>> len(inventory)
4
>>> eng2sp.keys()
[’one’, ’three’, ’two’]
This form of dot notation specifies the name of the method, keys, and the name of the object on which
to apply the method, eng2sp. The parentheses indicate that this method takes no parameters. A method
call is called an invocation; in this case, we would say that we are invoking the keys method on the object
eng2sp.
160
The values method is similar; it returns a list of the values in the dictionary:
>>> eng2sp.values()
[’uno’, ’tres’, ’dos’]
The items method returns both, in the form of a list of tuples—one for each key-value pair:
>>> eng2sp.items()
[(’one’,’uno’), (’three’, ’tres’), (’two’, ’dos’)]
The syntax provides useful type information. The square brackets indicate that this is a list. The parentheses
indicate that the elements of the list are tuples.
If a method takes an argument, it uses the same syntax as a function call. For example, the method has key
takes a key and returns true if the key appears in the dictionary:
>>> eng2sp.has_key("one")
True
>>> eng2sp.has_key("deux")
False
If you try to call a method without specifying an object, you get an error. In this case, the error message is
not very helpful:
>>> has_key("one")
NameError: name ’has_key’ is not defined
Because dictionaries are mutable, you need to be aware of aliasing. Whenever two variables refer to the
same object, changes to one affect the other. If you want to modify a dictionary and keep a copy of the
original, use the copy method. For example, opposites is a dictionary that contains pairs of opposites:
alias and opposites refer to the same object; copy refers to a fresh copy of the same dictionary. If we
modify alias, opposites is also changed:
161
opposites {"up": "down", "right": "wrong", "true": "false"}
alias
alias
In Lecture 8, we wrote a function that counted the number of occurrences of a letter in a string. A more
general version of this problem is to form a histogram of the letters in the string, that is, how many times
each letter appears. Such a histogram might be useful for compressing a text file. Because different letters
appear with different frequencies, we can compress a file by using shorter codes for common letters and
longer codes for letters that appear less frequently.
Dictionaries provide an elegant way to generate a histogram:
>>> letter_counts = {}
>>> for letter in "Mississippi":
... letter_counts[letter] = letter_counts.get(letter, 0) + 1
...
>>> letter_counts
{’M’: 1, ’s’: 4, ’p’: 2, ’i’: 4}
We start with an empty dictionary. For each letter in the string, we find the current count (possibly zero)
using the get method and then increment it. The get method for dictionaries will check to see if its first
argument is a key in the dictionary. If it is, then get will return the value associated with that key. If not,
then get will return its second argument (in this case, 0). After the for loop, the dictionary contains pairs
of letters and their frequencies. It might be more appealing to display the histogram in alphabetical order.
We can do that with the items method which returns a list, and then sort the resulting list:
The sort method is one of several that can be applied to lists, others include append, extend, and
reverse. Consult the Python documentation for details.
162
15.12 Glossary
immutable A value that cannot be changed.
x, y = 1, 2
dictionary: A collection of key-value pairs that maps from keys to values. The keys can be any immutable
type, and the values can be any type.
key: A value that is used to look up an entry in a dictionary. Keys must be of an immutable type (e.g.
integer, float, string, tuple).
key-value pair: One of the items in a dictionary.
invoke: To call a method.
163
15.13 Laboratory exercises
1. Here are some tuples representing people using the Owheo lab, using their first and last names.
brendan = ("Brendan","McCane")
sandy = ("Sandy","Garner")
nick = ("Nick","Meek")
student1 = ("Allan", "Anderson")
student2 = ("Barry", "Byars")
student3 = ("Christine", "Carver")
student4 = ("Delia","de Wattinger")
(a) Put the tuples in a list, then use a for loop to print just each last name.
(b) Allocate all the names to a set variable called owheo.
(c) Allocate the first 3 tuples to a set variable called teachers.
(d) Allocate the remainder to a set variable called students.
(e) Use a for loop to print each student’s name, properly formatted e.g. Allan Anderson
(f) Use a for loop to print each teacher’s name just using the first initial of the first name e.g. B.
McCane
2. Write a function that takes a list of items and returns the number of unique items in the list. The
function should pass the following doctests:
def num_unique(a_list):
"""
>>> num_unique([1, 2])
2
>>> num_unique([1, 2, 2, 2, 1, 2, 1, 2, 2, 1])
2
>>> num_unique([1, 1, 1, 1, 1])
1
>>> num_unique([1, 2, 3, 4, 5])
5
"""
Show your work to a demonstrator here to make sure you are on the right track.
3. Write a function that takes a list of numbers and returns the mean and the median of the list as a tuple.
For the median, if there are an even number of numbers in the list, then return the average of the two
middle numbers. The function should pass the following doctests:
def mean_median(number_list):
"""
>>> mean_median([1, 2, 3])
(2.0, 2.0)
>>> mean_median([1, 2, 9])
(4.0, 2.0)
>>> mean_median([8, 2, 2])
(4.0, 2.0)
164
>>> mean_median([2, 8, 1, 1])
(3.0, 1.5)
"""
4. Write a program that reads in a string and prints a table of the letters of the alphabet that occur in
the string together with the number of times each letter occurs. The table should be arranged in
alphabetical order. You should use a dictionary for this problem. Case should be ignored. A sample
run of the program would look this this:
Enter a string: > ThiS is String with Upper and lower case Letters.
a 2
c 1
d 1
e 5
g 1
h 2
i 4
l 2
n 2
o 1
p 2
r 4
s 5
t 5
u 1
w 2
5. Write another program that reads the text from a file and produces a similar output to that in question
4.
6. Write a program that reads in a string and returns a table of letter-pairs that occur in the string and
how often they occur. A sample run of the program would look like this (the order of the table is not
important):
Get a demonstrator to review your work before you leave the lab.
165
166
Lesson 16
167
• to instantiate an integer, we might say x=5
• to instantiate a string, we might say x=’this is a string’
• to instantiate a list, we might say x=[1,2,3].
We need to do the same things for user-defined types. First we define the type (its data and behaviour),
and then we are free to create and use variables of that type.
As an example, a university student management system might define a class called Student, and then
objects of that class will be created for each student that enrols in the university:
Class Instances
Student student1 student2
Data: name Harry Hermione
id 123456 654321
age 21 20
major Dark Arts Quantum Magic
Methods:
change major(new major) change major(new major) change major(new major)
get grades() get grades() get grades()
Note how both the data (name, id, etc) and the methods (change major, get grades) are carried along with
each instance of the class.
class CLASSNAME:
<statement 1>
<statement 2>
...
<statement n>
Class definitions can appear anywhere in a program, but they are usually near the beginning (after the
import statements). The syntax rules for a class definition are the same as for other compound statements.
There is a header which begins with the keyword, class, followed by the name of the class, and ending
with a colon. In most cases, the statements inside a class definition should be function definitions. Functions
defined inside a class have a special name — they are called methods of the class. Although not required
by Python, <statement 1> should be a docstring describing the class and <statement 2> should be
an initialization method. The init 4 method is a special method that is called just after a variable of the
class type is constructed or instantiated.
We are now ready to create our own user-defined type: the Point. Consider the concept of a mathematical
point. In two dimensions, a point is two numbers (coordinates) that are treated collectively as a single
object. In mathematical notation, points are often written in parentheses with a comma separating the
coordinates. For example, (0, 0) represents the origin, and (x, y) represents the point x units to the right
and y units up from the origin. A natural way to represent a point in Python is with two numeric values.
The question, then, is how to group these two values into a compound object. One solution is to use a list
or tuple, and for some applications that might be the best choice. An alternative is to define a new user-
defined compound type, also called a class. This approach involves a bit more effort, but it has advantages
that will become apparent soon.
4 the init method has two underscores on each side of its name
168
Here’s a simple definition of a Point class which we can put into a file called Point.py (by convention,
user-defined class names begin with an uppercase letter):
class Point:
"""A class to represent a two-dimensional point"""
def __init__(self):
self.x = 0
self.y = 0
You will see from the class definition above the special method called init . This method is called when
a Point object is instantiated. To instantiate a Point object, we call a function named (you guessed it)
Point:
>>> type(Point)
<type ’classobj’>
>>> p = Point()
>>> type(p)
<type ’instance’>
The function Point() is called the class constructor. The variable p is assigned a reference to a new Point
object. When calling the function Point(), Python performs some magic behind the scenes and calls the
init method from within the class constructor.
The init method has a parameter called self. Every class method must have at least one parameter
and that parameter should be called self. The self parameter is a reference to the object on which the
method is being called (more on this below).
16.5 Attributes
The init method above contains the definition of two object variables x and y. Object variables are
also called attributes of the object. Once a Point object has been instantiated, it is now valid to refer to its
attributes:
169
>>> p1 = Point()
>>> print p1.x, p1.y
0 0
>>> p2 = Point()
>>> print p2.x, p2.y
0 0
>>> p2.x = 3
>>> p2.y = 4
>>> print p2.x, p2.y
3 4
>>> p3 = Point()
>>> print p3.x, p3.y
0 0
>>> p3.y = 4
>>> p3.x = 8
>>> print p3.x, p3.y
8 4
>>> print p2.x, p2.y
3 4
>>> print p1.x, p1.y
0 0
This syntax is similar to the syntax for selecting a variable from a module, such as math.pi or string.uppercase.
Both modules and instances create their own namespaces, and the syntax for accessing names contained in
each, called attributes, is the same. In this case the attribute we are selecting is a data item from an instance.
The following state diagram shows the result of these assignments:
Point Point Point
p1 p2 p3
x 0 x 3 x 8
y 0 y 4 y 4
The variable p1, p2 and p3 refer to Point objects, each of which contains two attributes. Each attribute
refers to a number. The expression p1.x means, Go to the object p1 refers to and get the value of x. The
purpose of dot notation is to identify which variable you are referring to unambiguously. You can use dot
notation as part of any expression, so the following statements are legal:
The first line outputs (3, 4); the second line calculates the value 25.
One can also think of classes as defining an unordered collection, where the elements are indexed by a
variable name (x and y in the Point class). In this respect, a class is a bit like a dictionary that uses strings
for keys. Classes have one big advantage over dictionaries though — we can add extra functionality to
classes with methods (discussed in Section 16.7).
170
16.6 Default Parameters
It is quite common to specify default parameters for methods, and especially for the init method. Here
is the Point class using default parameters:
class Point:
"""A class to represent a two-dimensional point"""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
Parameter specifications such as x=0, create a parameter that has a default value and is called a named pa-
rameter that has a default parameter value. Here’s another example:
class Date:
"""A class to represent a date - uses 1/1/2015 for default"""
def __init__(self, day=1, month=1, year = 2015):
self.day = day
self.month = month
self.year = year
def print_date(self):
print str(self.day)+"/"+str(self.month)+"/"+str(self.year)
#main routine
if __name__=='__main__':
d1 = Date() # uses default
d1.print_date()
d2 = Date(year=2014) # doesn't use default year
d2.print_date()
d3 = Date(day=20)
d3.print_date()
print read_value_type()
print read_value_type(prompt='Enter a float> ')
print read_value_type(convert=int)
print read_value_type(prompt='Enter a boolean> ', convert=bool)
The only limitation on default parameters is that they must come after non-default parameters in the func-
tion or method definition.
16.7 Methods
init is an example of a class function or method. We can add as many methods as we like to a class,
and this is one of the reasons OOP is so powerful. It’s quite a different way of thinking about programming.
Rather than having functions that act on any old data, in OOP, the data carries the required functionality
around with it.
171
Here’s an example of a class representing a Movie. It has 3 methods, the init for setting initial values, a
display data method for displaying the values stored, and a calculate profit method. Notice how
each method requires self as the first parameter.
class Movie:
"""
a class storing some facts about a movie
"""
def __init__(self, title, writer, director, genre, cost, revenue):
self.title = title
self.writer = writer
self.director = director
self.genre = genre
self.cost = cost
self.revenue = revenue
def display_data(self):
info = "Movie title: {}, Writer: {}, Director: {}, Genre: {}".format(
self.title, self.writer, self.director, self.genre)
print(info)
profit = "Net profit:\${}K".format( self.calculate_profit())
print(profit)
def calculate_profit(self):
return self.revenue - self.cost
#main routine
if __name__ == "__main__":
m1 = Movie("Avatar", "James Cameron",
"James Cameron","science fiction",
425000000, 2783918982)
m1.display_data()
172
Let’s add a few methods to our Point class:
import math
class Point:
"""A class to represent a two-dimensional point"""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
if __name__=='__main__':
p1 = Point()
p1.move(2,3)
p2 = Point()
p2.move(5,7)
print p1.distance(p2)
16.8 Sameness
The meaning of the word same seems perfectly clear until you give it some thought, and then you realize
there is more to it than you expected. For example, if you say, “Chris and I have the same car”, you mean
that his car and yours are the same make and model, but that they are two different cars. If you say, “Chris
and I have the same mother”, you mean that his mother and yours are the same person. When you talk
about objects, there is a similar ambiguity. For example, if two Points are the same, does that mean they
contain the same data (coordinates) or that they are actually the same object? To find out if two references
refer to the same object, use the == operator. For example:
>>> p1 = Point()
>>> p1.x = 3
>>> p1.y = 4
>>> p2 = Point()
>>> p2.x = 3
>>> p2.y = 4
>>> p1 == p2
False
Even though p1 and p2 contain the same coordinates, they are not the same object.
173
If we assign p1 to p2, then the two variables are aliases of the same object:
>>> p2 = p1
>>> p1 == p2
True
This type of equality is called shallow equality because it compares only the references, not the contents of
the objects. To compare the contents of the objects—deep equality —we can add a method called equals
to our class definition:
import math
class Point:
"""A class to represent a two-dimensional point"""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
if __name__=='__main__':
p1 = Point(2, 3)
p2 = Point(2, 3)
print p1 == p2
print p1.equals(p2)
False
True
Of course, if the two variables refer to the same object, they have both shallow and deep equality.
One of the benefits of OOP is that it is very easy to separate how a program interacts with an object (by
calling methods of the object), with how the particular class is implemented. If such a separation is made,
then the programmer can easily change the implementation without affecting all the code that uses that
class. This is a very powerful notion because we might later discover a much better way of implementing
a particular data structure. For example, I might decide to implement the point using a list rather than as
two variables:
174
import math
class Point:
"""A class to represent a two-dimensional point"""
def __init__(self, x=0, y=0):
self.pt = [x, y]
if __name__=='__main__':
p1 = Point(2, 3)
p2 = Point(2, 3)
print p1 == p2
print p1.equals(p2)
Note that although the implementation of the Point class changed, the code that used Point did not.
This is because the interface did not change (that is, the method arguments and return values remained
consistent).
This separation of interface and representation allows us to access objects independent of the class imple-
mentation. For example, an equally valid representation of a point is as an angle from horizontal and a
distance from the origin. These are called polar coordinates or (r, θ) coordinates (r for radius, θ for the an-
gle). Rather than having two different classes for each representation, we can use a single class and provide
(r, θ) coordinates as derived values via a method:
import math
class Point:
...
def get_polar_coordinates(self):
r = math.sqrt(self.x*self.x+self.y*self.y)
theta = math.atan2(self.y, self.x)
return (r, theta)
...
p1 = Point()
p1.move(0,2)
print p1.get_polar_coordinates()
(2.0, 1.5707963267948966)
175
16.10 Operator Overloading
Python includes the very convenient notion of operator overloading which allows the programmer to add
operators to classes (e.g. the plus operator, ’+’). This is very convenient for classes that represent numerical
types, but can also be useful for non-numerical types (e.g. strings). For example, if I were to define a
two-dimensional vector (a mathematical object a bit like a point), then I might define it as follows with an
add method:
class Vec2D:
def __init__(self):
self.x = 0
self.y = 0
The add method allows us to use the ’+’ operator to add two vectors:
>>> a = Vec2D()
>>> a.x = 2
>>> a.y = 3
>>> b = Vec2D()
>>> b.x = 4
>>> b.y = 5
>>> c = a+b
>>> print c.x, c.y
6 8
The line c = a+b causes the method add to be called. That method creates a new Vec2D, assigns values
to its members (in this case the sum of the two parameters), returns that new Vec2D, which is subsequently
assigned to the variable c.
16.11 Glossary
abstraction: Hiding details of implementation behind a programming structure such as a class or function.
Essentially abstracting away the details.
attribute: One of the named data items that makes up an instance.
class: A user-defined compound type. A class can also be thought of as a template for the objects that are
instances of it.
constructor: A method used to create new objects.
deep equality: Equality of values, or two references that point to objects that have the same value.
information hiding: See abstraction.
initialization: The init method is used to initialize an object when it is created.
instantiate: To create an instance of a class.
176
instance: An object that belongs to a class.
interface: The public face of a class. That is, the methods used and the method signatures (parameters and
return types).
method: A function that belongs to a class.
177
16.12 Laboratory exercises
1. (a) Write a class representing an invoice, with an init method which stores data for quantity,
description and price. Write a method which calculates the GST (15%) and displays the price
without GST, the GST value and the price including the GST.
(b) In the main routine, create several invoice objects and call the display method of each object thus
created.
2. Write and test new methods for the Date class that prints the date according to the following formats:
Show your work to a demonstrator here to make sure you are on the right track.
4. A str object can be constructed by passing any object to the str constructor.
(a) Write code that calls str on a Point object. Record the output below:
(b) The answer to the previous question is generally not what we want. To get str to produce
something sensible, we can define a str method that returns any string we like. Add a
str method to Point that returns for example, “(5, 3)” for a Point whose x value is 5 and y
value is 3.
(c) Add a str method to Date so that it returns something sensible.
5. Add a sub method for the operator ’-’ to the Vec2D class.
6. A vector can be multiplied by a scalar value (e.g. a float), which simply multiplies each component
with the scalar. Add a mul method to implement scalar multiplication. Make sure you test your
method.
7. Extension Exercise: Generalise the Vec2D class so that it can be a vector of any dimension.
8. Extension Exercise: In Lecture 14, we used lists of lists to represent a matrix. Using Test Driven Devel-
opment, implement a matrix class that includes the following methods:
Get a demonstrator to review your work before you leave the lab.
178
Lesson 17
17.1 Rectangles
Let’s say that we want a class to represent a rectangle. The question is, what information do we have to
provide in order to specify a rectangle? To keep things simple, assume that the rectangle is oriented either
vertically or horizontally, never at an angle. There are a few possibilities: we could specify the center of
the rectangle (two coordinates) and its size (width and height); or we could specify one of the corners
and the size; or we could specify two opposing corners. A conventional choice is to specify the upper-left
corner of the rectangle and the size. Again, we’ll define a new class called Rectangle in a new file called
Rectangle.py:
class Rectangle:
def __init__(self):
self.corner = Point()
self.width = 100
self.height = 100
if __name__=='__main__:
box = Rectangle()
print box.corner.x, box.corner.y, box.width, box.height
0 0 100 100
Note that within a Rectangle we have created a member attribute which is itself an object — in this case
a Point. The dot operator composes. The expression box.corner.x means, Go to the object box refers to
and select the attribute named corner; then go to that object and select the attribute named x. The figure shows the
state of this object:
Rectangle
box width 100 Point
height 100 x 0
corner y 0
179
Classes (and hence objects) can be made as complex as we like, and as programs become larger, hiding
this data complexity becomes increasingly important. This is the notion of abstraction and information
hiding. The power of using classes and objects arises because the objects can be viewed abstractly with
little thought to how the class is actually implemented. Just like we have been using strings, lists and
dictionaries without thinking about how they have been implemented, we can also create our own types
for later use, without worrying about their internals.
Functions and methods can also return instances. For example, we may want to calculate the centre of the
rectangle. We can do that by adding a centre method to our class definition:
class Rectangle:
def __init__(self):
self.corner = Point()
self.width = 100
self.height = 100
def centre(self):
c = Point()
c.x = self.corner.x + self.width/2.0
c.y = self.corner.y + self.height/2.0
return c
if __name__=='__main__':
box = Rectangle()
print box.corner.x, box.corner.y, box.width, box.height
c = box.centre()
print c.x, c.y
0 0 100 100
50.0 50.0
We can change the state of an object by making an assignment to one of its attributes. For example, to
change the size of a rectangle without changing its position, we could modify the values of width and
height:
box.width = box.width + 50
box.height = box.height + 100
180
17.4 Copying
Aliasing can make a program difficult to read because changes made in one place might have unexpected
effects in another place. It is hard to keep track of all the variables that might refer to a given object.
Copying an object is often an alternative to aliasing. The copy module contains a function called copy that
can duplicate any object:
Once we import the copy module, we can use the copy method to make a new Point. p1 and p2 are not
the same point, but they contain the same data. To copy a simple object like a Point, which doesn’t contain
any embedded objects, copy is sufficient. This is called shallow copying. For something like a Rectangle,
which contains a reference to a Point, copy doesn’t do quite the right thing. It copies the reference to the
Point object, so both the old Rectangle and the new one refer to a single Point.
If we create a box, b1, in the usual way and then make a copy, b2, using copy, the resulting state diagram
looks like this:
b1 width 100 100 width b2
height 100 x 0 100 height
corner y 0 corner
This is almost certainly not what we want. Can you think why?
zend
Fortunately, the copy module contains a method named deepcopy that copies not only the object but also
any embedded objects. You will not be surprised to learn that this operation is called a deep copy .
>>> b2 = copy.deepcopy(b1)
181
of Points and leave it at that, but there is also certain functionality that would be useful to associate with
the polygon such as adding a point, removing a point, moving the polygon, and an equality method. Using
a class allows us to package up the representation and the functionality into a convenient single piece of
Python (I’ve left out doctests for the sake of brevity) as shown in Figure 17.1.
The equals method requires some explanation. Firstly, the eq variable is a Boolean variable and it will
be False if any of the tests fail (that is, if either the length of the points is different, or any of the points are
different). Second, the for loop as used here is in a standard pattern for traversing multiple lists in lock
step. The zip function is a standard Python function that takes multiple lists as input, and returns a single
list with tuples containing elements from each list. For example:
The equals in Figure 17.1 has the same effect as the following (lengthier) equals method:
return eq
Note that many of the same method names are used in Polygon as in Point including move and equals.
This is deliberate. It is good practice to give semantically similar methods the same name in different
classes. Doing so allows programmers (and programs) to forget the type of the object and just call the move
method when they want to move a geometric object.
And here is a little bit of code using the Polygon class:
17.6 Glossary
deep copy: To copy the contents of an object as well as any embedded objects, and any objects embedded
in them, and so on; implemented by the deepcopy function in the copy module.
shallow copy: To copy the contents of an object, including any references to embedded objects; imple-
mented by the copy function in the copy module.
182
from Point import Point
import copy
class Polygon:
"""
A polygon class with a list of points.
"""
def __init__(self):
self.points = []
def __str__(self):
s = ""
for p in self.points:
s += str(p) + "; "
return s
2. Add a method called move to Rectangle that has the effect of moving the location of the rectangle.
Show your work to a demonstrator here to make sure you are on the right track.
3. Add a method called insert point at(self, x, y, index) to Polygon that inserts a new
point after the given index. If the index is -1, then it should insert the point at the beginning of the list
of points. If the index is greater than or equal to the length of the list, it should insert the point at the
end. Write several doctests to test your method.
4. Write a method that will compute the centroid of the polygon. The centroid is computed by indepen-
dently averaging the x and y coordinates of each of the vertices in the polygon. For example:
(0, 2) (2, 2)
Centroid= (1, 1)
(0, 0) (2, 0)
Notice that the coordinates of the centroid are the average of the coordinates of the points in the
polygon (in this case, both are: (0 + 0 + 2 + 2)/4 = 1).
5. Write a method that will rotate the polygon around the origin. To rotate a point by an angle θ, the
following formulae can be used:
x0 = x cos(θ) − y sin(θ)
y 0 = x sin(θ) + y cos(θ),
184
where x0 and y 0 are the new coordinates of the point.
6. Extension Exercise: Write a method that will rotate the polygon around its centroid. The easiest way
to do this is to first move the polygon so its centroid is at (0, 0), then do the rotation, then move the
polygon back to its original centroid location.
Get a demonstrator to review your work before you leave the lab.
185
186
Lesson 18
Graphical User Interface (GUI) programming is very different from the kind of programming we’ve been
discussing so far. We might call the programming we’ve done so far Sequence Driven Programming. In this
style of program, the computer does something, then does something else, perhaps waiting for the user to
type something in, or getting input from somewhere else, but essentially it proceeds in a sequential fashion.
GUI programs are different because they are usually Event Driven. In this sort of program, there is usually
a lot more user interaction, and the program almost always has to do something (redrawing itself on the
screen if nothing else).
Before looking at actual GUI programs, it’s worthwhile seeing how we might simulate event driven pro-
gramming using sequence driven programming. After all, underneath the hood, at the system level, it
really is sequence driven programming that is happening. Consider the Python program displayed in Fig-
ure 18.1.
The program in Figure 18.1 contains all the elements of an event-driven program. It has an event loop that
runs forever, a way of getting events from the user, a function that processes events, and a function that
redraws the screen. Note also that we used a class for our simulated event loop. This is not completely
necessary, but generally makes writing GUI applications a little easier.
Tkinter (Tk interface) is Python’s default GUI toolkit, although there are many others. It is reasonably
simple and lightweight and should be perfectly adequate for GUI programs of small to medium complexity.
Let’s have a look at a simple Tkinter program:
187
class EventLoop:
"""
A non-gui simulated event loop class.
"""
def __init__(self):
self.text = ""
def draw_screen(self):
for i in range(20):
print
print self.text
def get_event(self):
return raw_input()
def mainloop(self):
# start the event loop
while True:
event = self.get_event()
self.process_event(event)
self.draw_screen()
event = EventLoop()
event.mainloop()
188
from Tkinter import *
class HelloWorld:
"""
A Hello World App.
"""
def __init__(self, parent):
self.label = Label(parent, text="Hello World")
self.label.grid(column=0,row=0)
If you run1 the above program, you should see a window which looks like Figure 18.2.
The structure of the program above is typical of a Tkinter program and has the following general steps:
1. define a class which encapsulates the user interface components of the application:
Note that root and label are objects of class Tk and Label respectively. These classes are defined by
Tkinter.
One piece of notation you should be familiar with is the term widget. A widget is a GUI element. Every
GUI element can be called a widget. Tkinter has several different types of widgets. The most useful ones
are:
189
Label: a widget containing fixed text,
Button: a button widget,
Entry: a widget for entry of single lines of text,
Canvas: a widget for drawing shapes onto the screen.
class HelloWorld:
"""
A Hello World App with a callback
"""
def __init__(self, parent):
self.hello_button = Button(parent, text="Hello", command = self.say_hi)
self.hello_button.grid(column=0, row=0)
self.quit_button = Button(parent, text="Quit", fg="red", command=parent.destroy)
self.quit_button.grid(column=1,row=0)
def say_hi(self):
print "hi there"
root = Tk()
app = HelloWorld(root)
root.mainloop()
If we run the above program, we will get a window that looks like Figure 18.3. In both calls to the function
Button, there is a parameter called command. The value of the command parameter is the callback or
function associated with the action of pressing the button. So, if we press the Hello button, it should print
‘hi there’ to the system console because that’s what the function say hi does. If we press the Quit button,
then the special function parent.destroy will be called which, unsurprisingly, causes the window to be
destroyed.
Since we’re writing a GUI program, it would be very unusual (and quite disconcerting) to make use of the
console at all. When we want to give a message to the user, it is more usual to use a dialog widget. The
190
module tkMessageBox includes numerous useful dialogs. The above program can be modified to use a
showinfo dialog box instead:
class HelloWorld:
"""
A Hello World app with a message box
"""
def __init__(self, parent):
self.hello_button = Button(parent, text="Hello", command = self.say_hi)
self.hello_button.grid(column=0, row=0)
self.quit_button = Button(parent, text="Quit", fg="red",
command=parent.destroy)
self.quit_button.grid(column=1,row=0)
def say_hi(self):
showinfo(message="Hi there")
root = Tk()
app = HelloWorld(root)
root.mainloop()
Now when we press the Hello button, we should see something similar to Figure 18.4.
191
Let’s have a look at an example of how to use an Entry widget:
class HelloInput:
"""
An app that gets input from the user and associates Tkinter variables with
the input.
"""
def __init__(self, parent):
self.hello_button = Button(parent, text="Hello", command = self.say_hi)
self.hello_button.grid(column=2, row=0)
self.quit_button = Button(parent, text="Quit", fg="red", command=parent.destroy)
self.quit_button.grid(column=3,row=0)
self.input = Entry(parent)
self.input.grid(column=1,row=0)
self.label = Label(parent, text="Enter your name: ")
self.label.grid(column=0, row=0)
def say_hi(self):
showinfo(message="Hi there "+self.input.get())
root = Tk()
app = HelloInput(root)
root.mainloop()
Note that when the callback say hi is called, the string associated with the Entry widget is retrieved and
included as part of the dialog message. The main window for this program can be seen in Figure 18.5.
192
from Tkinter import *
class TempConverter:
"""
A simple app for converting from Fahrenheit to Celsius
"""
def __init__(self, parent):
self.celsius_val = Label(parent, text="", width=20)
self.celsius_val.grid(column=1,row=1)
self.celsius_label = Label(parent, text="Celsius", width=20)
self.celsius_label.grid(column=1, row=0)
self.fahr_input = Entry(parent)
self.fahr_input.grid(column=0,row=1)
self.fahr_label = Label(parent, text="Fahrenheit")
self.fahr_label.grid(column=0, row=0)
def convert(self):
# get the fahrenheit value from the widget and convert it from
# a string to a float number
dfahr = float(self.fahr_input.get())
# calculate the celsius value as a float
celsius = (dfahr-32)*5.0/9.0
# update the celsius widget with the celsius value by
# converting from a float to a string
self.celsius_val.configure(text = str(celsius))
root = Tk()
app = TempConverter(root)
root.mainloop()
The function convert does the conversion. Notice how it first gets the value from self.fahr input
which is what the user entered into the entry field. Then it does the conversion, and finally, it changes the
text on the output field by calling the configure method. If you type in and run the above program, you
should get an application that looks like Figure 18.6. 2
2 Note that if the user enters something that isn’t a number an error will occur (actually an exception). To be thorough, we should
encase the conversion in a try-except block. Unfortunately, we don’t have the time to cover exceptions in COMP150.
193
Figure 18.6: Fahrenheit to Celsius Converter
.
def some_callback_method(self):
# code you want to execute in response to an event
# goes here
def maybe_another_callback(self):
# maybe code to respond to a different event
# goes here
# main routine
root = Tk()
instance_name = ClassName(root)
root.mainloop()
General pattern:
Examples:
To respond to events you need to define a function that will be called when the event happens, and you
also need to associate the event with the function. This often happens at the time of creating widgets.
194
from Tkinter import *
class ClassName:
def __init__(self, parent):
self.my_hi_button = Button(parent, text="Hello", bg="red", command=self.callback_name)
self.my_hi_button.grid(row=0,column=0)
def callback_name(self):
print 'hello there' # this prints to the console
# main routine
root = Tk()
instance_name = ClassName(root)
root.mainloop()
18.7 Glossary
Graphical User Interface (GUI): a program interface using graphical output and input is often taken di-
rectly from the keyboard and mouse.
Event Driven: A programming style where the flow of control of the program is determined by external
events such as mouse-clicks, keyboard presses, etc.
Global variables: Variables that can be accessed from anywhere within a program. The use of global vari-
ables should be limited as much as possible.
Objects: A special type of compound variable that has specific functions (called methods) associated with
that variable type.
195
18.8 Laboratory exercises
1. Produce the following GUI. The user is able to type into the entry field. The reversed string should
appear when the user presses the REVERSE NOW button.
Show your work to a demonstrator here to make sure you are on the right track.
2. Produce the following GUI. The user should enter the base-width and height of a pyramid and press
the CALCULATE VOLUME button to see the volume displayed. The formula for calculating the
volume of a square-based pyramid is:
(width2 × height)/3
Your answer should be rounded to show 1 decimal place. The program should quit and the window
vanish when the user presses the quit button. Remember parent.destroy.
3. Produce the following GUI. The user should be able to type in the name of a file and press the Count
Lines button to display the number of lines in the file. The program should quit and the window
vanish when the user presses the quit button. Remember parent.destroy.
196
4. (a) Add a function called convert f to c to tk converter1.py from Section 18.5. convert f to c
should take the temperature in Fahrenheit as input and return the temperature in Celsius. It should
pass the following doctests:
Note that this is the correct way to test equality of real numbers, you should never use a direct com-
parison. You can have the doctests tested each time you run the program by including the following
code just before the call to root.mainloop:
import doctest
doctest.testmod()
(b) Modify the function convert so that it makes use of the new function convert f to c.
(c) Create a new function called convert c to f and devise appropriate doctests to test it.
(d) In the convert function, use a class attribute called forwardConvert which can be either True or
False. Modify convert to use an if-else statement to call convert f to c if forwardConvert
is True and convert c to f if forwardConvert is False.
(e) Add a Checkbutton button to your interface with the following code:
You will also need to add a method called toggle convert that allows the program to keep track of
whether the converter should do a forward convert or a backwards convert (do this using a boolean
variable). Finally, modify the convert method so that the appropriate conversion function is used
(either convert f to c or convert c to f).
5. Extension Exercise: Extend your temperature converter so that other conversions are also handled.
E.g. miles to kilometres, pounds to kilograms, litres to gallons etc. You’ll need to look up the conver-
sion equations on the web. Also, this exercise requires a bit of creativity in how you structure your
interface. See if you can come up with several options before deciding on a particular look.
6. Extension Exercise: Using the code from section 18.4 as a starting point write a program which reads
mathematical expressions from the user and displays a message box containing the result.
197
Get a demonstrator to review your work before you leave the lab.
198
Lesson 19
19.1 Graphics
The Tkinter canvas widget can be used to draw shapes to a window. We are going to spend the next two
lectures building a simple computer game. The game is called catch — the game tosses the ball across the
screen, and the player has to catch the ball with their mitt. The game is just for a bit of fun, but it will make
use of several important concepts we’ve introduced so far in the course. Let’s start by creating a canvas in
a file called circle.py and drawing a circle onto it:
# circle.py
from Tkinter import *
class CatchGame:
"""
A circle in a window.
"""
def __init__(self, parent):
self.canvas = Canvas(parent, width=200, height=200)
self.canvas.grid(column=0,row=0)
self.ball = self.canvas.create_oval(90,90,110,110,fill="blue")
root = Tk()
app = CatchGame(root)
root.mainloop()
If you run the above program, you should get output similar to Figure 19.1. The important line in circle.py
is the call to create oval which can produce an ellipse shape (circles are a type of ellipse). The create oval
function returns a variable which we can use later to modify various aspects of the display.
To change the coordinates of a shape on the canvas, we can use the coords function. However, if we
want to move a shape whilst the canvas is displayed, then we need to use some form of callback. In
tk move circle.py below, we add a callback called move which is called after 1 second by using the
function after:
199
Figure 19.1: Tkinter Canvas Program.
class CatchGame:
"""
A moving circle in a window.
"""
def __init__(self, parent):
self.canvas = Canvas(parent, width=200, height=200)
self.canvas.grid(column=0,row=0)
self.ball = self.canvas.create_oval(90,90,110,110,fill="blue")
self.canvas.after(1000, self.move)
def move(self):
self.canvas.coords(self.ball, 150, 150, 170, 170)
root = Tk()
game = CatchGame(root)
root.mainloop()
The self.canvas.after(1000, self.move) function call does some magic under the hood of Tkinter
and causes the function CatchGame.move to be called after 1000 milliseconds (or 1 second).
That’s all well and good, but a little limiting. For a catch game, we really need the ball to be moving
continuously across the screen. Before having a look at the solution below, it is worthwhile spending some
time to see if you can figure out how to do this yourself.
The solution is quite simple and rather ingenious. All we need to do is call the Canvas.after function at
the end of every move function. Well, not quite, we also need to do some housekeeping to keep track of the
current coordinates of the ball, and change them at each call to move. Type the following code into a file
called tk move circle2.py and see what happens:
200
from Tkinter import *
import random
class CatchGame:
"""
A random moving circle in a window.
"""
def __init__(self, parent):
self.canvas = Canvas(parent, width=200, height=200)
self.canvas.grid(column=0,row=0)
self.x = 10
self.y = 100
self.dx = 2
self.dy = 2
self.ball = self.canvas.create_oval(self.x,self.y,self.x+20,self.y+20,fill="blue")
self.canvas.after(100, self.move)
def move(self):
self.x = self.x+self.dx
self.y = self.y+self.dy
self.canvas.coords(self.ball, self.x, self.y, self.x+20, self.y+20)
self.canvas.after(100, self.move)
root = Tk()
game = CatchGame(root)
root.mainloop()
When running the program you should see the ball move from the top left of the canvas towards the bottom
right and then off the screen forever.
Now we have a ball that’s a bit more interesting — it starts on the left hand side of the window, and heads
off in a random direction. Unfortunately, it often quickly disappears from the window, never to return.
What we really need is to have the ball bounce off the top and bottom of the window. This is another
situation where it’s worth spending some time thinking about how to make the ball bounce. So before you
look on ahead, see if you can come up with a reasonable scheme.
The solution is actually quite simple. A ball bouncing without losing energy in the collision should leave
the bounce at the same angle as it entered. If the ball is bouncing off a horizontal surface, its velocity in the
201
x direction will stay the same, and it’s velocity in the y direction will be opposite to what it was before. This
can be achieved fairly easily with an if-elif statement. The move function becomes:
class CatchGame:
"""
A bouncing random moving circle in a window.
"""
def __init__(self, parent):
self.canvas = Canvas(parent, width=200, height=200)
self.canvas.grid(column=0,row=0)
self.x = 10
self.y = random.randint(10,190)
self.dx = 2
self.dy = random.randint(-5,5)
self.ball = self.canvas.create_oval(self.x,self.y,self.x+20,self.y+20,
fill="blue")
self.canvas.after(100, self.move)
def move(self):
self.x = self.x+self.dx
self.y = self.y+self.dy
if self.y<0:
self.y = 0
self.dy = -self.dy
elif self.y>180:
self.y = 180
self.dy = -self.dy
self.canvas.coords(self.ball, self.x, self.y, self.x+20, self.y+20)
self.canvas.after(100, self.move)
root = Tk()
game = CatchGame(root)
root.mainloop()
The ball should now bounce off the top and the bottom of the screen. I’m a little concerned that there are a
lot of magic numbers (0, 180, 100, 20 etc) creeping into the code. All these numbers make the code a bit hard
to read, and can make code hard to modify. Generally, if you find that you use the same number in more
than one place, it is good practice to put it in a constant variable — that way you can modify the constants
in your program at a later date by just changing one number and not several. Notice that I’ve introduced
initialization parameters to specify the values of these numbers, but I’ve also provided sensible defaults
using named parameters. This is generally good practice as it allows the programmer to initiallize a game
with a minimum of effort. Also, with GUI widgets, we can often discover their state (such as their width
and height), by using the widget’s query method. To discover the width of a widget in Tkinter, we can use
the winfo width() method. Why do you think that’s preferable than specifying a program constant? If
we make those changes, our bouncing ball program eventually looks like bounce.py:
202
# bounce.py
from Tkinter import *
import random
class CatchGame:
"""
A random bouncing circle in a window with constant variables.
"""
def __init__(self, parent, width=200, height=200, ball_size=20, wait_time=100):
self.ball_size = ball_size
self.wait_time = wait_time
self.canvas = Canvas(parent, width=width, height=height)
self.canvas.grid(column=0,row=0)
self.x = 10
self.y = random.randint(10,height-ball_size)
self.dx = 2
self.dy = random.randint(-5,5)
self.ball = self.canvas.create_oval(self.x,self.y,
self.x+ball_size,self.y+ball_size,
fill="blue")
self.canvas.after(wait_time, self.move)
def move(self):
self.x = self.x+self.dx
self.y = self.y+self.dy
if self.y<0:
self.y = 0
self.dy = -self.dy
elif self.y>self.canvas.winfo_height()-self.ball_size:
self.y = self.canvas.winfo_height()-self.ball_size
self.dy = -self.dy
self.canvas.coords(self.ball, self.x, self.y,
self.x+self.ball_size, self.y+self.ball_size)
self.canvas.after(self.wait_time, self.move)
root = Tk()
game = CatchGame(root, width=400, height=400, ball_size=5, wait_time=10)
root.mainloop()
19.4 Glossary
random: Having no specific pattern. Unpredictable. Computers are designed to be predictable, and it is
not possible to get a truly random value from a computer. Certain functions produce sequences of
values that appear as if they are random, and it is these pseudo-random values that we get from Python.
203
19.5 Laboratory exercises
1. The following Tkinter script draws a simple house on a Tkinter canvas:
root = Tk()
houseCanvas = Canvas(root, width=400, height=400)
houseCanvas.grid(row=0, column=0)
root.mainloop()
(a) Create a file called house.py, and type in the above code. Run this script and confirm that you
get a window that looks like this:
so that you can pass in the location of the house on the canvas and a colour for the main body of
the house.
(f) Use draw house to place five houses on the canvas in different locations, each with a different
colour.
204
(g) Create a House class that incorporates all the necessary attributes to draw a house on a can-
vas. For example, position (x, y), colour, width and height (and any other attributes it needs).
The class should have an init method that initialises all the necessary attributes. Add a
draw method to the class that actually does the drawing. Redo the previous exercise (5 different
houses) using 5 different objects of the House class.
Show your work to a demonstrator here to make sure you are on the right track.
2. Modify the code in bounce.py, to make the ball go faster.
3. Modify the code in bounce.py so that the bounce is a little bit random. This should make the catch
game we develop in the next lecture a bit more interesting.
4. Extension Exercise: Modify your bounce code so the ball bounces off all the walls.
5. Extension Exercise: Add fixed rectangles to the bounce canvas. Arrange it so the moving ball also
bounces off the rectangles.
6. Extension Exercise: Add fixed circles to the board. You will have to figure out what the direction of the
bounce should be in the case of the ball hitting a circle.
Get a demonstrator to review your work before you leave the lab.
205
206
Lesson 20
class CatchGame:
"""
A moving mitt in a window.
"""
def __init__(self, parent, width=200, height=200, mitt_size=20):
self.canvas = Canvas(parent, width=width, height=height)
self.canvas.grid(column=0,row=0)
self.mittx = width-30
self.mitty = height/2
self.mitt_size = mitt_size
self.mitt = self.canvas.create_oval(self.mittx, self.mitty,
self.mittx+self.mitt_size,
self.mitty+self.mitt_size,
fill="blue")
parent.bind("k", self.move_mitt)
parent.bind("j", self.move_mitt)
root = Tk()
catch = CatchGame(root)
root.mainloop()
207
The callback function, move mitt takes the variable event as a parameter. This variable is of type Event.
Each event we want the program to respond to must be bound with a bind call. You can bind as many
events as you like, and they can also be bound to different callbacks if desired. In move mitt, the particular
event is checked by testing the char attribute of the Event type. Type in help(Event) at the python
prompt to find out what other attributes an Event variable has.
# bounce_and_mitt.py
class CatchGame:
"""
An almost complete catch game with a ball and a mitt.
"""
def __init__(self, parent, width=200, height=200, ball_size=5,
wait_time=100, mitt_size=20):
self.canvas = Canvas(parent, width=width, height=height)
self.canvas.grid(column=0,row=0)
# mitt stuff
self.mittx = width-30
self.mitty = height/2
self.mitt_size = mitt_size
self.mitt = self.canvas.create_oval(self.mittx, self.mitty,
self.mittx+self.mitt_size,
self.mitty+self.mitt_size, fill="red")
parent.bind("k", self.move_mitt)
parent.bind("j", self.move_mitt)
# ball stuff
self.ball_size = ball_size
self.wait_time = wait_time
self.x = 10
self.y = random.randint(10,height-ball_size)
self.dx = 2
self.dy = random.randint(-5,5)
self.ball = self.canvas.create_oval(self.x,self.y,
self.x+ball_size,self.y+ball_size,
fill="blue")
self.canvas.after(wait_time, self.move)
208
def move(self):
self.x = self.x+self.dx
self.y = self.y+self.dy
if self.y<0:
self.y = 0
self.dy = -self.dy
elif self.y>self.canvas.winfo_height()-self.ball_size:
self.y = self.canvas.winfo_height()-self.ball_size
self.dy = -self.dy
self.canvas.coords(self.ball, self.x, self.y,
self.x+self.ball_size, self.y+self.ball_size)
self.canvas.after(self.wait_time, self.move)
root = Tk()
catch = CatchGame(root)
root.mainloop()
Which is all very nice, but since the ball and mitt don’t interact, it doesn’t make for much of a game. What
we need to do is check for collisions between the ball and the mitt. Take a bit of time to think about how
that might be achieved before looking at the answer below.
What we need to do is check if the ball is located near the mitt. With circles, this is fairly easy — we just
need to check if the distance between the circle centres is smaller than the sum of the two radii. See Figure
20.1 for a diagram of the setup.
(mittx, mitty)
mitt size
2.0
(x, y)
Mitt
ball size
2.0
Ball
209
p
Recall that d(p1 , p2 ) = (x1 − x2 )2 + (y1 − y2 )2 , where p1 = (x1 , y1 ) and p2 = (x2 , y2 ) are two points, and
d(p1 , p2 ) is the distance between them. That function would be easy enough to implement, but we have a
slight hiccup since create oval uses the top-left corner of a bounding box, rather than the circle centre to
represent a circle, so we need to calculate the centre coordinates of the ball and mitt first. We can add catch
functionality to the game by replacing the line self.canvas.after(self.wait time, self.move)
in the move function with the following code:
Note the structure of the new code. There are three mutually exclusive options: we caught the ball; we
missed the ball (and it’s gone off screen); or the ball hasn’t reached us yet. The perfect structure for an
if-elif-else statement. But even this code is getting a bit complex. Can you see an easy way to simplify
it?
Essentially the same code is part of the statements for the if part as well as the elif part. There is also very
similar code at the start of the program as well. It makes sense to abstract that code away into a function.
We might define the restart function as follows:
self.x = 0
self.dx = 2
self.y = random.randint(0,self.canvas.winfo_height()-self.ball_size)
self.dy = random.randint(-5,5)
self.canvas.after(self.wait_time*10, self.move)
I can then reduce the size of the code by replacing the if statement with the following:
if distance<=(self.ball_size+self.mitt_size)/2.0:
print "Good Catch"
self.restart()
# or have we totally missed it
210
elif self.x>self.canvas.winfo_width():
print "You missed"
self.restart()
# or hasn't it got to us yet
else:
# make sure we're called again
self.canvas.after(self.wait_time, self.move)
I also had to make one other minor change. In the init method, I added the following line of code after
the call to self.canvas.grid:
self.canvas.wait_visibility()
This function ensures that the window has been created and is visible before doing anything else. If the win-
dow wasn’t yet visible, then any call to self.canvas.winfo width() or self.canvas.winfo height()
would return 0 and that could cause other parts of the code to fail.
# catch_final.py
class CatchGame:
"""
A catch game. Catch the ball with the mitt. To move the mitt, press k to
move up, j to move down.
"""
def __init__(self, parent, width=200, height=200, ball_size=5,
wait_time=100, mitt_size=20):
# canvas is the field on which the game is played
self.canvas = Canvas(parent, width=width, height=height)
# set the position of the canvas
self.canvas.grid(column=0,row=0)
# have to wait until the canvas is visible before we can do
# other stuff.
self.canvas.wait_visibility()
# mitt stuff
# position
self.mittx = width-30
self.mitty = height/2
# size
self.mitt_size = mitt_size
# visual representation of mitt
self.mitt = self.canvas.create_oval(self.mittx, self.mitty,
self.mittx+self.mitt_size,
211
self.mitty+self.mitt_size,
fill="red")
# bind k, j keys to the method move_mitt
parent.bind("k", self.move_mitt)
parent.bind("j", self.move_mitt)
# ball stuff
# size
self.ball_size = ball_size
# wait_time effectively controls the speed of the ball but is
# the time between successive calls to move. Hence, a larger
# wait_time means a slower ball and vice versa.
self.wait_time = wait_time
# this sets the ball position back to the left hand side and
# initiallises speed and direction.
self.restart()
# visual representation of the ball
self.ball = self.canvas.create_oval(self.x,self.y,
self.x+ball_size,self.y+ball_size,
fill="blue")
# scoring stuff
self.scores = Label(parent, text="Computer 0: Player 0")
# put the score below the canvas
self.scores.grid(column=0,row=1)
# initiallise the scores
self.computer_score = 0
self.player_score = 0
def restart(self):
# start at left hand side
self.x = 0
# move ball by 4 pixels with each call to move. Changing this
# value can be used to make the ball go faster or slower
self.dx = 4
# start y position of ball is anywhere within bounds of canvas
self.y = random.randint(0,self.canvas.winfo_height()-self.ball_size)
# sets the direction of the ball. Making the numbers higher
# means allowing a bigger change in the y velocity.
self.dy = random.randint(-5,5)
# this is what causes the ball to be continuously updated.
# Effectively puts a vall to move onto a queue.
self.canvas.after(self.wait_time*10, self.move)
def move(self):
212
# position is changed based on velocity (dx, dy) of ball
self.x = self.x+self.dx
self.y = self.y+self.dy
# if out of bounds, bounce the ball
if self.y<0:
self.y = 0
self.dy = -self.dy
elif self.y>self.canvas.winfo_height()-self.ball_size:
self.y = self.canvas.winfo_height()-self.ball_size
self.dy = -self.dy
# redraw the ball at its new position
self.canvas.coords(self.ball, self.x, self.y,
self.x+self.ball_size, self.y+self.ball_size)
# check if we've caught the ball
# calculate the centre of the ball
ball_centre_x = self.x + self.ball_size/2.0
ball_centre_y = self.y + self.ball_size/2.0
# calculate the centre of the mitt
mitt_centre_x = self.mittx + self.mitt_size/2.0
mitt_centre_y = self.mitty + self.mitt_size/2.0
# calculate the distance between the centres
distance = sqrt((ball_centre_x-mitt_centre_x)**2+
(ball_centre_y-mitt_centre_y)**2)
# did we catch the ball?
if distance<=(self.ball_size+self.mitt_size)/2.0:
# update the scores
self.player_score += 1
self.scores.configure(text="Computer " + str(self.computer_score) +
": Player " + str(self.player_score))
# start again
self.restart()
# or have we totally missed it
elif self.x>self.canvas.winfo_width():
# update scores
self.computer_score += 1
self.scores.configure(text="Computer " + str(self.computer_score) +
": Player " + str(self.player_score))
# start again
self.restart()
# or hasn't it got to us yet
else:
# make sure we're called again
self.canvas.after(self.wait_time, self.move)
root = Tk()
catch = CatchGame(root)
root.mainloop()
This is a reasonable sized program, and minimising the complexity of programs of this size and bigger can
be quite difficult. There are several techniques you can use to make this easier:
213
• If the problem seems too big to know where to start, break it up into smaller milestones and build the
program bit-by-bit.
20.4 Glossary
bind: The process of associating a widget event with a function.
constant: A numerical value that does not change during the execution of a program. It is conventional
to use names with all uppercase letters to represent constants, though Python programs rely on the
discipline of the programmers to enforce this, since there is no language mechanism to to support true
constants in Python.
refactoring: The process of reorganising code to make it easier to understand, read and maintain.
Show your work to a demonstrator here to make sure you are on the right track.
3. At the moment, the program will keep going forever, or until you close the window. Add code that
will stop the program when either the player or computer score reaches 10. Announce the winner
using a tkMessageBox.
4. Modify catch final.py so that after a player catches the ball, the computer pitches faster, and after
a player misses, the computer pitches slower.
1. Copy catch final.py to pong1.py and change the mitt into a paddle by using create rectangle
instead of the create oval. Make the adjustments needed to keep the paddle on the screen.
2. Copy pong1.py to pong2.py. Replace the distance function with a boolean function
hit(bx, by, r, px, py, h) that returns True when the vertical coordinate of the ball (by) is
between the bottom and top of the paddle, and the horizontal location of the ball (bx) is less than or
equal to the radius (r) away from the front of the paddle. Use hit to determine when the ball hits the
214
paddle, and make the ball bounce back in the opposite horizontal direction when hit returns True.
Your completed function should pass these doctests:
Finally, change the scoring logic to give the player a point when the ball goes off the screen on the left.
3. Copy pong2.py to pong3.py. Add a new paddle on the left side of the screen which moves up
when ’a’ is pressed and down when ’s’ is pressed. Change the starting point for the ball to the
center of the screen, (400, 300), and make it randomly move to the left or right at the start of each
round.
Get a demonstrator to review your work before you leave the lab.
215
216
Lesson 21
System programming
The sys module contains functions and variables which provide access to the environment in which the
python interpreter runs. The following example shows the values of a few of these variables on one of our
systems 1 :
Starting Jython on vex produces different values for the same variables:
1 Note that if you try this on your machine, or a lab machine, then the results will be different.
217
The argv variable holds a list of strings read in from the command line when a Python script is run. These
command line arguments can be used to pass information into a program at the same time it is invoked:
#
# demo_argv.py
#
import sys
print sys.argv
Running this program from the unix command prompt demonstrates how sys.argv works:
argv is a list of strings. Notice that the first element is the name of the program. Arguments are separated
by white space, and separated into a list in the same way that string.split operates. If you want an
argument with white space in it, use quotes:
With argv we can write useful programs that take their input directly from the command line. For example,
here is a program that finds the sum of a series of numbers:
#
# sum.py
#
from sys import argv
nums = argv[1:]
print sum(nums)
In this program we use the from <module> import <attribute> style of importing, so argv is
brought into the module’s main namespace. We can now run the program from the command prompt like
this:
$ python sum.py 3 4 5 11
23
$ python sum.py 3.5 5 11 100
119.5
218
21.2 The os and glob module
The os module contains functions for interacting with the underlying operating system. It includes many
standard operating system functions including, but not limited to:
>>> import os
>>> photos =
os.listdir('/home/cshome/coursework/COMP150/coursefiles150/Photos/')
>>> photos
['112_1286.jpg', '112_1287.jpg', '112_1284.jpg', '112_1285.jpg',
'112_1282.jpg', '112_1283.jpg', '112_1280.jpg', '112_1281.jpg',
'112_1288.jpg', '112_1276.jpg', '112_1277.jpg', '112_1274.jpg',
'112_1275.jpg', '112_1272.jpg', '112_1273.jpg', '112_1271.jpg',
'112_1278.jpg', '112_1279.jpg']
>>> os.stat('/home/cshome/coursework/COMP150/coursefiles150/Photos/' + photos[0])
(33216, 597894501L, 436207622L, 1, 15391, 1026, 1089680L, 1239768034,
1239768034, 1239768034)
The os.stat command returns several pieces of information about the file in question, including its size
(1089680L).
The glob module is another useful module that contains several functions, but the most important one is
the glob function. Globbing is a term for converting a generic name which includes wildcard characters,
into specific names of files or directories that match the generic name. Here are some simple examples:
219
>>> import glob
>>> glob.glob("/home/cshome/coursework/COMP150/coursefiles150/Photos/*.jpg")
['/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1272.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1286.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1280.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1274.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1275.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1281.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1287.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1288.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1273.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1285.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1271.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1277.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1278.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1283.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1282.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1276.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1279.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1284.jpg']
>>> glob.glob("/home/cshome/coursework/COMP150/coursefiles150/Photos/*2.jpg")
['/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1272.jpg',
'/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1282.jpg']
The combination of a little bit of programming knowledge, plus the os and glob modules really allows us
to remove the drudgery from a lot of mundane and laborious computer tasks.
21.3.1 ImageMagick
ImageMagick is a suite of stand-alone programs that are often run from the command line which makes
them ideal for scripting or batch processing. The most useful of the programs is the utility called convert.
If you open a terminal shell and type convert at the prompt, you will be greeted with a dizzying array
of options (not reproduced here for space reasons). There are lots of things you can do with images using
convert. Let’s try out a few simple ones.
To convert one of our pictures to a charcoal drawing, try the following command 2 :
$ convert \\
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1286.jpg \\
-charcoal 5 charcoal.jpg
In a finder window, double-click on charcoal.jpg to see the result. The result should look similar to
Figure 21.1.
2 You can type the whole command on a single line without the \\s.
220
Figure 21.1: A charcoaled image
$ convert \\
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1271.jpg \\
-swirl 90 swirl.jpg
The result should look similar to Figure 21.2. Too much to drink perhaps? A bad trip maybe?
Try out a few other operations if you feel so inclined. However, enough frivolity, on to the real work.
First, create a file called magick.py. Let’s start with listing all the photos in the shared Photos folder. Add
the following code to magick.py and execute it.
221
import os, glob
photos = glob.glob("/home/cshome/coursework/COMP150/coursefiles150/Photos/*.jpg")
for p in photos:
print(p)
print("Done")
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1272.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1286.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1280.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1274.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1275.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1281.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1287.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1288.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1273.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1285.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1271.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1277.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1278.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1283.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1282.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1276.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1279.jpg
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1284.jpg
Done
That’s a good start because now we know we can specify the input filenames. What about the output
files? Well, we can’t just use the input filenames as the output filenames for two reasons: we don’t want
to overwrite the original files; and you don’t have write access to the above directory/folder. So, we need
to do some processing on the filenames. To do this, the re (for regular expressions) package is useful. We
could spend several lectures on regular expressions, but we won’t. We’ll try to give you a little taste of them
here — if you don’t fully understand what’s going on, don’t panic, the most important thing is that you
recognise that regular expressions are an incredibly powerful way of manipulating strings.
Regular expressions are a bit like globbing, except the rules are a bit different. Have a close look at the
format of the filenames listed above. What we would like to do is maintain the filename part of the output
file so it’s easy to associate the input and output, but save the file in the current directory. To do that, we
need to chop out all the leading directory names and structure. To do that, we use the re.sub command.
Here’s how:
import os, glob, re
photos = glob.glob("/home/cshome/coursework/COMP150/coursefiles150/Photos/*.jpg")
for p in photos:
newp = re.sub(".*/([a-z_0-9]+)\.jpg", r"\1", p)
print("input name: " + p + ", output name: " + newp)
print("Done")
222
The re.sub command (sub is short for substitute) is performing a bit of regular expression magic. Let’s
have a quick look at what all those parts are doing. The first parameter is a regular expression (RE), the
second a replacement string, and the third, the string to do the matching and replacing on. The RE in this
case is .*/([a-z_0-9]+)\.jpg. Let’s look at different parts of the RE:
• jpg: Characters that occur unadorned are simply matched directly to the input string. So in this case,
the characters jpg will only match to the end of the input strings (the file type).
• \.: A . is a special character (described below). When we actually want to match with a . in the
input string, we need to precede it with a \. So the final part of the RE, \.jpg, matches the end of the
filenames.
• [a-z_0-9]: Square braces indicate that the RE should match any of the characters inside the braces.
Rather than typing out all the characters in the alphabet and all the numerals, we can abbreviate those
with the range operator -. So a-z matches all lowercase characters, 0-9 matches all numerals, and
_ matches the underscore character. The sub-expression [a-z_0-9] therefore matches any single
lower case character, underscore or numeral. If we add a + to the end, then we can match any sequence
of one or more of those characters.
• ([a-z_0-9]+): The round brackets around this sub-expression, essentially saves the matched part
to a variable for later use. In this case, the first part of the filename is saved to the special variable \1.
• .*/: In the first part of the RE, the . is a special character that matches any character, and the * is
a special character that says to match 0 or more instances of the previous matched character. So, .*
matches any sequence of characters. If we add a /, then .*/ matches any sequence of characters
followed by a /. In other words, all the leading directory information.
The final result of the RE is that we’ll match the complete filename (including the directory structure) of
each file, and more importantly, the filename, minus the directory structure and file type, will be saved in
the special variable \1. We then use the special variable in the replacement parameter (in this case, r’\1’
of the function. The final result is that we replace the entire name, with just the important part of the
filename.
Now that we have useful input and output names, it is a simple matter to create a customised command to
pass to the os.system function:
photos = glob.glob("/home/cshome/coursework/COMP150/coursefiles150/Photos/*.jpg")
for p in photos:
newp = re.sub('.*/([a-z_0-9]+)\.jpg', r'\1', p)
print("input name: " + p + ", output name: " + newp)
command = "convert " + p + " -resize 50x50 " + newp + "_50.jpg"
print("doing: " + command)
os.system(command)
print("Done")
223
input name:
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1272.jpg,
output name: 112_1272
doing: convert
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1272.jpg
-resize 50x50 112_1272_50.jpg
input name:
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1286.jpg,
output name: 112_1286
doing: convert
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1286.jpg
-resize 50x50 112_1286_50.jpg
input name:
/home/cshome/coursework/COMP150/coursefiles150/Photos/112_1280.jpg,
output name: 112_1280
...
Done
For this particular problem, application programs, such as PhotoShop, offer a batch processing alternative
within the application. However, the power of such approaches is limited to what the application provides
— which depends on what the programmer thinks the user might need. The power of the scripting ap-
proach is that it is limited only by the users skill and imagination — all command line driven programs can
be scripted and many GUI programs too.
21.4 Glossary
environment: The operating context in which a program runs. This would include things such as the file
system, current working directory, network environment etc.
command line arguments: Any arguments passed to a program from the command line.
glob: A common method for selecting multiple files from the command line.
regular expression: A special type of expression that is used for string matching.
224
21.5 Laboratory exercises
1. Write a program named mean.py that takes a sequence of numbers on the command line and returns
the mean of their values.
$ python mean.py 3 4
3.5
$ python mean.py 3 4 5
4.0
$ python mean.py 11 15 94.5 22
35.625
A session of your program running on the same input should produce the same output as the sample
session above.
2. Write a program named median.py that takes a sequence of numbers on the command line and
returns the median of their values.
$ python median.py 3 7 11
7
$ python median.py 19 85 121
85
$ python median.py 11 15 16 22
15.5
A session of your program running on the same input should produce the same output as the sample
session above.
3. Modify the program you wrote for Exercise 5 in Section 15.13 so that it takes the file to open as a
command line argument.
Show your work to a demonstrator here to make sure you are on the right track.
4. Write a program that will list all the python programs (that is, files that end in .py) in a given directory,
where the directory name is specified on the command line.
5. Extend the program from the previous exercise so that all functions defined in the python programs
are printed to the screen. The output should look something like this (python funcs is the program,
dir is the directory to scan):
6. Extension Exercise: Modify the final version of magick.py, so that it first resizes each input image to
200x200, then produces a morph sequence between each pair of input images. To morph between two
input images you can use the convert program as follows:
225
Get a demonstrator to review your work before you leave the lab.
226
Lesson 22
To wrap-up this course, we are going to look at a case study in which we implement a simple cryptography
encoder and editor for text files. This case study will bring together several aspects of the topics you have
been learning over the past several weeks including: files, strings, GUI programming, and dictionaries.
And, along the way, we’re going to see what it’s like to really think like a computer scientist.
• open a file
• edit a file
227
Is there anything else a user might be concerned about?
zend
That’s pretty much it for a bare-bones text editor and encryptor. Let’s get coding.
Before actually starting to code, there is another decision to be made. Should we develop the GUI first and
add the functionality later, or the functionality first and the GUI later? There’s no hard and fast rule for this
question, and in many cases it depends on the task at hand. For this problem it probably makes sense to
develop in a GUI first manner.
First, let’s get a basic text window up and running. Tkinter has a text widget called Text which we
could use. But with larger documents we will need to be able to scroll the text and the Text widget doesn’t
supply that functionality. Luckily, there is a slightly more advanced widget in the ScrolledText module.
Following is code for creating a basic scrolled text window.
import Tkinter
import ScrolledText
if __name__ == "__main__":
root = Tkinter.Tk()
textWidget = ScrolledText.ScrolledText(root, width=80, height=50)
textWidget.pack()
root.mainloop()
Notice how we’re using the “Release Early, Release Often” method. We already have a working program!
The next step is to add an option for opening and loading in a file. The usual way of providing file opening
operations is via a menu bar. We haven’t met menu bars in Tkinter as yet, but they are quite straightfor-
ward. In the following code we add a menu bar with a file option.
228
import Tkinter
import ScrolledText
class TextEdit:
"""
A simple text editor using a ScrolledText widget.
"""
def __init__(self, parent):
self.parent = parent
self.textWidget = ScrolledText.ScrolledText(parent, width=80, height=50)
self.textWidget.pack()
def open_command(self):
print "hello"
if __name__ == "__main__":
root = Tkinter.Tk()
textApp = TextEdit(root)
root.mainloop()
You will notice several things about the above program. First, a top-level menu bar (called menuBar) is
created. This is followed by the creation of a second level menu bar (called fileMenu). We can add as
many menus as we like, but in this application, only one is needed. To fileMenu we added a command
which is simply a Python function. So far, the command doesn’t do anything very interesting, but we still
have a working program.
To open a file, we can use a tkFileDialog:
import tkFileDialog
def open_command(self):
file = tkFileDialog.askopenfile()
if file != None:
contents = file.read()
self.textWidget.delete("1.0", Tkinter.END)
self.textWidget.insert(Tkinter.END, contents)
This function now does several things. It gets a file via the tkFileDialog.askopenfile() method,
reads the contents of the file into a string, deletes any text currently in the main window and then inserts
the text from the file into the window. The textWidget.delete and textWidget.insert methods
deserve some extra attention.
The textWidget.delete method takes two indexes and deletes all the text between the first index and
up to but not including the last index. The string "1.0" is a specially formatted index string of the form
LINE NUMBER.CHARACTER. So, "1.0" refers to line 1, character 0 1 . The constant Tkinter.END refers to
the end of the text.
The textWidget.insert method takes just one index and inserts its second parameter just before the
character specified by the first parameter. The result of the whole function is to read all the text in from a
229
file and replace the text in textWidget with the file contents.
def save_command(self):
file = tkFileDialog.asksaveasfile()
if file != None:
# slice off the last character from get, as an extra return is added
contents = self.textWidget.get("1.0", Tkinter.END)[:-1]
file.write(contents)
file.close()
At this point, we have a quite usable text editor. You could even use it to write Python programs if you
wanted to. The full listing is given in Figure 22.1. (I’ve added a Quit menu option as well):
Note that commandBar and textWidget are both children of the parent window. The openButton
widget, however, is a child of commandBar (and therefore a grandchild of parent). This hierarchical
organisation of the GUI is a very common mechanism for more complicated layouts. To add more buttons
to the command bar, we just need to make the new buttons children of commandBar.
230
import Tkinter
import ScrolledText
import tkFileDialog
class TextEdit:
"""
A simple text editor using a ScrolledText widget.
"""
def __init__(self, parent):
self.parent = parent
self.textWidget = ScrolledText.ScrolledText(parent, width=80, height=50)
self.textWidget.pack()
def open_command(self):
file = tkFileDialog.askopenfile()
if file != None:
contents = file.read()
self.textWidget.delete("1.0", Tkinter.END)
self.textWidget.insert(Tkinter.END, contents)
def save_command(self):
file = tkFileDialog.asksaveasfile()
if file != None:
contents = self.textWidget.get("1.0", Tkinter.END)[:-1]
file.write(contents)
file.close()
def exit_program(self):
self.parent.destroy()
if __name__ == "__main__":
root = Tkinter.Tk()
textApp = TextEdit(root)
root.mainloop()
231
22.6 Laboratory Exercises
1. Add to the command bar buttons for saving and quitting.
2. It is possible to use images instead of words for displaying buttons. All icons you see on toolbars in
applications are simply images attached to buttons. Tkinter uses the PhotoImage class to load images
and icons. For example, to replace the word “Open” on the command bar with an icon from a file
called “open.gif”, we can use the following code:
self.openIcon = Tkinter.PhotoImage(file="open.gif")
self.openButton = Tkinter.Button(self.commandBar, image=self.openIcon,
command=self.open_command)
On blackboard, there are several example icon files. Replace all of the words on the command bar
with icon buttons instead. Experiment with using different images. You can even use images that you
created (either through an image creation program like GIMP, or just use a photo that you’ve taken).
3. Write a simple spell checker program. Your program should ask the user to enter a word to check (use
a dialog box or an Entry widget). If it is spelled correctly, the program should say OK. If it is spelled
incorrectly, the program should say so and make some suggestions regarding correct spelling. The
file /usr/share/dict/words contains a good list of words for English. The steps to follow in your
program are to load the list of words into memory, then search that list for the word from the user. If
it’s there, then OK, but if not, then it’s a misspelling.
4. Add a spell checker to your text editor program from the first two questions.
Get a demonstrator to review your work before you leave the lab.
232
Lesson 23
In the last lecture we saw how to write a simple text editor using Python. In this lecture we are going to
extend that editor to optionally encrypt the contents of the document.
Rotate A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
2 C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
The top row of the table indicates the input character, and the subsequent rows indicates the output char-
acter based on a rotation of 1 or 2 characters. Can you see why we call it a rotation? What is the maximum
rotation we can perform?
How can we represent the rotation in Python? Well, we could use a dictionary which has a character as the
key and another character as the value like so:
caesar = dict()
caesar["A"] = "B"
caesar["B"] = "C"
caesar["C"] = "D"
caesar["D"] = "E"
caesar["E"] = "F"
...
newchar = caesar[oldchar]
233
or something similar.
However, there is a simpler way involving the use of the functions ord and chr. The ord function returns
the integer representation of a character:
>>> ord("a")
97
>>> ord("A")
65
>>>
This example explains why "Apple" < "apple" evaluates to True. The problem of using numbers to
encode alphabets is a fairly complex one. The ord function uses a character set called ASCII (American
Standard Code for Information Interchange) which is a simple and commonly used character set, but is
neither universal nor particularly versatile. It is often used to encode English text, but is hopeless for other
languages (including Latin languages such as French). The Unicode character set is rather more versatile
and is a global standard, but due to its complexity, we won’t use it here. ASCII uses the numbers from 32 to
126 to represent printable characters, and some of the numbers from 0 to 31 for various control codes (such
as end of line). The chr function is the inverse of the ord function. If you pass it a number between 32 and
126, it will return a printable character:
>>> chr(35)
'#'
>>> chr(90)
'Z'
Now we can convert characters into numbers and vice versa, so we might be tempted to implement a
Caesar cipher like this:
What we really want to do is limit the results of ord(char)+rotateBy to be between 0 and 126 which we
can do by using modulo arithmetic. We’ve seen the modulus operator in Lecture 2. A more correct Caesar
cipher can be written as:
Actually, there is a technical issue with chr(0) and the ScrolledText widget that is singularly unin-
teresting. Suffice to say that we need to use a modified version to get the Caesar cipher to work with the
ScrolledText widget:
Can you tell what the difference between the two functions is?
234
def caesarDecrypt(char, rotateBy):
return chr((ord(char)-rotateBy-1)%126)
We could, if we wished, use caesarEncrypt and caesarDecrypt to encrypt our files, but such an en-
cryption algorithm is trivially easy to break (see the laboratory exercises below). A Vigenere cipher makes
use of a Caesar cipher but is much more difficult to break (although not impossible, except in very limiting
circumstances). The Vigenere cipher simply encrypts each character in the input string with a potentially
different rotateBy value. Since rotateBy values are limited to between 0 and 126 anyway, it makes
sense to use a string representation of the rotateBy values. In other words, a password:
Take some time to look at these functions and make sure you understand what is going on.
import Tkinter
import ScrolledText
import tkFileDialog
import tkSimpleDialog
import random
class EncryptTextEdit:
"""
A simple text editor that allows encryption and decryption of files for saving.
"""
def __init__(self, parent):
self.parent = parent
self.textWidget = ScrolledText.ScrolledText(parent, width=80, height=50)
self.textWidget.pack()
235
self.fileMenu.add_command(label="Open", command=self.open_command)
self.fileMenu.add_command(label="Save", command=self.save_command)
self.fileMenu.add_separator()
self.fileMenu.add_command(label="Quit", command=self.exit_program)
self.menuBar.add_cascade(label="File", menu=self.fileMenu)
self.encryptMenu = Tkinter.Menu(self.menuBar, tearoff=0)
self.encryptMenu.add_command(label="Encrypt", command=self.encrypt_command)
self.encryptMenu.add_command(label="Decrypt", command=self.decrypt_command)
self.menuBar.add_cascade(label="Encrypt", menu=self.encryptMenu)
parent.config(menu=self.menuBar)
def open_command(self):
file = tkFileDialog.askopenfile()
if file != None:
contents = file.read()
self.textWidget.delete("1.0", Tkinter.END)
self.textWidget.insert(Tkinter.END, contents)
def save_command(self):
file = tkFileDialog.asksaveasfile()
if file != None:
contents = self.textWidget.get("1.0", Tkinter.END)[:-1]
file.write(contents)
file.close()
def exit_program(self):
self.parent.destroy()
236
def encrypt_command(self):
password = tkSimpleDialog.askstring("Password", "Please Enter your Password")
if password != None:
contents = self.textWidget.get("1.0", Tkinter.END)[:-1]
encrypted = self.encrypt(contents, password)
self.textWidget.delete("1.0", Tkinter.END)
self.textWidget.insert(Tkinter.END, encrypted)
def decrypt_command(self):
password = tkSimpleDialog.askstring("Password", "Please Enter your Password")
if password != None:
contents = self.textWidget.get("1.0", Tkinter.END)[:-1]
encrypted = self.decrypt(contents, password)
self.textWidget.delete("1.0", Tkinter.END)
self.textWidget.insert(Tkinter.END, encrypted)
if __name__ == "__main__":
root = Tkinter.Tk()
textApp = EncryptTextEdit(root)
root.mainloop()
23.3 Glossary
cipher: An encryption algorithm.
ASCII: American Standard Code for Information Interchange. A common representation of text characters.
Unicode: A more modern representation of text characters that allows for a much larger range of characters
and includes characters from many different languages.
modulo arithmetic: arithmetic performed on a limited range of numbers.
237
23.4 Laboratory Exercises
1. A Caesar cipher is trivially easy to break as there are only N different ways of encrypting a given
text, where N is the number of characters in the alphabet. From the Blackboard page, download
caesar crypt.txt and save it to your home directory. caesar crypt.txt has been encrypted
using the caesarEncrypt function. Using the program above as a starting point, write a function
that tests each possible rotateBy value to decrypt the text.
2. A Caesar cipher is the simplest of substitution ciphers. A more general substitution cipher allows any
mapping from input character to output character, not just a rotation. So, for example, I might use the
following substitution map:
Source A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Target P J T Y Q R C K L E W D U A X O B V S G H I M N F Z
The number of different possible mappings is N × (N − 1) × (N − 2) × ...1. You can see this is true
by noting that there are N possible substitutions for the first character (e.g. can choose any of A to
Z), N − 1 for the second character (e.g. any of A to Z except for the first substitution) and so on. As
you probably know, this function is called factorial. Implement a factorial function and work out the
number of different possible mappings when N = 126. Make sure you remember to include doctests
to test your function.
Do you think the method you used in Question 1 will work for this case?
Show your work to a demonstrator here to make sure you are on the right track.
3. One way of implementing a substitution cipher is to use a dictionary that maps from an input char-
acter randomly to an output character. The following code can be used to construct the dictionary:
import random
keys = range(1,127)
values = range(1,127)
random.seed("SomePassword")
random.shuffle(values)
encrypt = dict()
decrypt = dict()
for (i,j) in zip(keys,values):
encrypt[chr(i)] = chr(j)
decrypt[chr(j)] = chr(i)
Encryption and decryption are then trivial. Instead of the call to caesarEncrypt or caesarDecrypt
in the encryption program, we can have the following code:
ch = encrypt[ch]
Using the above code, or code of your own design, incorporate a random substitution cipher into the
encryption program.
Note: The zip function takes two sequences and returns a list of tuples. The above for loop is a
typical way of iterating through two equal length sequences.
238
4. The statement random.seed("SomePassword") seeds the random number generator so that random.shuffle
produces the same shuffle each time the program is run. I have used a string constant as a seed in
the code for the previous question, but this means that all documents encrypted with a substitution
cipher will use the same substitution. Modify your answer to the previous question to allow the user
to enter a password for specifying the seed value.
Extension Exercises
1. Substitution ciphers can be broken using frequency analysis. Frequency analysis examines the num-
ber of times each character occurs in the encrypted text and makes guesses regarding the identity
of the character based on its relative frequency. For example, “e” is the most common letter in En-
glish prose, therefore we would guess that the most common character in the encrypted text is “e”.
For more details about frequency analysis, have a look at: http://en.wikipedia.org/wiki/
Frequency_analysis. From the Blackboard page, download the cipher text substitution encrypt.txt
and see if you can decrypt it using frequency analysis.
239
240
Lesson 24
What Next?
In this lecture, I will be giving a summary of the course, outlining various aspects of the exam, and giving
you an idea of some typical sort of exam questions.
• recursion
• exceptions
• lambda functions
• list comprehensions
• heaps of stuff from the standard library
I would encourage all of you to find out as much about those things as you can.
24.2 What you can expect from COMP160 and Computer Science
If you enjoyed COMP150 then I would encourage you to continue on to COMP160 and even a major or
minor in Computer Science. There has been a shortage of skilled computer scientists in the work-force for
some time now, and that seems unlikely to change in the foreseeable future. There are plenty of jobs, and
let’s face it, programming is just fun! I’m often amazed that people get paid doing this stuff.
COMP160 has quite a different focus to COMP150. For a start, it uses the programming language Java.
Java is one of the most popular programming languages in use according to the TIOBE Programming
Community Index, and has been for some time. Aside from learning how to program in Java, COMP160
focuses less on using high level language tools (such as lists and dictionaries), and more on fundamental
problem solving in an Object-Oriented language. This process continues into second and third year with
the focus on fundamental concepts of computing. So if you want to know how to implement a list or a
dictionary efficiently, or how artificial intelligence, computer graphics or databases work, then Computer
Science is a good option for you.
241
242
GNU Free Documentation License
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is
not allowed.
Preamble
The purpose of this License is to make a manual, textbook, or other functional and useful document “free”
in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without
modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author
and publisher a way to get credit for their work, while not being considered responsible for modifications
made by others.
This License is a kind of “copyleft”, which means that derivative works of the document must themselves
be free in the same sense. It complements the GNU General Public License, which is a copyleft license
designed for free software.
We have designed this License in order to use it for manuals for free software, because free software needs
free documentation: a free program should come with manuals providing the same freedoms that the
software does. But this License is not limited to software manuals; it can be used for any textual work,
regardless of subject matter or whether it is published as a printed book. We recommend this License
principally for works whose purpose is instruction or reference.
243
Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.)
The relationship could be a matter of historical connection with the subject or with related matters, or of
legal, commercial, philosophical, ethical or political position regarding them.
The “Invariant Sections” are certain Secondary Sections whose titles are designated, as being those of
Invariant Sections, in the notice that says that the Document is released under this License. If a section does
not fit the above definition of Secondary then it is not allowed to be designated as Invariant. The Document
may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there
are none.
The “Cover Texts” are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover
Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be
at most 5 words, and a Back-Cover Text may be at most 25 words.
A “Transparent” copy of the Document means a machine-readable copy, represented in a format whose
specification is available to the general public, that is suitable for revising the document straightforwardly
with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some
widely available drawing editor, and that is suitable for input to text formatters or for automatic translation
to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent
file format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent
modification by readers is not Transparent. An image format is not Transparent if used for any substantial
amount of text. A copy that is not “Transparent” is called “Opaque”.
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input
format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming sim-
ple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats
include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only
by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not gen-
erally available, and the machine-generated HTML, PostScript or PDF produced by some word processors
for output purposes only.
The “Title Page” means, for a printed book, the title page itself, plus such following pages as are needed
to hold, legibly, the material this License requires to appear in the title page. For works in formats which
do not have any title page as such, “Title Page” means the text near the most prominent appearance of the
work’s title, preceding the beginning of the body of the text.
A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or
contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for a
specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”,
or “History”.) To “Preserve the Title” of such a section when you modify the Document means that it
remains a section “Entitled XYZ” according to this definition.
The Document may include Warranty Disclaimers next to the notice which states that this License applies
to the Document. These Warranty Disclaimers are considered to be included by reference in this License,
but only as regards disclaiming warranties: any other implication that these Warranty Disclaimers may
have is void and has no effect on the meaning of this License.
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, pro-
vided that this License, the copyright notices, and the license notice saying this License applies to the
Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this
License. You may not use technical measures to obstruct or control the reading or further copying of the
copies you make or distribute. However, you may accept compensation in exchange for copies. If you
distribute a large enough number of copies you must also follow the conditions in section 3.
You may also lend copies, under the same conditions stated above, and you may publicly display copies.
244
3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers) of the Document,
numbering more than 100, and the Document’s license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover,
and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the
publisher of these copies. The front cover must present the full title with all words of the title equally
prominent and visible. You may add other material on the covers in addition. Copying with changes
limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be
treated as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as
many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either
include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque
copy a computer-network location from which the general network-using public has access to download
using public-standard network protocols a complete Transparent copy of the Document, free of added
material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution
of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated
location until at least one year after the last time you distribute an Opaque copy (directly or through your
agents or retailers) of that edition to the public.
It is requested, but not required, that you contact the authors of the Document well before redistributing
any large number of copies, to give them a chance to provide you with an updated version of the Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3
above, provided that you release the Modified Version under precisely this License, with the Modified Ver-
sion filling the role of the Document, thus licensing distribution and modification of the Modified Version
to whoever possesses a copy of it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from
those of previous versions (which should, if there were any, be listed in the History section of the
Document). You may use the same title as a previous version if the original publisher of that version
gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the
modifications in the Modified Version, together with at least five of the principal authors of the Doc-
ument (all of its principal authors, if it has fewer than five), unless they release you from this require-
ment.
C. State on the Title page the name of the publisher of the Modified Version, as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices.
F. Include, immediately after the copyright notices, a license notice giving the public permission to use
the Modified Version under the terms of this License, in the form shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the
Document’s license notice.
H. Include an unaltered copy of this License.
245
I. Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the
title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no
section Entitled “History” in the Document, create one stating the title, year, authors, and publisher
of the Document as given on its Title Page, then add an item describing the Modified Version as stated
in the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to a Transparent copy
of the Document, and likewise the network locations given in the Document for previous versions it
was based on. These may be placed in the “History” section. You may omit a network location for a
work that was published at least four years before the Document itself, or if the original publisher of
the version it refers to gives permission.
K. For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section,
and preserve in the section all the substance and tone of each of the contributor acknowledgements
and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section
numbers or the equivalent are not considered part of the section titles.
M. Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified
Version.
N. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invari-
ant Section.
O. Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections
and contain no material copied from the Document, you may at your option designate some or all of these
sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s
license notice. These titles must be distinct from any other section titles.
You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your
Modified Version by various parties–for example, statements of peer review or that the text has been ap-
proved by an organization as the authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-
Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover
Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If
the Document already includes a cover text for the same cover, previously added by you or by arrangement
made by the same entity you are acting on behalf of, you may not add another; but you may replace the old
one, on explicit permission from the previous publisher that added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names
for publicity for or to assert or imply endorsement of any Modified Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License, under the terms de-
fined in section 4 above for modified versions, provided that you include in the combination all of the
Invariant Sections of all of the original documents, unmodified, and list them all as Invariant Sections of
your combined work in its license notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and multiple identical Invariant Sections
may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different
contents, make the title of each such section unique by adding at the end of it, in parentheses, the name
246
of the original author or publisher of that section if known, or else a unique number. Make the same
adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled “History” in the various original documents,
forming one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and
any sections Entitled “Dedications”. You must delete all sections Entitled “Endorsements”.
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released under this License,
and replace the individual copies of this License in the various documents with a single copy that is in-
cluded in the collection, provided that you follow the rules of this License for verbatim copying of each of
the documents in all other respects.
You may extract a single document from such a collection, and distribute it individually under this License,
provided you insert a copy of this License into the extracted document, and follow this License in all other
respects regarding verbatim copying of that document.
8. TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under
the terms of section 4. Replacing Invariant Sections with translations requires special permission from their
copyright holders, but you may include translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections. You may include a translation of this License, and all the
license notices in the Document, and any Warranty Disclaimers, provided that you also include the original
English version of this License and the original versions of those notices and disclaimers. In case of a
disagreement between the translation and the original version of this License or a notice or disclaimer, the
original version will prevail.
If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement
(section 4) to Preserve its Title (section 1) will typically require changing the actual title.
9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under
this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will
automatically terminate your rights under this License. However, parties who have received copies, or
rights, from you under this License will not have their licenses terminated so long as such parties remain
in full compliance.
247
10. FUTURE REVISIONS OF THIS LICENSE
The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License
from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail
to address new problems or concerns. See http://www.gnu.org/copyleft/.
Each version of the License is given a distinguishing version number. If the Document specifies that a
particular numbered version of this License “or any later version” applies to it, you have the option of
following the terms and conditions either of that specified version or of any later version that has been
published (not as a draft) by the Free Software Foundation. If the Document does not specify a version
number of this License, you may choose any version ever published (not as a draft) by the Free Software
Foundation.
Copyright © YEAR YOUR NAME. Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation License, Version 1.2 or any later
version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover
Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU
Free Documentation License”.
If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the “with . . . Texts.” line
with this:
with the Invariant Sections being LIST THEIR TITLES, with the Front-Cover Texts being LIST,
and with the Back-Cover Texts being LIST.
If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those
two alternatives to suit the situation.
If your document contains nontrivial examples of program code, we recommend releasing these examples
in parallel under your choice of free software license, such as the GNU General Public License, to permit
their use in free software.
248
Index
249
frame, 38 mapping type, 159
fruitful functions, 55 method, 77, 167, 171
function, 28 methods, 168
function call, 28, 29 mode, 89
function definition, 28 modifiers, 145
functional programming style, 146 module, 63
modulo arithmetic, 234
generalize, 114 modulus operator, 21
glob, 219 multiple assignment, 99
Graphical User Interface (GUI), 187 mutable, 138, 155
header, 29 named parameter, 171
high-level language, 1 Natural languages, 5
IDE, 11 nested, 48, 133
identifier, 143 newline, 104
IDLE, 11 non-volatile, 89
if statement, 45 None, 56
immutable, 75, 155
object code, 2
increment, 100
object-oriented programming, 167
incremental development, 57
object-oriented programming language, 167
index, 73
objects, 143, 167
infinite loop, 101
odd, 61
information hiding, 180
OOP, 167
initialization, 168
operands, 21
initialize, 100
operator overloading, 176
instances, 167
Operators, 21
instantiated, 168
optional parameter, 127
int, 17
integer, 17 parameters, 31
integer division, 21 parsing, 5
interface, 175 path, 92
interpreters, 2 portable, 1
invocation, 160 presentation, 91
item assignment, 138 prime number, 112, 122
iteration, 101 print statement, 6
problem solving, 1
Jython, 217
procedural programming, 167
key-value pairs, 159 program, 3
keys, 159 programming paradigm, 167
keywords, 19 pure function, 145
Python prompt, 2
len, 28 Python shell, 2
list, 80, 91, 133
list traversal, 135 RAM, 89
local, 116 refactoring, 213
local variable, 37 regular expressions, 222
logical operators, 46 representation, 91
loop, 101 return value, 28, 56
loop variable, 114 rules of precedence, 22
low-level languages, 1
scaffolding, 58
main, 31 script, 2
250
semantic error, 4 temporary variables, 56
sequence, 28 tokens, 5
sequences, 133 trace, 102
shallow equality, 174 traceback, 39
shallow copying, 181 traversal, 76
side effects, 145 tuple, 155
slice, 74 tuple assignment, 156
source code, 2 types, 17
stack diagram, 38
state diagram, 18 Unicode, 234
statement, 20 unit testing, 65
step size, 135
value, 17
str, 17
Variable names, 19
string, 17 variables, 18
Syntax, 4 virtual machine, 2
syntax error, 4 volatile, 89
251