Unit 2
Learn something new
1. Try the following program. The first print statement uses a function called len to find
out the length of the string refe
ed to by line. The output is 30, because there are 30
characters in the string; note that letters, spaces and punctuation all count as
characters. A function is a prepackaged set of instructions; in this case it’s one built
in to Python to determine how many items are in a collection.
line = 'This is a sample line of text.'
print(len(line))
print(line.split())
print(len(line.split()))
The second print statement uses a function called split. It divides a string into parts,
eaking it at every series of space characters or other characters like tabs that display
as blanks. The result—as you can see from the output you get by running the
program—is a list of smaller strings. The split function is extremely handy when we
want to process words within a long string.
Notice that both len and split are followed by parentheses. When we call a
function—use it—we pass it the information it needs to ca
y out its work by placing
the information inside these parentheses. In the first print statement, we pass line to
len. The len function computes the result and returns its answer, 30. This answer
essentially replaces the function call. Writing print(len(line)) has exactly the same
effect here as writing print(30).
Note that print is also a function. In the same statement, we pass len(line), or 30, to
print. The print function requires this information to ca
y out its job; it needs to
know what to display.
In the last line of the program, we call the split function on line. The split function
eturns a list of words, which in turn is passed to len. The len function counts the
number of items in the list and returns the answer, 7, passing this number to print
which displays it. As a result, we find out how many words are in the sample text.
One last point to note is that we call the split function by writing line.split(), not
split(line). The reason is that split is not a general function like len that works on
many kinds of objects—lists, strings, etc. Instead it is specifically associated with
strings. We say it is a string method. To call a string method, we connect it to any
existing string with a dot (.). Strings, lists and other data types in Python each come
with an extensive set of methods to ca
y out tasks naturally associated with the kind
of data they store.
2. Integers are one data type we have already seen and this data type comes with the
methods you would expect for numbers: addition, multiplication, etc. We can use
these methods using the dot notation just introduced, but since they’re so common,
Python provides a much easier way. Try the following:
print(2+3)
print(71*(5-2))
print(36/3)
print(2**1000)
This displays 2 plus 3, 71 times 3, 36 divided by 3 and 21000 (that’s 2 times itself
1,000 times). The last answer is 302 digits long, but no problem for Python. Python
follows the standard rules you learned in math class, doing multiplications and
divisions before additions and subtractions; we can use parentheses, as in the second
line, to force a different order if we like.
If we use names for numbers, we can sometimes make calculations clearer.
numberOfBoxes = 4
itemsPerBox = 20
print('Total items:', numberOfBoxes * itemsPerBox)
3. Now try the following.
x = 4
print(x*2)
print(x)
x = x*2
print(x)
The first line assigns the name x to the integer 4. The next line calculates 4 times 2
and displays the answer, 8. Note carefully that x is still a name for 4, as the second
print demonstrates. Using a name does not change what it refers to.
If we want x to refer to a new value, we have to place it on the left of the equals sign
that Python uses for assignment. The fourth line first uses the cu
ent value of x and
computes 4 times 2. It then reassigns x to refer to the answer, 8.
This is a good time to stress again that the equals sign means something very different
in Python than in a math class. In alge
a, x=x*2 is a statement that x has the same
2
value as a number twice as large. This means x must be zero. Writing the same thing
in Python means to ca
y out a calculation using the cu
ent value of x and then to let
x refer to the answer. It certainly does not tell us that x must be zero. 1
4. Reassigning a name based on its cu
ent value is such a common operation that
Python provides a shortcut. Rather than writing x = x*2 to assign x to a value twice
as large as its cu
ent one, we can write just x *= 2. Likewise, if we want to reassign
x to a value one larger, we can write x = x+1, but Python allows us to shorten this to x
+= 1.
5. Let’s use what we know to analyze some lines of text. We can write much more
useful programs if we take our text from a file rather than from strings typed in by
hand. The file pap.txt contains the complete novel Pride and Prejudice by Jane
Austen. Make sure this file is in the same folder as your program file and then run
the following program.
with open('pap.txt') as book:
for line in book:
if 'property' in line:
print(line)
The output is a series of eight lines, all the lines in the file pap.txt that include the
word ‘property’. The first line creates a file object associated with pap.txt and allows
us to refer to it with the name book. Like a list or string, a file object can be used as
the ordered collection in a for statement. The simplest way, as in our example,
assigns a name (like line) one by one to a series of strings, one for each line of the
file. The rest of this program uses nothing new. It checks if the cu
ent line contains
the word ‘property’ and, if so, displays it.
The with statement creates a connection with a file, maintains it while the indented
instructions are ca
ied out and then
eaks it. We indent only statements that need to
use the file.
6. Next, we’ll run through the same file and count the total number of words used.
Here’s the program. When you run it, the output should be 121555.
1 If you’ve programmed in a language like C++ or Java, you should note that Python assignment is also
quite different from what you’re used to. In one of these languages, a name like x refers to a location in
memory where something is stored. When we write x=x*2, the x goes on refe
ing to the same memory
location, but the contents are changed to a new value twice as large. In Python, the same statement causes
x to refer to a new object in an entirely different part of memory. If the original object refe
ed to by x is
no longer needed for other purposes, the system reclaims the memory for reuse. As a result, we can write
x=4 and then, later, x='hello'. This would be illegal in C++ or Java, since the memory refe
ed to
originally is just large enough for an integer and cannot hold a string.
3
count = 0
with open('pap.txt') as book:
for line in book:
count += len(line.split())
print('Word count:', count)
We use the name count to refer to the total number of words seen so far. Before we
look at the file, it starts at zero. After creating a file object, we run through the lines
one by one. For each line, we use split to get a list of individual words, pass the
esult to len to find out how many words are in the list and update count, increasing it
y this value. For example, if we have seen 900 words so far and the cu
ent line is
the string ‘just an example’, then line.split will return [‘just’, ‘an’, ‘example’], meaning
that len(line.split()) will return 3. The effect is as if we had written count = count+3,
eassigning count to the value 903. 2
7. With a slight variation, we can see how many times any particular word is used in the
ook. Here’s a program that counts occu
ences of the word ‘the’. Run it and you’ll
find that the file contains this word 4,047 times.
count = 0
with open('pap.txt') as book:
for line in book:
for word in line.split():
if word == 'the':
count += 1
print("Number of times 'the' is used:", count)
This program uses just two new techniques. First, to check if two values are the
same, we use == in Python. When you see this in a program, you should read it as
equals . So the if statement here says: if word equals 'the', the value refe
ed to by
count should increase by one . Again, = means something completely different in
Python and you should not say equals when you read it.
The second new point is the use of double quotes (") in the last line. We normally
enclose a string with single quotes ('), but here that would cause a problem since we
use single quotes within the string.
The program itself is easy to understand. We go one by one through the lines in the
file. Within each line, we go one by one through the words. Each time we find the
word ‘the’, we count up by one.
2 The actual number of words is slightly different than what this program reports, because we’ve defined
words to be just strings of characters su
ounded by blanks. This means that in the string ‘The
mandropping his gunran out the door’, the substring ‘gunran’ will be counted as one word.
4
Note that the following will not work.
count = 0
with open('pap.txt') as book:
for line in book:
if 'the' in line:
count += 1
print("Number of times 'the' is used:", count)
This program has two e
ors. First, no matter how many times ‘the’ appears in a line,
count will be increased only once. Second, count will be increased inco
ectly if the
word ‘then’ or ‘other’ is included in a line, since each of these contains the word
‘the’. 3
8. Finally, here’s a program that finds the line with most words in it. The only thing
technically new here is the use of the symbol > to mean ‘greater than’. We can also
write < for ‘less than’ and >= and <= for ‘greater than or equal’ and ‘less than or
equal.’
More important, though, is the use of a standard programming technique. As we
proceed, we keep track of the most extreme example so far—in our case the line
we’ve seen with the most words. We compare each new example to the one we’re
saving and update only when we have a new champion.
maxcount = 0
with open('pap.txt') as book:
for line in book:
count = len(line.split())
if count>maxcount:
maxline = line
maxcount = count
print(maxline)
Here maxcount keeps track of the number of words in the longest line encountered at
any time. Naturally, it’s zero before we begin to look at any lines. For each new line,
we let count refer to the number of words it contains. If count is bigger than
maxcount, we have a new winner. In this case, we remember the line and the new
maximum number of words using as maxline and maxcount.
3 Our original version has some problems too. It will not count occu
ences of ‘The’ with a capital T or
occu
ences followed by a punctuation mark: ‘…only theextremely patientwomen understand….’
5
9. By now, we’ve used for statements to go character by character, word by word and
line by line. How does Python know which we mean? The answer is simple, but it’s
worth thinking through carefully, because misunderstanding this point is one of the
most common causes of e
ors.
When we write a for statement, Python looks at the target—the last thing on the
header line—to see what sort of object it is. It then uses the type of the target to
decide how to go one by one through it.
If the target is a list , for will go item by item through the items that make up the list.
So this code
target = ['here', 'are', 'four', 'words']
for item in target:
print(item)
will yield the output
here
are
four
words
When the target is a string , Python automatically goes character by character through
the string. So this code
target = 'here are four words'
for item in target:
print(item)
will yield the output
h
e
e
a
e
f
6
o
u
w
o
d
s
The split function
eaks up a string into a list of strings. So if we have
target = 'here are four words'
then target.split() is a list that looks like this:
['here', 'are', 'four', 'words']
And when we use target.split() as the target in a for statement, Python will go one by
one through the items in this list, that is word by word through the words in the
original string.
This code
target = 'here are four words'
for item in target.split():
print(item)
will yield the output
here
are
four
words
This is exactly the same as in our first example. In either case, the for statement is
going through a list item by item.
Now what happens when the target of a for statement is a file object ? In this case,
Python automatically goes one by one through the lines in the file.
Suppose we have a file called sample.txt, which consists of the following two lines:
7
here are four words
Fred Ted Ned
Then this code
with open('sample.txt') as file:
for item in file:
print(item)
will set item one by one to each of the lines in the file. The output looks like this:
here are four words
Fred Ted Ned
Each line taken from the file is a string. For example, when this last program is
working with the first line of the file, it is exactly as if we had written
item = 'here are four words'
Since item is a string, we can use it as one. In particular, we can go through it letter
y letter—by using it as a target—or we can use split to
eak it up into a list of
words and go through those. Here’s code to go one by one through the lines in the
file and, for each line, go character by character:
with open('sample.txt') as file:
for item in file: # item is a string, holding
for character in item: # a full line from the file
print(character)
Here’s the output:
h
e
e
a
e
8
f
o
u
w
o
d
s
F
e
d
T
e
d
N
e
d
And here’s code to go word by word:
with open('sample.txt') as file:
for item in file: # item is a string
for word in item.split(): # item.split() is a list
print(word)
Here’s the output:
here
are
four
words
Fred
Ted
Ned
9
Unit 2
Apply what you know
0. Study the programs in the Learn something new section until you can write them
yourself from scratch without relying on this document or any other source of
information. Here are the programs:
0.1. List the lines in the file pap.txt that contain the word ‘property’.
0.2. Count the number of words in the file pap.txt.
0.3. Count the number of times the word ‘the’ appears in the file pap.txt.
0.4. Find and display the line in the file pap.txt containing the most words.
1. Write a program that counts the number of times the lowercase letter ‘e’ is used in the
file pap.txt.
2. Write a program that checks itself and reports the number of lines of the program that
contain the word ‘for’, either alone or as part of another word.
3. Write a program to determine which word is the shortest of the following: apple,
anana, peach, plum, grapefruit.
4. Write a program to determine the average of the numbers given in a list. The first
line of your program should give a name to a list of numbers to be averaged: e.g.
numbers = [3, 17, 1, 44, 239].
5. The list method append changes a list by adding an item at the end. For example if
students refers to the list ['Ed' 'Ted', 'Fred'], then after calling
students.append('Jennifer') the list will be ['Ed' 'Ted', 'Fred', 'Jennifer']. Write a
program using this method to print a list of the lengths of the words given in a list.
The first line of your program should give a name to the list of words, e.g. students =
['Ed', 'Ted', 'Fred', 'Jennifer']. For this example, the output would be [2, 3, 4, 8].
6. The builtin function input displays a string on the screen and returns a string
containing what the user types in response. For example, the statement answer =
input('How are you feeling? ') will display the given question and wait for the user to
type something and press enter. Then answer will be assigned to a string that holds
what the user has typed. Write a program using input that asks the user to type in any
number of words and then reports the maximum number of vowels contained in a
single word, e.g. ‘please’ is a sixletter word containing three vowels.
XXXXXXXXXXALICE'S ADVENTURES IN WONDERLAND
XXXXXXXXXXLewis Ca
oll
XXXXXXXXXXTHE MILLENNIUM FULCRUM EDITION 3.0
XXXXXXXXXXCHAPTER I
XXXXXXXXXXDown the Ra
it-Hole
Alice was beginning to get very tired of sitting by her siste
on the bank, and of having nothing to do: once or twice she had
peeped into the book her sister was reading, but it had no
pictures or conversations in it, `and what is the use of a book,'
thought Alice `without pictures or conversation?'
So she was considering in her own mind (as well as she could,
for the hot day made her feel very sleepy and stupid), whethe
the pleasure of making a daisy-chain would be worth the trouble
of getting up and picking the daisies, when suddenly a White
Ra
it with pink eyes ran close by her.
There was nothing so VERY remarkable in that; nor did Alice
think it so VERY much out of the way to hear the Ra
it say to
itself, `Oh dear! Oh dear! I shall be late!' (when she thought
it over afterwards, it occu
ed to her that she ought to have
wondered at this, but at the time it all seemed quite natural);
ut when the Ra
it actually TOOK A WATCH OUT OF ITS WAISTCOAT-
POCKET, and looked at it, and then hu
ied on, Alice started to
her feet, for it flashed across her mind that she had neve
efore seen a ra
it with either a waistcoat-pocket, or a watch to
take out of it, and burning with curiosity, she ran across the
field after it, and fortunately was just in time to see it pop
down a large ra
it-hole under the hedge.
In another moment down went Alice after it, never once
considering how in the world she was to get out again.
The ra
it-hole went straight on like a tunnel for some way,
and then dipped suddenly down, so suddenly that Alice had not a
moment to think about stopping herself before she found herself
falling down a very deep well.
Either the well was very deep, or she fell very slowly, for she
had plenty of time as she went down to look about her and to
wonder what was going to happen next. First, she tried to look
down and make out what she was coming to, but it was too dark to
see anything; then she looked at the sides of the well, and
noticed that they were filled with cupboards and book-shelves;
here and there she saw maps and pictures hung upon pegs. She
took down a jar from one of the shelves as she passed; it was
labelled `ORANGE MARMALADE', but to her great disappointment it
was empty: she did not like to drop the jar for fear of killing
somebody, so managed to put it into one of the cupboards as she
fell past it.
`Well!' thought Alice to herself, `after such a fall as this, I
shall think nothing of tumbling down stairs! How
ave they'll
all think me at home! Why, I wouldn't say anything about it,
even if I fell off the top of the house!' (Which was very likely
true.)
Down, down, down. Would the fall NEVER come to an end! `I
wonder how many miles I've fallen by this time?' she said aloud.
`I must be getting somewhere near the centre of the earth. Let
me see: that would be four thousand miles down, I think--' (for,
you see, Alice had learnt several things of this sort in he
lessons in the schoolroom, and though this was not a VERY good
opportunity for showing off her knowledge, as there was no one to
listen to her, still it was good practice to say it over) `--yes,
that's about the right distance--but then I wonder what Latitude
or Longitude I've got to?' (Alice had no idea what Latitude was,
or Longitude either, but thought they were nice grand words to
say.)
Presently she began again. `I wonder if I shall fall right
THROUGH the earth! How funny it'll seem to come out among the
people that walk with their heads downward! The Antipathies, I
think--' (she was rather glad there WAS no one listening, this
time, as it didn't sound at all the right word) `--but I shall
have to ask them what the name of the country is, you know.
Please, Ma'am, is this New Zealand or Australia?' (and she tried
to curtsey as she spoke--fancy CURTSEYING as you're falling
through the air! Do you think you could manage it?) `And what
an ignorant little girl she'll think me for asking! No, it'll
never do to ask: perhaps I shall see it written up somewhere.'
Down, down, down. There was nothing else to do, so Alice soon
egan talking again. `Dinah'll miss me very much to-night, I
should think!' (Dinah was the cat.) `I hope they'll remembe
her saucer of milk at tea-time. Dinah my dear! I wish you were
down here with me! There are no mice in the air, I'm afraid, but
you might catch a bat, and that's very like a mouse, you know.
But do cats eat bats, I wonder?' And here Alice began to get
ather sleepy, and went on saying to herself, in a dreamy sort of
way, `Do cats