Quiz 1 Study Guide
This page contains a list of things you should know before taking Quiz 1.
Types and Values
The four types of values we discussed in the first two weeks of class are: int
, str
, float
, and list
. You should know which values have which types, be able to write expressions to produce each type, and be able to tell which type each expression will produce.
Exercises
What are the types for each of the following expressions? To what value does each expression evaluate?
5
5.0
"Test"
[1,2,3]
4+6
3**2
3%2
3.0/5.0
3/5
float(5)
range(3)
"abc" + "xyz"
"a%d" % 5
"a%d%d" % (5,2)
"a%s"
str(int(float(str(3))))
raw_input()
Variables
Variables in Python and other procedural languages are mutable, meaning they can change value over time. This is unlike e.g. variables in arithmetic algebra. Know the syntax for variable assignment and the effect it has on a program.
Exercises
Which of the following are legal variable names? For each of the following which is not a legal variable name, why can’t you use it as a variable?
x
variable
four
for
2
2many
snake_case
#
What will each of the following pieces of code print? Try to figure this out without using the Python interpreter.
-
x = 2 print x
-
x = 2 x = 4 print x
-
x = 2 x = x + 1 print x
-
x = 9 y = 4 x = x/y y += x print y
Loops
In class, we have discussed the for
loop. This loop runs an indented block of code many times, setting a variable to each element of a list as it does so. Know how loops run; be able to predict what will happen when a loop is executed and understand the influence this has on variables in the program. Know the term accumulator and know how to use an accumulator with a loop to build up an answer.
Exercises
What will each of the following pieces of code print? Try to figure this out without using the Python interpreter.
-
for n in [0,1,2,3]: print "Hello"
-
for n in [0,1,2,3]: print "Hello #%d" % n
-
for n in range(5): print n+1
-
accumulator = 0 for n in range(5): accumulator = accumulator + n print accumulator
-
accum = 1 for n in range(5): accum = accum * n print accum
-
st = "" for n in range(5): st = st + "*" + str(n) for n in range(5): print st
Debugging
By this point, you have written several small programs and undoubtedly encountered mistakes that you have fixed. Know how to find mistakes in small programs. Given a description of what a program should do and what it actually does, identify how it can be fixed. (Hint: there may be more than one thing wrong with a program.)
Exercises
#1
Program
name = "Gertrude"
print "Hello" + name
Desired Output
Hello, Gertrude.
Actual Output
HelloGertrude
#2
Program
for n in range(5):
print n
Desired Output
1
2
3
4
5
Actual Output
0
1
2
3
4
#3
Program
s = ""
for num in range(4):
s = s + str(num)
for row in range(4):
print s
Desired Output
1 2 3 4
1 2 3 4
1 2 3 4
Actual Output
0123
0123
0123
0123
#4
Program
total = 1
for n in range(1,5):
print n
total = n
print "Total: %d" % total
Desired Output
1
2
3
4
5
Total: 15
Actual Output
1
2
3
4
Total: 4