In addition to the concepts below, you should also know the
concepts from Quiz 1,
Quiz 2, and Quiz 3.
Many of the earlier concepts -- especially functions -- are fundamental
to what we've been studying recently.
You should be able to define the following terms:
- definite loop
- indefinite loop
- top-down design
- unit testing
- bottom-up development
- stubbed-out function
- pseudocode
- file object
- newline character
- lists and strings as objects
- list of lists
You should understand and be able to use the following Python concepts:
- while (indefinite) loops
- nested loops
- the file methods open() and close()
- definite loop on a file (e.g., for line in infile:)
- the string strip(), split() methods
- the list append() method
- python in (and not in) operator used on lists and strings
- You should also be able to write basic operations using lists and files,
such as:
- read and print all lines in a file
- read all lines in a file and store as a list
Practice problems:
- Write a while loop that prints out all even numbers from 2 to 20.
- Write a program that reads in the following data from a file
and computes the average value:
98
100
91
82
88
86
77
- Given any specific definite (for) loop, write an indefinite (while) loop
that performs the same computation.
- Discussion question 3 on page 261
- Write a function called getLetter() that asks the user for a letter
and returns the given letter. Your function should check to make sure the user
enters just a single letter. If the user enters anything other than a single
letter, the function should ask again, until it receives valid input.
- Given the assignments for s and L, what is the value and type of
each expression? Try them in python to check your answers.
L = ["There are", "many", "like it,", "but this one", "is mine."]
s = "abcdefg"
len(L)
len(s)
L[3].split()
L[3].split("h")
"a" in L[2]
"DEF" in s
- Trace through the following code, show the output from its entire
execution, and draw the stack before the return statement in
function foo:
def main(): OUTPUT STACK
print "in main"
b = foo(5)
print len(b)
print b[0]
print b[1][1]
def foo(n):
print "in foo"
biglist = []
for i in range (n):
sublist = ["h", i, i+10]
biglist.append(sublist)
# draw stack here
return biglist
main()
What is the type and value of each of the following expressions:
value type
(1) biglist
(2) biglist[2]
(3) biglist[2][0]
(4) biglist[1][2]
(5) len(biglist)
(6) len(biglist[1])
- Write a program that reads in a list of words from a file infile.txt into a list of strings, and then calls a function (you write)
MakeAcronym that takes the list and returns a string that
is the acronym of all words in the list and prints it out. For example,
if the input file is:
Thanks
Goodness
It's
Friday
Your program will print out the string "TGIF"
include("../style/footer.php"); ?>