python data structures coursera week 4 quiz answers
Chapter 8 Quiz
1. How are "collection" variables different from normal variables?
- Collection variables can store multiple values in a single variable
- Collection variables merge streams of output into a single stream
- Collection variables can only store a single value
- Collection variables pull multiple network documents together
2. What are the Python keywords used to construct a loop to iterate through a list?
- for / in
- foreach / in
- def / return
- try / except
3. For the following list, how would you print out 'Sally'?
friends = [ โJosephโ, โGlennโ, โSallyโ ]
- print friends[3]
- print(friends[โSallyโ])
- print(friends[2])
- print(friends[2:1])
4. What would the following Python code print out?
fruit = โBananaโ
fruit[0] = โbโ
print(fruit)
- Banana
- Nothing would print โ the program fails with a traceback error
- banana
- [0]
- b
- B
5. Which of the following Python statements would print out the length of a list stored in the variable data?
- print(strlen(data))
- print(len(data))
- print(data.length())
- print(data.length)
- print(data.Len)
- print(length(data))
6. What type of data is produced when you call the range() function?
x = list(range(5))
- A boolean (true/false) value
- A string
- A list of integers
- A list of words
- A list of characters
7. What does the following Python code print out?
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(len(c))
- 21
- 15
- [1, 2, 3]
- [4, 5, 6]
- [1, 2, 3, 4, 5, 6]
- 6
8. Which of the following slicing operations will produce the list [12, 3]?
t = [9, 41, 12, 3, 74, 15]
- t[1:3]
- t[12:3]
- t[2:4]
- t[:]
- t[2:2]
9. What list method adds a new item to the end of an existing list?
- push()
- forward()
- index()
- append()
- pop()
- add()
10. What will the following Python code print out?
friends = [ โJosephโ, โGlennโ, โSallyโ ]
friends.sort()
print(friends[0])
- friends
- Joseph
- Glenn
- Sally
Assignment 8.4
8.4 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output. You can download the sample data at http://www.py4e.com/code3/romeo.txtYou can download the sample data at http://www.py4e.com/code3/words.txt
file_name = โromeo.txtโ
try:
file = open(file_name, โrโ)
except FileNotFoundError:
print(fโFile {file_name} not found.โ)
quit()
unique_words = []
for line in file:
words = line.split()
for word in words:
if word not in unique_words:
unique_words.append(word)
unique_words.sort()
print(unique_words)
Assignment 8.5
8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line: From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end. Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count. You can download the sample data at http://www.py4e.com/code3/mbox-short.txt
file_name = โmbox-short.txtโ
try:
file = open(file_name, โrโ)
except FileNotFoundError:
print(fโFile {file_name} not found.โ)
quit()
count = 0
for line in file:
if line.startswith(โFrom โ):
words = line.split()
email = words[1]
print(email)
count += 1
print(fโThere were {count} lines in the file with From as the first wordโ)