. A program is a set of instructions that a computer follows to perform a task.
. Hardware is all of the physical devices, or components, that a computer is made
of.
. The central processing unit (CPU), main memory, secondary storage devices,
input devices, and output devices.
. The CPU
. Main memory
. Secondary storage
. Input device
. Output device
. The operating system
. A utility program
. Application software
. One byte
. A bit
. The binary numbering system.
. It is an encoding scheme that uses a set of numeric codes to represent the
English letters, various punctuation marks, and other characters. These numeric
codes are used to store characters in a computer's memory. (ASCII stands for the
American Standard Code for Information Interchange.)
. Unicode
. Digital data is data that is stored in binary, and a digital device is any device that
works with binary data.
. Machine language
. Main memory, or RAM
. The fetch-decode-execute cycle.
. It is an alternative to machine language. Instead of using binary numbers for
instructions, assembly language uses short words that are known as mnemonics.
. A high-level language
. Syntax
. A compiler
. An interpreter
. A syntax error
Chapter
. Any person, group, or organization that is asking you to write a program.
. A single function that the program must perform in order to satisfy the customer.
. A set of well-defined logical steps that must be taken to perform a task.
Gaddis – Starting Out with Python, Appendix D Page
. An informal language that has no syntax rules, and is not meant to be compiled or
executed. Instead, programmers use pseudocode to create models, or "mock-ups"
of programs.
. A diagram that graphically depicts the steps that take place in a program.
. Ovals are terminal symbols. Parallelograms are either output or input symbols.
Rectangles are processing symbols.
. print 'Jimmy Smith'
. print "Python's the best!"
. print 'The cat said "meow"'
. A name that references a value in the computer's memory.
. bottles is illegal because it begins with a number. r&d is illegal because
the & character is not allowed.
. No, it is not because variable names are case-sensitive.
. It is invalid because the variable that is receiving the assignment (in this case
amount) must appear on the left side of the = operator.
. The value is val
. value will reference an int. value will reference a float. value will
reference a float. value will reference an int. value will reference a
str (string).
.
. sales = input('Enter the sales for the week: ')
. last_name = input("Enter the customer's last name: ")
. Complete the following table by writing the value of each expression in the Value
column.
Expression Value
-----------------------------------------------
+ *
/ -
+ * -
( + ) *
/ ( - )
+ * ( - )
float() / .
float( / ) .
int(. / .)
.
.
Chapter
. A function is a group of statements that exist within a program for the purpose of
performing a specific task.
. A large task is divided into several smaller tasks that are easily performed.
Gaddis – Starting Out with Python, Appendix D Page
. If a specific operation is performed in several places in a program, a function can
be written once to perform that operation, and then be executed any time it is
needed.
. Functions can be written for the common tasks that are needed by the different
programs. Those functions can then be incorporated into each program that needs
them.
. When a program is developed as a set of functions that each performs an
individual task, then different programmers can be assigned the job of writing
different functions.
. A function definition has two parts: a header and a block. The header indicates the
starting point of the function, and the block is a list of statements that belong to
the function.
. To call a function means to execute the function.
. When the end of the function is reached, the computer returns back to the part of
the program that called the function, and the program resume execution at that
point.
. Because the Python interpreter uses the indentation to determine where a block
begins and ends.
.
• The overall task that the program is to perform is broken down into a series of
subtasks.
• Each of the subtasks is examined to determine whether it can be further broken
down into more subtasks. This step is repeated until no more subtasks can be
identified.
• Once all of the subtasks have been identified, they are written in code.
. A local variable is a variable that is declared inside a function. It belongs to the
function in which it is declared, and only statements in the same function can
access it.
. The part of a program in which a variable may be accessed.
. Yes, it is permissible.
. Arguments
. Parameters
. A parameter variable’s scope is usually the entire function in which the parameter
is declared.
. No, it does not.
. The entire program
. Here are three:
• Global variables make debugging difficult. Any statement in a program can
change the value of a global variable. If you find that the wrong value is being
stored in a global variable, you have to track down every statement that
accesses it to determine where the bad value is coming from. In a program
with thousands of lines of code, this can be difficult.
• Functions that use global variables are usually dependent on those variables. If
you want to use such a function in a different program, you will most likely
have to redesign it so it does not rely on the global variable.
Gaddis – Starting Out with Python, Appendix D Page
• Global variables make a program hard to understand. A global variable can be
modified by any statement in the program. If you are to understand any part of
the program that uses a global variable, you have to be aware of all the other
parts of the program that access the global variable.
. A global constant is a name that is available to every function in the program. It is
permissible to use global constants. Because their value cannot be changed during
the program's execution, you do not have to worry about its value being altered.
Chapter
. A logical design that controls the order in which a set of statements execute.
. It is a program structure that can execute a set of statements only under certain
circumstances.
. A decision structure that provides a single alternative path of execution. If the
condition that is being tested is true, the program takes the alternative path.
. An expression that can be evaluated as either true or false.
. You can determine whether one value is greater than, less than, greater than or
equal to, less than or equal to, equal to, or not equal to another value.
.
if y == :
x =
.
if sales >= :
commission = .
. A dual alternative decision structure has two possible paths of execution—one path
is taken if a condition is true, and the other path is taken if the condition is false.
. if-else
. When the condition is false
. z is not less than a.
. Boston
New York
.
if number == :
print 'One'
elsif number == :
print 'Two'
elsif number == :
print 'Three'
else:
print 'Unknown'
. It is an expression that is created by using a logical operator to combine two
Boolean subexpressions.
.
F
Gaddis – Starting Out with Python, Appendix D Page
T
F
F
T
T
T
F
F
T
.
T
F
T
T
. The and operator: If the expression on the left side of the and operator is false,
the expression on the right side will not be checked.
The or operator: If the expression on the left side of the or operator is true, the
expression on the right side will not be checked.
.
if speed >= and speed <= :
print 'The number is valid'
.
if speed <> :
print 'The number is not valid'
. True or False
. A variable that signals when some condition exists in the program.
Chapter
. A structure that causes a section of code to repeat.
. A loop that uses a true/false condition to control the number of times that it
repeats.
. A loop that repeats a specific number of times.
. An execution of the statements in the body of the loop.
. Before
. None. The condition count < will be false to begin with.
. A loop that has no way of stopping, and repeats until the program is interrupted.
.
for x in range():
print 'I love to program!'
.

