11.3 Part III Exercises
Now that you know how to code basic program logic, the exercises ask
you to implement some simple tasks with statements. Most of the work
is in exercise 4, which lets you explore coding alternatives. There
are always many ways to arrange statements, and part of learning
Python is learning which arrangements work better tHan others.
See Section B.3 for the solutions.
Coding basic loops. Write a for loop that prints the ASCII code of
each character in a string named S. Use the
built-in function ord(character) to convert each
character to an ASCII integer. (Test it interactively to see how it
works.) Next, change your loop to compute the sum of the ASCII codes of all
characters in a string. Finally, modify your code again to return a new list that contains
the ASCII codes of each character in the string. Does this expression
have a similar effect—map(ord,S)? (Hint: see
Part IV.)
Backslash characters. What happens on your
machine when you type the following code interactively? for i in range(50):
print 'hello %d\n\a' % i Beware that if run outside of the IDLE interface, this example may
beep at you, so you may not want to run it in a crowded lab. IDLE
prints odd characters instead (see the backslash escape characters in
Table 5-2). Sorting dictionaries. In Chapter 6, we saw that dictionaries are unordered
collections. Write a for loop that prints a
dictionary's items in sorted (ascending) order.
Hint: use the dictionary keys and list
sort methods. Program logic alternatives. Consider the
following code, which uses a while loop and
found flag to search a list of powers of 2, for
the value of 2 raised to the 5th power (32). It's
stored in a module file called power.py. L = [1, 2, 4, 8, 16, 32, 64]
X = 5
found = i = 0
while not found and i < len(L):
if 2 ** X == L[i]:
found = 1
else:
i = i+1
if found:
print 'at index', i
else:
print X, 'not found'
C:\book\tests> python power.py
at index 5 As is, the example doesn't follow normal Python
coding techniques. Follow the steps below to improve it. For all the
transformations, you may type your code interactively or store it in
a script file run from the system command line (using a file makes
this exercise much easier). First, rewrite this code with a while loop
else, to eliminate the found
flag and final if statement. Next, rewrite the example to use a for loop with
an else, to eliminate the explicit list indexing
logic. Hint: to get the index of an item, use the list
index method (L.index(X)
returns the offset of the first X in list
L). Next, remove the loop completely by rewriting the examples with a
simple in operator membership expression. (See
Chapter 6 for more details, or type this to test:
2 in [1,2,3].) Finally, use a for loop and the list
append method to generate the powers-of-2 list
(L), instead of hard-coding a list literal. Deeper thoughts: (1) Do you think it would improve performance to
move the 2**X expression outside the loops? How
would you code that? (2) As we saw in Exercise 1, Python also
includes a map(function, list) tool that can
generate the powers-of-2 list too: map(lambda x: 2**x,
range(7)). Try typing this code interactively;
we'll meet lambda more formally
in Chapter 14.
|