I l@ve RuBoard

### 3.9 Exercises

Now that you know how to code basic program logic, this session asks 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.

1. Coding basic loops.

1. 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).

2. Next, change your loop to compute the sum of the ASCII codes of all characters in a string.

3. 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?TT CLass="monofont">map(ord, S)? (Hint: see Chapter 4.)

2. 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```

Warning: this example beeps at you, so you may not want to run it in a crowded lab (unless you happen to enjoy getting lots of attention). Hint: see the backslash escape characters in Table 2.6.

3. Sorting dictionaries. In Chapter 2, 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.

4. 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 power 5 (32). It's stored in a module file called power.py.

```L = [1, 2, 4, 8, 16, 32, 64]
X = 5

found = i = 0
if 2 ** X == L[i]:
found = 1
else:
i = i+1

if found:
print 'at index', i
else:

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 (though using a file makes this exercise much easier).

1. First, rewrite this code with a while loop else, to eliminate the found flag and final if statement.

2. 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).

3. Now, remove the loop completely by rewriting the examples with a simple in operator membership expression (see Chapter 2 for more details, or type this: 2 in [1,2,3]).

4. Finally, use a for loop and the list append method to generate the powers-of-2 list (L), instead of hard-coding a list constant.

5. 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, as follows: map(lambda x: 2**x, range(7)). Try typing this code interactively; we'll meet lambda more formally in Chapter 4.

 I l@ve RuBoard