Gaddis – Starting Out with Python, Appendix D Page




.



.





.




. A variable that is used to accumulate the total of a series of numbers.
. Yes, it should be initialized with the value . This is because values are added to
the accumulator by a loop. If the accumulator does not start at the value , it will
not contain the correct total of the numbers that were added to it when the loop
ends.
.
.

. a) quantity +=
b) days_left -=
c) price *=
d) price /=
. A sentinel is a special value that marks the end of a list of items.
. A sentinel value must be unique enough that it will not be mistaken as a regular
value in the list.
. It means that if bad data (garbage) is provided as input to a program, the program
will produce bad data (garbage) as output.
. When input is given to a program, it should be inspected before it is processed. If
the input is invalid, then it should be discarded and the user should be prompted to
enter the correct data.
. The input is read, and then a pretest loop is executed. If the input data is invalid,
the body of the loop executes. In the body of the loop, an error message is
Gaddis – Starting Out with Python, Appendix D Page
displayed so the user will know that the input was invalid, and then the input read
again. The loop repeats as long as the input is invalid.
. It is the input operation that takes place just before an input validation loop. The
purpose of the priming read is to get the first input value.
. None.
Chapter
. The difference is that a value returning function returns a value back to the
statement that called it. A simple function does not return a value.
. A prewritten function that performs some commonly needed task.
. The term “black box” is used to describe any mechanism that accepts input,
performs some operation (that cannot be seen) using the input, and produces
output.
. It assigns a random integer in the range of through to the variable x.
. It prints a random integer in the range of through .
. It prints a random integer in the range of through .
print random.randrange(, )
. It prints a random floating point number in the range of . up to, but not
including, ..
. It prints a random floating-point number in the range of . through ..
. What is the purpose of the return statement in a function?
. Look at the following function definition:
def do_something(number):
return number *
a. do_something
b. It returns a value that is twice the argument passed to it.
c.
. A function that returns either True or False.
. import math
. square_root = math.sqrt()
. angle = math.radians()
Chapter
. A file that a program writes data to. It is called an output file because the program
sends output to it.
. A file that a program reads data from. It is called an input file because the
program receives input from it.
. () Open the file () Process the file () Close the file
. Text and binary. A text file contains data that has been encoded as text, using a
scheme such as ASCII. Even if the file contains numbers, those numbers are
stored in the file as a series of characters. As a result, the file may be opened and
viewed in a text editor such as Notepad. A binary file contains data that has not
Gaddis – Starting Out with Python, Appendix D Page
been converted to text. As a consequence, you cannot view the contents of a
binary file with a text editor.
. Sequential and direct access. When you work with a sequential access file, you
access data from the beginning of the file to the end of the file. When you work
with a direct access file, you can jump directly to any piece of data in the file
without reading the data that comes before it.
. The file's name on the disk, and the name of a variable that references a file
object.
. The file's contents are erased.
. Opening a file creates a connection between the file and the program. It also
creates an association between the file and a file object.
. Closing a file disconnects the program from the file.
. A file’s read position marks the location of the next item that will be read from
the file. When an input file is opened, its read position is initially set to the first
item in the file.
. You open the file in append mode. When you write data to a file in append mode,
the data is written to the end of the file's existing contents.
.
outfile = open('numbers.txt', 'w')
for num in range(, ):
outfile.write(str(num) + '\n')
outfile.close()
. The readline method returns an empty string ('') when it has attempted to
read beyond the end of a file.
.
infile = open('numbers.txt', 'r')
line = infile.readline()
while line != '':
print line
line = infile.readline()
infile.close()
. Revise the program that you wrote for Checkpoint .x so to use the for loop
instead of the while loop.
infile = open('data.txt', 'r')
for line in infile:
print line
infile.close()
. A record is a complete set of data that describes one item, and a field is a single
piece of data within a record.
. You copy all of the original file's records to the temporary file, but when you get
to the record that is to be modified, you do not write its old contents to the
temporary file. Instead, you write its new, modified values to the temporary file.
Gaddis – Starting Out with Python, Appendix D Page
Then, you finish copying any remaining records from the original file to the
temporary file.
. You copy all of the original file's records to the temporary file, except for the
record that is to be deleted. The temporary file then takes the place of the original
file. You delete the original file and rename the temporary file, giving it the name
that the original file had on the computer's disk.
. An exception is an error that occurs while a program is running. In most cases, an
exception causes a program to abruptly halt.
. The program halts.
. IOError
. ValueError
Chapter
.
for letter in name:
print letter
.
.
. An IndexError exception will occur if you try to use an index that is out of
range for a particular string.
. Use the built-in len function.
. The second statement attempts to assign a value to an individual character in the
string. Strings are immutable, however, so the expression animal[] cannot
appear on the left side of an assignment operator.
. cde
. defg
. abc
. abcdefg
.
if 'd' in mystring:
print 'yep. tis there.'
. little = big.upper()
.
if ch.isdigit():
print 'Digit'
else:
print 'No digit'
. a A
.
again = raw_input('Do you want to repeat ' + \
'the program or quit? (R/Q) ')
while again.upper() != 'R' and again.upper() != 'Q':
again = raw_input('Do you want to repeat the ' +
'program or quit? (R/Q) ')
Gaddis – Starting Out with Python, Appendix D Page
. $
.
for letter in mystring:
if letter.isupper():
count +=
. [, , , , ]
. [, ]
. Use the built-in len function.
. The remove method removes a specific item from a list. The del statement
removes an item that is at a specific index.
. Use the built-in min and max functions.
. b
.
a) You pass an argument to the index method and it returns the index of
the first element in the list containing that item. If the item is not found in
the list, the method raises a ValueError exception.
b) The insert method allows you to insert an item into a list at a specific
position. You pass two arguments to the insert method: an index
specifying where the item should be inserted and the item that you want
to insert.
c) The sort method rearranges the elements of a list so they appear in
ascending order (from the lowest value to the highest value).
d) The reverse method reverses the order of the items in the list.
. mylist = days.split()
. You use a loop with an accumulator variable. The loop steps through the list,
adding the value of each element to the accumulator.
. You calculate the total of the list elements, then divide the total by the number of
elements in the list.
Chapter
. An object is a software entity that contains both data and procedures.
. Encapsulation is the combining of data and code into a single object.
. When an object’s internal data is hidden from outside code and access to that data
is restricted to the object’s methods, the data is protected from accidental
corruption. In addition, the programming code outside the object does not need to
know about the format or internal structure of the object’s data.
. Public methods can be accessed by entities outside the object. Private methods
cannot be accessed by entities outside the object. They are designed to be
accessed internally.
. The metaphor of a blueprint represents a class.
. Objects are the cookies.
Gaddis – Starting Out with Python, Appendix D Page
. Its purpose is to initialize an object's data attributes. It executes immediately after
the object is created.
. When a method executes, it must have a way of knowing which object's data
attributes it is supposed to operate on. That's where the self parameter comes in.
When a method is called, Python automatically makes its self parameter
reference the specific object that the method is supposed to operate on.
. By starting the attribute's name with two underscores.
. It returns a string representation of the object.
. By passing the object to the built-in str method.
. An attribute that belongs to a specific instance of a class.
.
. A method that returns a value from a class’s attribute but does not change it is
known as an accessor method. A method that stores a value in a data attribute or
changes the value of a data attribute in some other way is known as a mutator
method.
. The top section is where you write the name of the class. The middle section
holds a list of the class’s fields. The bottom section holds a list of the class’s
methods.
. A written description of the real-world objects, parties, and major events related
to the problem.
. If you adequately understand the nature of the problem you are trying to solve,
you can write a description of the problem domain yourself. If you do not
thoroughly understand the nature of the problem, you should have an expert write
the description for you.
. First, identify the nouns, pronouns, and pronoun phrases in the problem domain
description. Then, refine the list to eliminate duplicates, items that you do not
need to be concerned with in the problem, items that represent objects instead of
classes, and items that represent simple values that can be stored in variables.
. The things that the class is responsible for knowing, and the actions that the class
is responsible for doing.
. In the context of this problem, what must the class know? What must the class
do?
. No, not always.
Chapter
. A superclass is a general class and a subclass is a specialized class.
. When one object is a specialized version of another object, there is an “is a”
relationship between them. The specialized object "is a" version of the general
object.
. It inherits all of the superclass's attributes.
. Bird is the superclass and Canary is the subclass.
. I'm a vegetable.
I'm a potato.
Chapter
Gaddis – Starting Out with Python, Appendix D Page
. A recursive algorithm requires multiple method calls. Each method call requires
several actions to be performed by the JVM. These actions include allocating
memory for parameters and local variables, and storing the address of the
program location where control returns after the method terminates. All of these
actions are known as overhead. In an iterative algorithm, which uses a loop, such
overhead is unnecessary.
. A case in which the problem can be solved without recursion.
. Cases in which the problem is solved using recursion.
. When it reaches the base case.
. In direct recursion, a recursive method calls itself. In indirect recursion, method A
calls method B, which in turn calls method A.
Chapter
. The part of a computer and its operating system that the user interacts with.
. A command line interface typically displays a prompt, and the user types a
command which is then executed.
. The program.
. A program that responds to events that take place, such as the user clicking a
button.
. a) Label -- An area that displays one line of text or an image.
b) Entry -- An area in which the user may type a single line of input from
the keyboard.
c) Button -- A button that can cause an action to occur when it is clicked.
d) Frame -- A container that can hold other widgets.
. You create an instance of the Tkinter module's Tk class.
. This function runs like an infinite loop until you close the main window.
. The pack method arranges a widget in its proper position, and it makes the
widget visible when the main window is displayed.
. One will be stacked on top of the other.
. side='left'
. You use an Entry widget's get method to retrieve the data that the user has
typed into the widget.
. It is a string.
. TKinter
. Any value that is stored in the StringVar object will automatically be
displayed in the Label widget.
. You would use radio buttons.
. You would use check buttons.
. When you create a group of Radiobuttons, you associate them all with the
same IntVar object. You also assign a unique integer value to each
Radiobutton widget. When one of the Radiobutton widgets is selected, it
stores its unique integer value in the IntVar object.
Gaddis – Starting Out with Python, Appendix D Page
. You associate a different IntVar object with each Checkbutton. When a
Checkbutton is selected, its associated IntVar object will hold the value .
When a Checkbutton is deselected, its associated IntVar object will hold the
value .