Tải bản đầy đủ - 0 (trang)
Appendix B. Solutions to End-of-Part Exercises

Appendix B. Solutions to End-of-Part Exercises

Tải bản đầy đủ - 0trang

search path. The .pyc file is written automatically if Python has access to the source

file’s directory; it contains the compiled byte code version of a module. See Chapter 3 for more on modules.

4. Scripts. Assuming your platform supports the #! trick, your solution will look like

the following (although your #! line may need to list another path on your



print('Hello module world!')

% chmod +x module1.py

(or #!/usr/bin/env python)

% module1.py

Hello module world!

5. Errors. The following interaction (run in Python 3.0) demonstrates the sorts of

error messages you’ll get when you complete this exercise. Really, you’re triggering

Python exceptions; the default exception-handling behavior terminates the running Python program and prints an error message and stack trace on the screen

The stack trace shows where you were in a program when the exception occurred

(if function calls are active when the error happens, the “Traceback” section displays all active call levels). In Part VII, you will learn that you can catch exceptions

using try statements and process them arbitrarily; you’ll also see there that Python

includes a full-blown source code debugger for special error-detection requirements. For now, notice that Python gives meaningful messages when programming

errors occur, instead of crashing silently:

% python

>>> 2 ** 500




>>> 1 / 0

Traceback (most recent call last):

File "", line 1, in

ZeroDivisionError: int division or modulo by zero


>>> spam

Traceback (most recent call last):

File "", line 1, in

NameError: name 'spam' is not defined

6. Breaks and cycles. When you type this code:

L = [1, 2]


you create a cyclic data structure in Python. In Python releases before 1.5.1, the

Python printer wasn’t smart enough to detect cycles in objects, and it would print

an unending stream of [1, 2, [1, 2, [1, 2, [1, 2, and so on, until you hit the

break-key combination on your machine (which, technically, raises a keyboardinterrupt exception that prints a default message). Beginning with Python 1.5.1,

1102 | Appendix B: Solutions to End-of-Part Exercises

the printer is clever enough to detect cycles and prints [[...]] instead to let you

know that it has detected a loop in the object’s structure and avoided getting stuck

printing forever.

The reason for the cycle is subtle and requires information you will glean in

Part II, so this is something of a preview. But in short, assignments in Python always

generate references to objects, not copies of them. You can think of objects as

chunks of memory and of references as implicitly followed pointers. When you run

the first assignment above, the name L becomes a named reference to a two-item

list object—a pointer to a piece of memory. Python lists are really arrays of object

references, with an append method that changes the array in-place by tacking on

another object reference at the end. Here, the append call adds a reference to the

front of L at the end of L, which leads to the cycle illustrated in Figure B-1: a pointer

at the end of the list that points back to the front of the list.

Besides being printed specially, as you’ll learn in Chapter 6 cyclic objects must also

be handled specially by Python’s garbage collector, or their space will remain unreclaimed even when they are no longer in use. Though rare in practice, in some

programs that traverse arbitrary objects or structures you might have to detect such

cycles yourself by keeping track of where you’ve been to avoid looping. Believe it

or not, cyclic data structures can sometimes be useful, despite their special-case


Figure B-1. A cyclic object, created by appending a list to itself. By default, Python appends a reference

to the original list, not a copy of the list.

Part II, Types and Operations

See “Test Your Knowledge: Part II Exercises” on page 255 in Chapter 9 for the


1. The basics. Here are the sorts of results you should get, along with a few comments

about their meaning. Again, note that ; is used in a few of these to squeeze more

than one statement onto a single line (the ; is a statement separator), and commas

Part II, Types and Operations | 1103

build up tuples displayed in parentheses. Also keep in mind that the / division

result near the top differs in Python 2.6 and 3.0 (see Chapter 5 for details), and the

list wrapper around dictionary method calls is needed to display results in 3.0,

but not 2.6 (see Chapter 8):

# Numbers

>>> 2 ** 16

# 2 raised to the power 16


>>> 2 / 5, 2 / 5.0

# Integer / truncates in 2.6, but not 3.0

(0.40000000000000002, 0.40000000000000002)

# Strings

>>> "spam" + "eggs"


>>> S = "ham"

>>> "eggs " + S

'eggs ham'

>>> S * 5


>>> S[:0]


# Concatenation

# Repetition

# An empty slice at the front -- [0:0]

# Empty of same type as object sliced

>>> "green %s and %s" % ("eggs", S)

# Formatting

'green eggs and ham'

>>> 'green {0} and {1}'.format('eggs', S)

'green eggs and ham'

# Tuples

>>> ('x',)[0]


>>> ('x', 'y')[1]


# Indexing a single-item tuple

# Indexing a 2-item tuple

# Lists

>>> L = [1,2,3] + [4,5,6]

# List operations

>>> L, L[:], L[:0], L[-2], L[-2:]

([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [], 5, [5, 6])

>>> ([1,2,3]+[4,5,6])[2:4]

[3, 4]

>>> [L[2], L[3]]

# Fetch from offsets; store in a list

[3, 4]

>>> L.reverse(); L

# Method: reverse list in-place

[6, 5, 4, 3, 2, 1]

>>> L.sort(); L

# Method: sort list in-place

[1, 2, 3, 4, 5, 6]

>>> L.index(4)

# Method: offset of first 4 (search)


# Dictionaries

>>> {'a':1, 'b':2}['b']

1104 | Appendix B: Solutions to End-of-Part Exercises

# Index a dictionary by key







D = {'x':1, 'y':2, 'z':3}

D['w'] = 0

D['x'] + D['w']

# Create a new entry

D[(1,2,3)] = 4

# A tuple used as a key (immutable)

>>> D

{'w': 0, 'z': 3, 'y': 2, (1, 2, 3): 4, 'x': 1}

>>> list(D.keys()), list(D.values()), (1,2,3) in D

(['w', 'z', 'y', (1, 2, 3), 'x'], [0, 3, 2, 4, 1], True)

# Methods, key test

# Empties

>>> [[]], ["",[],(),{},None]

([[]], ['', [], (), {}, None])

# Lots of nothings: empty objects

2. Indexing and slicing. Indexing out of bounds (e.g., L[4]) raises an error; Python

always checks to make sure that all offsets are within the bounds of a sequence.

On the other hand, slicing out of bounds (e.g., L[-1000:100]) works because Python

scales out-of-bounds slices so that they always fit (the limits are set to zero and the

sequence length, if required).

Extracting a sequence in reverse, with the lower bound greater than the higher

bound (e.g., L[3:1]), doesn’t really work. You get back an empty slice ([ ]) because

Python scales the slice limits to make sure that the lower bound is always less than

or equal to the upper bound (e.g., L[3:1] is scaled to L[3:3], the empty insertion

point at offset 3). Python slices are always extracted from left to right, even if you

use negative indexes (they are first converted to positive indexes by adding the

sequence length). Note that Python 2.3’s three-limit slices modify this behavior

somewhat. For instance, L[3:1:-1] does extract from right to left:

>>> L = [1, 2, 3, 4]

>>> L[4]

Traceback (innermost last):

File "", line 1, in ?

IndexError: list index out of range

>>> L[-1000:100]

[1, 2, 3, 4]

>>> L[3:1]


>>> L

[1, 2, 3, 4]

>>> L[3:1] = ['?']

>>> L

[1, 2, 3, '?', 4]

3. Indexing, slicing, and del. Your interaction with the interpreter should look something like the following code. Note that assigning an empty list to an offset stores

an empty list object there, but assigning an empty list to a slice deletes the slice.

Slice assignment expects another sequence, or you’ll get a type error; it inserts items

inside the sequence assigned, not the sequence itself:

Part II, Types and Operations | 1105

>>> L = [1,2,3,4]

>>> L[2] = []

>>> L

[1, 2, [], 4]

>>> L[2:3] = []

>>> L

[1, 2, 4]

>>> del L[0]

>>> L

[2, 4]

>>> del L[1:]

>>> L


>>> L[1:2] = 1

Traceback (innermost last):

File "", line 1, in ?

TypeError: illegal argument type for built-in operation

4. Tuple assignment. The values of X and Y are swapped. When tuples appear on the

left and right of an assignment symbol (=), Python assigns objects on the right to

targets on the left according to their positions. This is probably easiest to understand by noting that the targets on the left aren’t a real tuple, even though they

look like one; they are simply a set of independent assignment targets. The items

on the right are a tuple, which gets unpacked during the assignment (the tuple

provides the temporary assignment needed to achieve the swap effect):

>>> X = 'spam'

>>> Y = 'eggs'

>>> X, Y = Y, X

>>> X


>>> Y


5. Dictionary keys. Any immutable object can be used as a dictionary key, including

integers, tuples, strings, and so on. This really is a dictionary, even though some

of its keys look like integer offsets. Mixed-type keys work fine, too:







D = {}

D[1] = 'a'

D[2] = 'b'

D[(1, 2, 3)] = 'c'


'a', 2: 'b', (1, 2, 3): 'c'}

6. Dictionary indexing. Indexing a nonexistent key (D['d']) raises an error; assigning

to a nonexistent key (D['d']='spam') creates a new dictionary entry. On the other

hand, out-of-bounds indexing for lists raises an error too, but so do out-of-bounds

assignments. Variable names work like dictionary keys; they must have already

been assigned when referenced, but they are created when first assigned. In fact,

variable names can be processed as dictionary keys if you wish (they’re made visible

in module namespace or stack-frame dictionaries):

1106 | Appendix B: Solutions to End-of-Part Exercises

>>> D = {'a':1, 'b':2, 'c':3}

>>> D['a']


>>> D['d']

Traceback (innermost last):

File "", line 1, in ?

KeyError: d

>>> D['d'] = 4

>>> D

{'b': 2, 'd': 4, 'a': 1, 'c': 3}


>>> L = [0, 1]

>>> L[2]

Traceback (innermost last):

File "", line 1, in ?

IndexError: list index out of range

>>> L[2] = 3

Traceback (innermost last):

File "", line 1, in ?

IndexError: list assignment index out of range

7. Generic operations. Question answers:

• The + operator doesn’t work on different/mixed types (e.g., string + list, list +


• + doesn’t work for dictionaries, as they aren’t sequences.

• The append method works only for lists, not strings, and keys works only on

dictionaries. append assumes its target is mutable, since it’s an in-place extension; strings are immutable.

• Slicing and concatenation always return a new object of the same type as the

objects processed:

>>> "x" + 1

Traceback (innermost last):

File "", line 1, in ?

TypeError: illegal argument type for built-in operation


>>> {} + {}

Traceback (innermost last):

File "", line 1, in ?

TypeError: bad operand type(s) for +


>>> [].append(9)

>>> "".append('s')

Traceback (innermost last):

File "", line 1, in ?

AttributeError: attribute-less object


>>> list({}.keys())

# list needed in 3.0, not 2.6


>>> [].keys()

Traceback (innermost last):

File "", line 1, in ?

AttributeError: keys

Part II, Types and Operations | 1107


>>> [][:]


>>> ""[:]


8. String indexing. This is a bit of a trick question—Because strings are collections of

one-character strings, every time you index a string, you get back a string that can

be indexed again. S[0][0][0][0][0] just keeps indexing the first character over and

over. This generally doesn’t work for lists (lists can hold arbitrary objects) unless

the list contains strings:







S = "spam"


L = ['s', 'p']


9. Immutable types. Either of the following solutions works. Index assignment

doesn’t, because strings are immutable:

>>> S = "spam"

>>> S = S[0] + 'l' + S[2:]

>>> S


>>> S = S[0] + 'l' + S[2] + S[3]

>>> S


(See also the Python 3.0 bytearray string type in Chapter 36—it’s a mutable sequence

of small integers that is essentially processed the same as a string.)

10. Nesting. Here is a sample:

>>> me = {'name':('John', 'Q', 'Doe'), 'age':'?', 'job':'engineer'}

>>> me['job']


>>> me['name'][2]


11. Files. Here’s one way to create and read back a text file in Python (ls is a Unix

command; use dir on Windows):

# File: maker.py

file = open('myfile.txt', 'w')

file.write('Hello file world!\n')


# Or: open().write()

# close not always needed

# File: reader.py

file = open('myfile.txt')


# 'r' is default open mode

# Or print(open().read())

% python maker.py

% python reader.py

Hello file world!

1108 | Appendix B: Solutions to End-of-Part Exercises

% ls -l myfile.txt


1 0


19 Apr 13 16:33 myfile.txt

Part III, Statements and Syntax

See “Test Your Knowledge: Part III Exercises” on page 390 in Chapter 15 for the


1. Coding basic loops. As you work through this exercise, you’ll wind up with code

that looks like the following:

>>> S = 'spam'

>>> for c in S:








>>> x = 0

>>> for c in S: x += ord(c)


>>> x


# Or: x = x + ord(c)

>>> x = []

>>> for c in S: x.append(ord(c))


>>> x

[115, 112, 97, 109]

>>> list(map(ord, S))

[115, 112, 97, 109]

# list() required in 3.0, not 2.6

2. Backslash characters. The example prints the bell character (\a) 50 times; assuming

your machine can handle it, and when it’s run outside of IDLE, you may get a series

of beeps (or one sustained tone, if your machine is fast enough). Hey—I warned


3. Sorting dictionaries. Here’s one way to work through this exercise (see Chapter 8

or Chapter 14 if this doesn’t make sense). Remember, you really do have to split

up the keys and sort calls like this because sort returns None. In Python 2.2 and

later, you can iterate through dictionary keys directly without calling keys (e.g.,

for key in D:), but the keys list will not be sorted like it is by this code. In more

recent Pythons, you can achieve the same effect with the sorted built-in, too:

>>> D = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7}

>>> D

{'f': 6, 'c': 3, 'a': 1, 'g': 7, 'e': 5, 'd': 4, 'b': 2}


>>> keys = list(D.keys())

# list() required in 3.0, not in 2.6

Part III, Statements and Syntax | 1109

>>> keys.sort()

>>> for key in keys:


print(key, '=>', D[key])


a => 1

b => 2

c => 3

d => 4

e => 5

f => 6

g => 7

>>> for key in sorted(D):


print(key, '=>', D[key])

# Better, in more recent Pythons

4. Program logic alternatives. Here’s some sample code for the solutions. For step e,

assign the result of 2 ** X to a variable outside the loops of steps a and b, and use

it inside the loop. Your results may vary a bit; this exercise is mostly designed to

get you playing with code alternatives, so anything reasonable gets full credit:


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

X = 5

i = 0

while i < len(L):

if 2 ** X == L[i]:

print('at index', i)


i += 1


print(X, 'not found')


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

X = 5

for p in L:

if (2 ** X) == p:

print((2 ** X), 'was found at', L.index(p))



print(X, 'not found')


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

X = 5

if (2 ** X) in L:

print((2 ** X), 'was found at', L.index(2 ** X))

1110 | Appendix B: Solutions to End-of-Part Exercises


print(X, 'not found')


X = 5

L = []

for i in range(7): L.append(2 ** i)


if (2 ** X) in L:

print((2 ** X), 'was found at', L.index(2 ** X))


print(X, 'not found')

# f

X = 5

L = list(map(lambda x: 2**x, range(7)))


# or [2**x for x in range(7)]

# list() to print all in 3.0, not 2.6

if (2 ** X) in L:

print((2 ** X), 'was found at', L.index(2 ** X))


print(X, 'not found')

Part IV, Functions

See “Test Your Knowledge: Part IV Exercises” on page 524 in Chapter 20 for the


1. The basics. There’s not much to this one, but notice that using print (and hence

your function) is technically a polymorphic operation, which does the right thing

for each type of object:

% python

>>> def func(x): print(x)


>>> func("spam")


>>> func(42)


>>> func([1, 2, 3])

[1, 2, 3]

>>> func({'food': 'spam'})

{'food': 'spam'}

2. Arguments. Here’s a sample solution. Remember that you have to use print to see

results in the test calls because a file isn’t the same as code typed interactively;

Python doesn’t normally echo the results of expression statements in files:

Part IV, Functions | 1111

def adder(x, y):

return x + y

print(adder(2, 3))

print(adder('spam', 'eggs'))

print(adder(['a', 'b'], ['c', 'd']))

% python mod.py



['a', 'b', 'c', 'd']

3. varargs. Two alternative adder functions are shown in the following file,

adders.py. The hard part here is figuring out how to initialize an accumulator to an

empty value of whatever type is passed in. The first solution uses manual type

testing to look for an integer, and an empty slice of the first argument (assumed to

be a sequence) if the argument is determined not to be an integer. The second

solution uses the first argument to initialize and scan items 2 and beyond, much

like one of the min function variants shown in Chapter 18.

The second solution is better. Both of these assume all arguments are of the same

type, and neither works on dictionaries (as we saw in Part II, + doesn’t work on

mixed types or dictionaries). You could add a type test and special code to allow

dictionaries, too, but that’s extra credit.

def adder1(*args):

print('adder1', end=' ')

if type(args[0]) == type(0):

sum = 0


sum = args[0][:0]

for arg in args:

sum = sum + arg

return sum

def adder2(*args):

print('adder2', end=' ')

sum = args[0]

for next in args[1:]:

sum += next

return sum

# Integer?

# Init to zero

# else sequence:

# Use empty slice of arg1

# Init to arg1

# Add items 2..N

for func in (adder1, adder2):

print(func(2, 3, 4))

print(func('spam', 'eggs', 'toast'))

print(func(['a', 'b'], ['c', 'd'], ['e', 'f']))

% python adders.py

adder1 9

adder1 spameggstoast

adder1 ['a', 'b', 'c', 'd', 'e', 'f']

adder2 9

adder2 spameggstoast

adder2 ['a', 'b', 'c', 'd', 'e', 'f']

1112 | Appendix B: Solutions to End-of-Part Exercises

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix B. Solutions to End-of-Part Exercises

Tải bản đầy đủ ngay(0 tr)