INDEX 243
BMI (Body Mass Index), 120 class statement, 167
Boolean classes
algebra (logic), 134 Button, 175
expression, 106, 131 Calculator, 197
operator, 132 Dice, 217
values, 106 DieView, 176, 193
break statement, 136 GraphicsInterface, 225
implementing post-test loop, 136 MSDie, 166
style considerations, 137 Player, 213
Brooks, Fred, 208 PokerApp, 219
bug, 13 Projectile, 169
butterfly effect, 11 Projectile as module file, 171
Button RBallGame, 211
class definition, 175 SimStats, 210
description, 173 TextInterface, 221
methods, 174 client, 207
button.py, 175 clone, 70, 82
byte code, 8 close
GraphWin, 81
Caesar cipher, 61 cmp, 203
calculator code duplication
in future value graph, 91
problem description, 194 maintenance issues, 88
program, 197 reducing with functions, 88
cannonball coffee, 39
algorithm, 162 Collatz sequence, 140
graphical display, 180 color
problem description, 162 changing graphics object, 75
program, 164, 169, 172 changing GraphWin, 75
Projectile class, 169 fill, 75
card, playing, 181 outline, 75
cball1.py, 164 specifying, 84
cball3.py, 169 color rgb, 84
cball4.py, 172 comments, 9
Celsius, 13 compareItems, 203
change counter compiler, 4
program, 27, 54 diagram, 5
change.py, 27 vs. interpreter, 5
change2.py, 54 compound condition, 115
chaos computer
discussion, 10–11 definition of, 1
program, 7 functional view, 3
chaos.py, 7 program, 1
chr, 46 computer science
Christmas, 85 definition of, 2
cipher, 50 methods of investigation, 2
ciphertext, 50 concatenation
Circle list, 185
constructor, 82 string, 43
methods, 82 condition, 105
circle compound, 115
area formula, 38 design issues, 115
intersection with line, 85 for termination, 134
class, 66, 161 syntax, 105
class standing, 120
244 INDEX
conditional loop, 124 debugging, 13
constructor, 67, 161 decision, 103
init , 168 implementation via Boolean operator, 138
parameters in, 67 multi-way, 110
control codes, 46 nested, 110
control structure, 103 simple (one-way), 105
decision, 103 two-way, 108
definition of, 22 decision tree, 115
loop, 22 decoding, 48
nested loops, 130 algorithm, 48
nesting, 110 program, 49
control structures definite loop, 124
Boolean operators, 138 definition of, 20
for statement, 22 use as counted loop, 22
if, 105 degree-days, 140
if-elif-else, 111 delete, 187
if-else, 108 DeMorgan’s laws, 134
while, 124 design, 13, 207
convert.py, 14, 103 object oriented, see object oriented design
convert2.py, 104 top-down, 146
convert gui.pyw, 79
coordinates steps in, 154
as instance variables, 67 design pattern
changing with setCoords, 75
in a GraphWin, 65 importance of, 124
of a Point, 65 design patterns
setCoords example, 76
transforming, 75 counted loop, 21, 123
counted loop end-of-file loop, 129
definition of, 21 interactive loop, 126
in Python, 22 IPO, 14
CPU (Central Processing Unit), 3 loop accumulator, 31, 123
craps, 159 model-view, 217
createLabeledWindow, 98 nested loops, 130, 131
cryptography, 50 sentinel loop, 127
cylinder, 180
loop and a half, 136
data, 27, 161 design techniques
data type
divide and conquer, 235
automatic conversion, 36 spiral development, 156
definition of, 28
explicit conversion, 36 when to use, 158
in format specifiers, 53 dice, 159
mixed-type expressions, 36 dice poker
string conversion, 52
string conversions, 49 classes
data types Dice, 217
file, 55 GraphicsInterface, 225
float, 28 PokerApp, 219
int, 28 TextInterface, 221
long int, 35
string, 41 problem description, 216
date, 120 dice roller
day number, 120
problem description, 173
program, 178
dictionary, 199
creation, 200
empty, 200
methods, 200
DieView, 191
class definition, 176, 193
INDEX 245
description, 176 fetch execute cycle, 3
Dijkstra, Edsgar, 2 Fibonacci numbers, 39, 140
disk, 3 file, 55
distance function, 96
division, 29 closing, 56
docstring, 171 opening, 56
dot notation, 8, 59, 67 processing, 56
draw, 82 program to print, 57
drawBar, 91 read operations, 56
duplication, see code duplication representation, 55
write operations, 57
Easter, 120 float, 28
elif, 111 literal, 28
empty list, 186 representation, 35
empty string, 128 floppy, 3
encapsulation, 170, 228 flowchart, 22
encoding, 46 flowcharts
for loop, 22
algorithm, 47 if semantics, 105
program, 47 loop and a half sentinel, 137
encryption, 50 max-of-three decision tree, 116
Entry, 79, 83 max-of-three sequential solution, 117
environment, programming, 7 nested decisions, 111
epact, 39 post-test loop, 135
equality, 105 temperature conversion with warnings, 104
Eratosthenes, 206 two-way decision, 109
error checking, 112 while loop, 125
errors flush, 81
KeyError, 201 for statement (for loop), 21, 123
math range, 30 as counted loop, 22
name, 16, 42 flowchart, 22
overflow, 33 semantics, 21
Euclid’s algorithm, 140 syntax, 21
eval, 49 using simultaneous assignment, 196
event, 77 formal parameter, 93
event loop, 179 format specifier, 53
event-driven, 77 from..import, 64
exam grader, 61, 120 function, 6
exception handling, 112 actual parameters, 93
exponential notation, 35 arguments, 93
expression as parameter, 203
as input, 18 as black box, 207
Boolean, 106, 131 as subprogram, 88
definition of, 15 call, 6, 93
spaces in, 16 createLabeledWindow, 98
defining, 6, 93
face, 85, 180 for modularity, 97
fact.py, 236 invoking, see function, call
factorial missing return, 97
multiple parameters, 94
definition of, 31 None as default return, 97
program, 32, 35 parameters, 6
recursive definition, 235 recursive, 236
factorial.py, 32 return value, 95
factorial2, 35 returning multiple values, 96
Fahrenheit, 13
246 INDEX
signature (interface), 148 program specification, 23
to reduce duplication, 88 future value graph
function definition, 88
functions final algorithm, 73
built-in problem, 70
program, 74, 76, 87, 91
chr, 46 rough algorithm, 71
cmp, 203 futval.py, 24
eval, 49 futval graph.py, 74
float, 37 futval graph2.py, 76, 87
int, 37 futval graph3.py, 91
len, 43 futval graph4.py, 99
long, 37
max, 118 gameOver, 152
open, 56 GCD (Greatest Common Divisor), 140
ord, 46 getAnchor, 83
range, 32 getCenter, 82, 83
raw input, 42 getInputs, 148
read, 56 getMouse, 78, 81
readline, 56
readlines, 56 example use, 78
round, 37 getNumbers, 187
str, 52 getP1, 82, 83
type, 28 getP2, 82, 83
write, 57 getPoints, 83
compareItems, 203 getRadius, 82
distance, 96 getText, 83
drawBar, 91 getX, 82
gameOver, 152 getY, 82
getInputs, 148 goofy.py, 247
getNumbers, 187 gozinta, 29
happy, 89 graphics library, 64, 81–84
main, 7
why use, 9 drawing example, 66
math library, see math library, functions generic methods summary, 82
mean, 188 graphical objects, 82–83
median, 189 methods
merge, 239
mergeSort, 240 for Text, 83
moveTower, 244 clone, 70
random library, see random library, functions for Circle, 82
recursive binary search, 237 for Entry, 83
recursive factorial, 236 for Image, 83
selsort, 238 for Line, 82
simNGames, 150 for Oval, 83
simOneGame, 152 for Point, 82
singFred, 89 for Polygon, 83
singLucy, 89 for Rectangle, 82
square, 95 getMouse, 78
stdDev, 188 move, 68
string library, see string library setCoords, 75
future value objects
algorithm, 23 Circle, 82
problem description, 23 Entry, 79, 83
program, 24, 99 GraphWin, 64, 81
Image, 83
Line, 82
Oval, 83
INDEX 247
Point, 65, 82 validation, 135
Polygon, 79, 83 input statement, 18
Rectangle, 82
Text, 83 multiple values, 20
GraphWin, 64, 81 semantics, 18
methods summary, 81 syntax, 18
Gregorian epact, 39 Input/Output Devices, 3
GUI, 64 instance, 66, 161
instance variable, 67, 161
hailstone function, 140 accessing, 168
halting problem, 246 and object state, 168
happy, 89 int, 28
happy birthday automatic conversion to float, 36
conversion to float, 37
lyrics, 88 literal, 28
problem description, 88 range of, 34
program, 90 representaiton, 34
happy.py, 90 integer division, 29
hard drive, 3 interface, 148
hardware, 2 interpreter, 4
hash array, 199 diagram, 5
hierarchy chart, 148, see structure chart Python, 5
house, 86 vs. compiler, 5
house (of representatives), 120 intractable problems, 2, 246
investment doubling, 140
identifier IPO (Input, Process, Output), 14
definition of, 15 iteration, 20
rules for forming, 15
key
Idle, 7 cipher, 51
if statement private, 51
public, 51
flowchart, 105 with dictionary, 199
semantics, 105
syntax, 105 key-value pair, 199
if-elif-else statement KeyError, 201
semantics, 111
syntax, 111 label, 72
if-else statement ladder, 39
decision tree, 116 leap year, 120
nested, 110, 116 left-justification, 54
semantics, 109 len
syntax, 108
Image, 83 with list, 185, 188
implementation, 13 with string, 43
import statement, 30, 106 lexicographic ordering, 105
with “from”, 64 library
indefinite loop, 124 definition of, 29
indexing graphics, see graphics library
dictionary, 200 math, see math library
from the right, 52 random, see random library
list, 185, 187 string, see string library
negative indexes, 52 lightning, 39
string, 42 Line, 82
infinite loop, 125, 136 line continuation
inheritance, 229
input, 9
using backslash ( ), 55
using brackets, 191
248 INDEX
linear time, 234 maxn.py, 117
list, 184 mean, 188
median, 184, 189
as sequence, 185 memory, 3
creation, 186
empty, 186 main, 3
indexing, 185 secondary, 3
merging, 239 merge, 239
methods, 187, 203 merge sort, 239, see sorting, merge sort
operators, 185 mergeSort, 240
removing items, 187 analysis, 241
slice, 187 message decoding
vs. string, 185 algorithm, 48
literal, 16 problem description, 48
float, 28 program, 49
int, 28 message encoding
string, 41, 172 algorithm, 47
log time, 234 problem description, 46
long int, 35 program, 47
when to use, 36 meta-language, 17
loop, 9 method, 67, 161
accumulator variable, 31 parameterless, 68
as control structure, 22 accessor, 68
counted, 21, 22 call (invoke), 67, 167
definite, 20, 124 mutator, 68
end-of-file, 129 normal parameter, 167
event loop, 179 object parameters, 68
for statement, 21 self parameter, 167
indefinite (conditional), 124 methods
index variable, 21 activate, 174
infinite, 125, 136 clicked, 174
interactive, 126 deactivate, 174
loop and a half, 136 dictionary, 200
nested, 130 list, 186, 187
over a sequence, 21 model-view, 217
post-test, 135 module file, 7
module hierarchy chart, see structure chart
using break, 136 molecular weight, 39
using while, 135 Monte Carlo, 144, 159
pre-test, 124 month abbreviation
vs. recursive function, 237 problem description, 44
while statement, 124 program, 45
loop and a half, 136 month.py, 45
lower, 59 move, 68, 82
Lucas, Edouard, 243 moveTower, 244
MPG, 140
machine code, 4 MSDie, 166
maintenance, 13 mutable, 185, 200
mapping, 199 mutator, 68
math library, 29
name error, 16, 42
functions, 30, 31 names, 15
using, 30 nesting, 110
math range error, 30
max, 118
max-of-n program, 117
max-of-three, 114–117 newline character ( n), 55, 108
with readline, 130
INDEX 249
Newton’s method, 40 formal, 93
None, 97 functions as parameters, 203
numbers2text.py, 49 matching by order, 95
numerology, 61 multiple, 94
objects as, 68
object, 161 removing code duplication, 90
aliasing, 69 scope issues, 92, 93
application as, 194 self, 167
as black box, 207 pi
as parameter, 68 math library, 31
attributes, 161 Monte Carlo approximation, 159
definition of, 63 series approximation, 39
state, 68 pixel, 65
pizza, 38
object oriented design (OOD), 207 plaintext, 50
object-oriented, 63 Player, 213
objects playing card, 181
plot, 81
built-in plotPixel, 81
file, 59 Point, 65, 82
None, 97 poker, see dice poker
string, 59 Polygon, 79, 83
polymorphism, 228
graphics, see graphics library, objects portability, 5
other, see classes post-test loop, 135
objrball.py, 213 precision, 53
Old MacDonald, 100 prime number, 140, 206
one-way decision, 105 priming read, 127
open, 56 print statement, 6, 17
operator semantics, 17
Boolean, 132 syntax, 17
printfile.py, 57
as control structure, 138 prism, 180
definition of, 16 private attributes, 178
precedence, 16, 132 private key encryption, 51
relational, 105 program, 1
short-circuit, 138 programming
operators definition of, 2
Boolean, 132 environment, 7
del, 187 event-driven, 77
list, 185 why learn, 2
mathematical, 16 programming language, 4–5
Python numeric operators, 28 and portability, 5
relational, 105 vs. natural language, 4
string formatting, 53 examples, 4
or, 132 high-level, 4
operational definition, 138 syntax, 17
ord, 46 translation, 4
output labeling, 17 programs, 35
output statements, 16 average n numbers, 123, 126–130
Oval, 83 average two numbers, 20
overflow error, 33 calculator, 197
override, 230 cannonball simulation, 164, 169, 172
overtime, 120 change counter, 27, 54
parameter, 6
actual, 93
as function input, 95
250 INDEX
chaos, 7 quiz grader, 60, 120
dice roller, 178
factorial, 32 racquetball, 133, 143
future value, 24 racquetball simulation
future value graph, 74, 76, 87, 91, 99
goofy: an impossible program, 247 algorithms
happy birthday, 90 simNGames, 149
max-of-n, 117 simOneGmae, 150
message decoding, 49
message encoding, 47 classes
month abbreviation, 45 Player, 213
print file, 57 RBallGame, 211
quadratic equation, 29, 107–109, 111–113 SimStats, 210
racquetball simulation, 153
racquetball simulation (object version, 213 discussion, 156
simple statistics, 189 problem description, 144
temperature conversion, 14, 79, 103, 104 program, 153
triangle, 78, 96 program (object version), 213
username generation, 44, 58 specification, 144
word frequency, 204 structure charts
prompt
Python, 6 level 2, 150
using Text object, 79 level 3, 151
prototype, 156 top-level, 148
pseudocode, 14 RAM (random access memory), 3
pseudorandom numbers, 144 random, 145
public key encryption, 51 random library, 145
pyc file, 8 functions
Python random, 145
Boolean operators, 132 randrange, 145
mathematical operators, 16 random numbers, 144
numeric operators, 28 random walk, 159
programming environment, 7 randrange, 145
relational operators, 105 range, 21
reserved words, 15 general form, 32
running programs, 7 range error, 30
pyw, 78 raw input, 42
RBallGame, 211
quadratic equation, 29 read, 56
algorithm with three-way decision, 110 readline, 56
decision flowchart, 109 readlines, 56
program, 29, 107 recBinSearch, 237
program (bullet-proof), 113 Rectangle, 82
program (simple if), 108 recursion, 235
program (two-way decision), 109 regression line, 141, 181
program (using exception), 112 relational operator, 105
program (using if-elif-else), 111 repetition
list, 185
quadratic time, 241 string, 43
quadratic.py, 29, 107 requirements, 13
quadratic2.py, 108 reserved words
quadratic3.py, 109 definition of, 15
quadratic4.py, 111 in Python, 15
quadratic5.py, 112 resolution, 72
quadratic6.py, 113 return statement, 95
multiple values, 96
roller.py, 178
root beer, 30
INDEX 251
round, 37 snowman, 85
software, 2
scientific notation, 35 software development, 13
scope, 92
screen resolution, 72 phases
script, 7 design, 13
search, 231 implementation, 13
searching maintenance, 13
requirements, 13
binary search, 232 specifications, 13
linear search, 232 testing/debugging, 13
problem description, 231
recursive formulation, 237 sort, 203
seed, 145 sorting, 234
selection sort, see sorting, selection sort
self, 167 merge sort
selSort, 238 algorithm, 240
semantics, 4 analysis, 241
senate, 120 implementation, 240
sentinel, 127
sentinel loop, 127 selection sort
sequence operators, 185 algorithm, 238
setArrow, 82 analysis, 241
setBackground, 81 implementation, 238
setCoords, 75, 81
example, 76 space
setFace, 83 between program lines, 24
setFill, 82 blank line in output, 94, 108
setOutline, 82 in expressions, 16
sets, 206 in prompts, 18
setSize, 83
setStyle, 83 specifications, 13
setText, 83 speeding fine, 120
setWidth, 82 sphere, 38, 180
shell game, 179
shuffle, 206 surface area formula, 38
Sieve of Eratosthenes, 206 volume formula, 38
signature, 148 split, 48
simNGames, 150 sqrt, 30
simOneGame, 152 square function, 95
simple decision, 105 square root, 40
simple statistics, 205 standard deviation, 184
problem, 184 statement, 6
program, 189 statements
SimStats, 210 assignment, 10, 17–20
simulation, 143 break, 136
simultaneous assignment, 19 class, 167
in for loop, 196 comment, 9
with multiple return values, 97 def (function definition), 6, 88
singFred, 89 for, 21, 123
singLucy, 89 from..import, 64
slicing if, 105
list, 187 if-elif-else, 111
string, 43 if-else, 108
slope of line, 39 import, 30
input, 9, 18
multiple input, 20
print, 6, 16–17
return, 95
simultaneous assignment, 19
try-except, 113
252 INDEX
while, 124 problem description, 13
stats.py, 189 program, 14, 103
stdDev, 188 program with GUI, 79
step-wise refinement, 154 temperature conversion with warnings
str, 52 design, 103
string, 17, 41 flowchart, 104
problem description, 103
as input, 41 program, 104
as lookup table, 44 tennis, 158
ASCII encoding, 46 testing, 13
concatenation, 43 unit, 155
converting to, 52 Text, 83
converting to other types, 49 as prompt, 79
formatting, see string formatting methods, 83
formatting operator (%), 53 text file, 55
indexing, 42 text2numbers.py, 47
textpoker.py, 221
from back, 52 Tkinter, 64
length, 43 top-down design, 146
library, see string library steps in process, 154
literal, 41, 172 Towers of Hanoi (Brahma), 243
multi-line, 172 recursive solution, 244
operators, 44 Tracker, 180
repetition, 43 triangle
representation, 46 area formula, 39
slicing, 43 program, 78, 96
substring, 43 triangle.pyw, 78
UniCode encoding, 46 triangle2.py, 96
vs. list, 185 truth table, 132
string formatting, 52 truth tables
examples, 53 definition of and, 132
format specifier, 53 definition of not, 132
leading zeroes, 55 definition of or, 132
left-justification, 54 try-except statement
using a tuple, 204 semantics, 113
string library, 48 syntax, 113
function summary, 51 tuple, 195
lower, 59 as string formtting argument, 204
split, 48 unpacking, 196
structure chart, 148 type conversion
structure charts to float, 37
racquetball simulation level 2, 150 automatic, 36
racquetball simulation level 3, 151 explicit, 36
racquetball simulation top level, 148 from string, 49
subprogram, 88 summary of functions, 52
substitution cipher, 50 to int, 37
substring, 43 to long int, 37
swap, 19 to string, 52
using simultaneous assignment, 20 type function, 28
syntax, 4, 17
Syracuse numbers, 140 undraw, 82
UniCode, 46
table tennis, 158 unit testing, 155
table-driven, 192 unpacking, 196
temperature conversion
algorithm, 14
INDEX 253
userfile.py, 58
username generation
program, 44, 58
username.py, 44
validation
of inputs, 135
value returning function, 95
variable
changing value, 18
definition of, 9
instance, 67, 161
local, 92
scope, 92
VGA, 72
volleyball, 133, 158
wc, 62
while statement
as post-test loop, 135
flow chart, 125
semantics, 124
syntax, 124
whitespace, see space
widget, 77, 172
width, 53
windchill, 140
winter, 85
word count, 62
word frequency
problem description, 201
program, 204
wordfreq.py, 204
write, 57