Exercises for Lesson 6
Exercise 1: Basic list operations
For this exercise, we’ll use the following statements:
numbers = [3, 1, 4, 1, 5]
words = ["apple", "banana", "cat", "dog", "elephant"]Predict the result of evaluating each of the following list expressions.
a) numbers[-1]
b) words[1:4]
c) words[1] + words[2]
d) len(numbers)
e) numbers + words
f) 2 * numbers + words
g) for word in words:
print(len(word), word)Exercise 2: Going backwards
Write a program to print a sequence of words in reverse order. You cannot use slicing or the existing reverse list method.
(Hint: you should use split to turn the provided string into a list, and use the accumulator pattern to build the resulting list in reversed order. Think carefully about how you can change the typical body of an accumulator loop to put the new value at the beginning of the accumulator variable instead of the end of the variable.)
Here is an example interaction:
Please enter a sequence of words, separated by spaces: apple banana cat dog elephant fish
The sequence reversed:
fish elephant dog cat banana apple
Exercise 3: for-loop patterns
Write a for loop using the variables numbers and words to generate each output. Think about whether you need the element and/or its index for each loop.
numbers = [3, 1, 4, 1, 5]
words = ["apple", "banana", "cat", "dog", "elephant"]
a) apple 5
banana 6
cat 3
dog 3
elephant 8
b) 0 apple
1 banana
2 cat
3 dog
4 elephant
c) 3
1
4
1
5
Exercise 4: list operations
Let’s explore a few more list operations.
Predict the values of numbers and words after executing each of the following statements. Assume that numbers and words are reset to their original values before each statement.
numbers = [3, 1, 4, 1, 5]
words = ["apple", "banana", "cat", "dog", "elephant"]
a) "apple" in words
b) "nana" in words
c) numbers.remove(2)
d) numbers.sort()
e) words.sort(reverse=True)
f) numbers.append([words.index("cat")])
g) words.pop(numbers.pop(3))
h) words.insert(numbers[0], "fish")Exercise 5: Drawing many shapes
Write a function that takes in lists of colors and radii and a Point, and draws concentric circles centered at that Point. The function signature and main are given for you.
from graphics import *
def drawCircles(colorList, radiusList, point, win):
"""
Draws circles all centered at the same point.
colorList: color of each circle (a list of strings)
radiusList: radius of each circle (a list of integers)
point: the center of each circle (a Point)
win: the window in which to draw (a GraphWin)
"""
# TODO
def main():
win = GraphWin("Circles!")
colors = ["purple", "blue", "green", "yellow", "orange", "red"]
radii = [100, 80, 60, 40, 50, 10]
center = Point(100, 100)
drawCircles(colors, radii, center, win)
win.getMouse()
main()