VI.
FIRST STEPS TOWARDS PROGRAMMING
Computers
are wonderful for the simple reason that they do really boring things over and
over and over again … and they never get bored! You
can ask a computer to do a task a million times and it won’t even stick
out its tongue at you. Here’s an example: Say you want to look up
everyone in the phonebook named “Smith” and give them a call to
sell them some spam. (You are a Spam-seller and you know that people named
Smith are known to just love Spam.) This would be really boring and tiring if
you had to do it by hand, but you could write a program to look through all the
names and pick out the phone numbers of everyone named Smith.
In
order to be able to do tasks like this, you first need to know how to write
some basic programming statements. The basic statements that
you will be using all the time in Python are: if statements,
while loops and for loops.
>>> name1 = ‘Smith’
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a:
... print x, len(x)
...
cat 3
window 6
defenestrate 12
It is not safe to modify the
sequence being iterated over in the loop (this can only happen for mutable
sequence types, i.e., lists). If you need to modify the list you are iterating
over, e.g., duplicate selected items, you must iterate over a copy. The slice
notation makes this particularly convenient:
>>> for x in a[:]: # make a slice copy of the
entire list
...
if len(x) > 6: a.insert(0, x)
...
>>> a
['defenestrate', 'cat', 'window', 'defenestrate']
4.3 The range() Function
If you do need to iterate
over a sequence of numbers, the built-in function range() comes in handy. It
generates lists containing arithmetic progressions, e.g.:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The given end point is never
part of the generated list; range(10) generates a list of 10 values, exactly the legal
indices for items of a sequence of length 10. It is possible to let the range
start at another number, or to specify a different increment (even negative;
sometimes this is called the `step'):
>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(-10, -100, -30)
[-10, -40, -70]
To iterate over the indices
of a sequence, combine range() and len() as follows:
>>> a = ['Mary', 'had', 'a', 'little',
'lamb']
>>> for i in range(len(a)):
... print i, a[i]
...
0 Mary
1 had
2 a
3 little
4 lamb
4.4 break and continue Statements, and else Clauses on Loops
The break statement, like in
C, breaks out of the smallest enclosing for or while loop.
The continue statement, also
borrowed from C, continues with the next iteration of the loop.
Loop statements may have an else clause; it is executed when the loop terminates
through exhaustion of the list (with for) or when the condition becomes false
(with while), but not when the loop is terminated by a break statement. This is
exemplified by the following loop, which searches for prime numbers:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
...
print n, 'equals', x, '*', n/x
...
break
... else:
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
4.5 pass Statements
The pass statement does
nothing. It can be used when a statement is required syntactically but the
program requires no action. For example:
>>> while 1:
... pass # Busy-wait for keyboard
interrupt
...
>>>
b = 1
>>>
while b < 10:
... print b
... b = b + 1
...
1
2
3
4
5
6
7
8
9
>>>