These problems are for practice. They are optional, but you are encouraged to complete them and discuss them with members of the course staff during lab and office hours.
0 - List Basics
What is printed by the following program?
m = ['mock', 'your', 'own', 'grinning']
for i in range(len(m):
print(m[i], end=" ")
- Remember that lists are indexed starting with 0
- Calling the
len
function on a list returns how many elements that list has
- Calling
range
on that length returns a collection identical to the list’s indices
- List
m
is of length 4
range(4)
corresponds to the collection 0, 1, 2, 3
, precisely the indices of list m
- Adding the argument
end=" "
to the print
function replaces the default end behavior (a new line) with a string consisting of a single space
- Remember that parentheses must be matched. Check the code above closely.
1 - List Basics
What is printed by the following program?
X = ['such', 'convergence', 'or', 'projection']
for i in X:
print(i, end=" ")
2 - List Basics
What is printed by the following program?
Z = ['raining', 'out', 'of', 'a', 'low', 'sky']
for i in range(length(Z)):
print(i, end=" ")
3 - List Mutability
What is printed by the following program?
nonsense = ['mimsy', 'were', 'the', 'borogroves']
serious_business = nonsense
serious_business = ['The', 'price', 'of', 'steel', 'rose']
print(nonsense[3])
4 - List Mutability
What is printed by the following program?
scenery = ['meteors', 'klaxoned', 'overhead']
for s in scenery:
s = 'furnace'
print(scenery[0])
5 - List Mutability
What is printed by the following program?
some_numbers = [1, 5, 6, 2, 3, 7]
for i in range(len(some_numbers)):
some_numbers[i] += 1
print(some_numbers[2])
6 - List Mutability
What is printed by the following program?
E = [14, 15, 17]
for e in E:
e -= 1
print(E[1])
7 - Conditionals Basics
What is printed by the following program?
x = 2
if x > 2:
print('x is greater than 2')
8 - Conditionals Basics
What is printed by the following program?
x = 4
if (x % 5) == 0:
print('x is a multiple of 5')
else:
print('x is not a multiple of 5')
9 - Conditionals Basics
What is printed by the following program?
y = 'optimization'
if len(y) > 7:
print('the string is long')
elif len(y) == 1:
print('the string is a character')
else:
print('the string is short')
10 - Nesting Conditionals
What is printed by the following program?
year = 2018
if year < 2023:
print('in the past')
if year > 2000:
print('this century')
else:
print('last century')
else:
print('in the future')
How would the output change if line 1 read year = 2000
? How would the output change if line 1 read year = 2045
?
11 - Conditionals and Loops
What is printed by the following program? If there an error, simply identify that there is an error.
some_values = [1, 2, -3, 4, -5, 6, 7, 8, 9, -11, 12]
for s in some_values:
if s > 0:
print(s, end = " ")
12 - Conditionals and Loops
What is printed by the following program? If there an error, simply identify that there is an error.
some_values = [1, 2, -3, 4, -5, 6, 7, 8, 9, -11, 12]
for s in some_values:
if s > 0:
print(s, end = " ")
else:
print(s * -1, end=" ")
13 - Conditionals and Loops
What is printed by the following program? If there an error, simply identify that there is an error.
some_values = [3, 6, -2, 3, 0, -3]
for s in some_values:
if s > 0:
print(12/s, end = " ")
elif s < 0:
print(-12/s, end=" ")
14 - Conditionals and Loops
What is printed by the following program? If there an error, simply identify that there is an error.
some_values = [3, 6, -2, 3, 0, -3]
for s in some_values:
if s > 0:
print(12/s, end = " ")
else:
print(-12/s, end=" ")
15 - Conditionals and Loops
What is printed by the following program? If there an error, simply identify that there is an error.
some_values = [3, 6, -2, 3, 0, -3]
for s in some_values:
if s > 0:
some_values[s]
else:
some_values[-1*s]