Download 9781449699390_TB_ch05

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
File: chap05, Chapter 5
Multiple Choice
1. What command is used to open a file named rainfall.txt for reading?
A.
B.
C.
D.
r.open(“rainfall.txt”)
rainfall.txt.open();
open("rainfall.txt","w")
open("rainfall.txt","r")
Ans: D
Page: 157
2. How is the newline character represented in Python?
A.
B.
C.
D.
\n
/n
“n”
NL
Ans: A
Page: 157
3. How would you use a for loop to read the text from a file named myFile?
A.
B.
C.
D.
for line in myFile:
for myFile read line:
read line myFile for:
for myFile : line
Ans: A
Page: 158
4. What statement is used to close a file?
A.
B.
C.
D.
exit()
stop()
close()
quit()
Ans: C
Page: 160
5. What is the % operator called when working with strings?
A.
B.
C.
D.
modulo
format
print
file
Ans: B
Page: 161
6. When using the % operator with a string, the ____ character produces floating point
output.
A.
B.
C.
D.
d
u
f
s
Ans: C
Page 162
Case Study 1
>>>
>>>
>>>
1
>>>
>>>
2
>>>
>>>
a = 10
b = 'apple'
print("The %s costs %d cents" % (b,a))
myStr = "The %+15s costs %4.1d cents" % (b,a)
myStr
myStr = "The %+15s costs %6.1f cents" % (b,a)
myStr
3
>>> myDict = { 'name':'apple', 'cost':10, 'price':15}
>>> print("The %(name)s costs %(price)5.1f cents" % myDict)
4
7. Refer to the session in the accompanying case study. What is printed on line 1?
A.
B.
C.
D.
The apple costs
15.0 cents
'The
apple costs 10.0 cents'
'The
apple costs 10 cents'
The apple costs 10 cents
Ans: D Refer to: Case Study 1
Page: 163
8. Refer to the session in the accompanying case study. What is printed on line 3?
A.
B.
C.
D.
The apple costs
15.0 cents
'The
apple costs 10.0 cents'
'The
apple costs 10 cents'
The apple costs 10 cents
Ans: B Refer to: Case Study 1
Page: 163
9. The ____ method returns the contents of the entire file as a list of strings, where each
item in the list represents one line of the file.
A.
B.
C.
D.
readlines
readline
readall
read
Ans: A
Page: 164
10. What Python module is used to get online data?
A.
B.
C.
D.
Uniform Resource Locator
urllib.request
url.request
get.url
Ans: B
Page: 171
11. Use the ____ function to connect to an address on the Internet.
A.
B.
C.
D.
urlopen()
open()
request()
get()
Ans: A
Page: 171-172
12. The data listed below is of the ____ data type.
>>> pageText
[b'<html>\n', b'<head>\n', b' <title>Hello
Everyone</title>\n',
b'</head>\n', b'<body>\n', b' <h1>Hello Python</h1>\n',
b'</body>\n',
b'</html>\n']
A.
B.
C.
D.
string
character
byte array
URL
Ans: C
Page: 172
13. The ____ loop continues to perform the statements in the body of the loop as long as
the condition remains true.
A. for
B. while
C. priming
D. infinite
Ans: B
Page: 173
14. In hypertext markup language, the <head> tag will end with a matching ____ tag.
A. </head>
B. <body>
C. <.head>
D. {head}
Ans: A
Page: 174
15. In a URL, the ____ comes first.
A.
B.
C.
D.
host name
resource
parameters
protocol
Ans: D
Page: 177
True or False
16. Once a file has been opened, it becomes a Python object just like all other data.
Ans: True
Page: 156
17. The format() method is used to create formatted strings in Python.
Ans: False
Page: 161
18. The read(n) method will read and return a string of n characters, or zero
characters if n is not provided.
Ans: False
Page: 164
19. It is easy to read online data using Python.
Ans: True
Page: 171
20. You must have more than one for loop in a list comprehension.
Ans: False
Page: 181
Matching
21. Match each of the descriptions below with the appropriate formatting option for
strings.
___ Put the value in a field 20 characters wide, and fill in with leading zeros.
___ Put the value in a field 20 characters wide, right-justified.
___ Put the value in a field 20 characters wide, left-justified.
___ Put the value in a field 20 characters wide with two characters to the right of the
decimal point.
A.
B.
C.
D.
%-20d
%20.2f
%+20d
%020d
Ans: D, C, A, B
Page: 162
Short Answer
22. Describe the functionality of the format operator in Python.
Ans: The % operator is a string operator called the format operator. The left side of the
expression holds the template or format string, and the right side holds a collection of
values that will be substituted into the format string. Note that the number of values in the
collection on the right side corresponds with the number of % characters in the format
string. Values are taken—in order—from the collection and inserted in to the format
string.
Page: 161
Case Study 2
>>> infile = open("rainfall.txt","r")
>>> aline = infile.readline()
>>> aline
'Akron 25.81\n'
>>> infile = open("rainfall.txt","r")
>>> linelist = infile.readlines()
>>> linelist
['Akron 25.81\n', 'Albia 37.65\n', 'Algona 30.69\n',
'Allison 33.64\n',
'Alton 27.43\n', 'AmesW 34.07\n', 'AmesSE 33.95\n',
'Anamosa 35.33\n',
'Ankeny 33.38\n', 'Atlantic 34.77\n', 'Audubon 33.41\n',
'Beaconsfield 35.27\n', 'Bedford 36.35\n', 'BellePlaine
35.81\n',
'Bellevue 34.35\n', 'Blockton 36.28\n', 'Bloomfield
38.02\n',
'Boone 36.30\n', 'Brighton 33.59\n', 'Britt 31.54\n',
'Buckeye 33.66\n',
'BurlingtonKBUR 37.94\n', 'Burlington 36.94\n',
'Carroll 33.33\n', 'Cascade 33.48\n']
>>> infile = open("rainfall.txt","r")
>>> filestring = infile.read()
>>> filestring
'Akron 25.81\nAlbia 37.65\nAlgona 30.69\nAllison
33.64\nAlton
27.43\nAmesW 34.07\nAmesSE 33.95\nAnamosa 35.33\nAnkeny
33.38\n
Atlantic 34.77\nAudubon 33.41\nBeaconsfield 35.27\nBedford
36.35\n
BellePlaine 35.81\nBellevue 34.35\nBlockton
36.28\nBloomfield
38.02\nBoone 36.30\nBrighton 33.59\nBritt 31.54\nBuckeye
33.66\n
BurlingtonKBUR 37.94\nBurlington 36.94\nCarroll
33.33\nCascade 33.48\n'
>>>
23. Refer to the session in the accompanying case study and describe the function of the
readlines() method.
Ans: The readlines() method returns a list of n strings, each representing a single
line of the file. If n is not provided, then all lines of the file are returned. Refer to: Case
Study 2
Pages: 164-165
24. Refer to the session in the accompanying case study and describe the function of the
readline() method.
Ans: The readline() method returns the next line of the file with all text up to and
including the newline character. If n is provided as a parameter, then only n characters
will be returned if the line is longer than n. Refer to: Case Study 2
Pages: 164-165
25. Describe web page encoding and explain how Python can be used to decode data
read from a web site.
Ans: Unlike text files, which are simple collections of characters, web pages can be
encoded in many different ways. To be sure that they are read properly, Python returns
the contents as encoded bytes of data. If we know how the data has been encoded, then it
is easy to decode it (turn it back into a string) by using the decode method. Note that
the line
>>>decodedPageText = pageText.decode("utf-8") assumes that the text
is encoded using utf-8. The variable decodedPageText is now a standard string
that can be manipulated using any string methods and functions.
Pages: 172-173
26. Explain the while loop mechanism and provide the syntax, or template, of the
structure.
Ans: Python provides us with a general loop mechanism called a while loop. Its structure
is as follows:
while <condition>:
statement 1
statement 2
...
As you may guess from the template shown above, the while loop continues to perform
the statements in the body of the loop as long as the condition remains true. Once the
condition evaluates to false the while loop stops performing the statements in the body. A
condition can be any Python Boolean expression.
Page: 173
27. What is an infinite loop?
Ans: Note that the last thing you will do in the body of a while loop is to read the next
line. If you do not read the next line, you will continue to process the same line over and
over again in an infinite loop. Infinite loops are almost never good, and it is your
responsibility to make sure that something changes in the body of your while loop so that
the condition will eventually fail.
Page: 174
28. Describe the four different parts of a URL.
Ans: There is a lot of information packed in to a URL, and if you want to get the most
out of the urllib.request module it is important for you to understand the
components of a URL. There are four components of a URL: (1) protocol, (2) host name,
(3) resource, and (4) parameters.
The protocol tells the program that an attempt is being made to load a web page. Other
possible values for protocol include ftp for file transfer protocol, or file for a file on your
hard drive. The host name tells the program the name of the web server. This is probably
the part you are most familiar with. The resource may indicate a particular HTML file to
load. It may also be a program that you want the web server to run. If the resource is a
program, the web server will run the program and the results of the program will be sent
back to the web browser. The parameters are the last components, similar to the
parameters you pass to a function call. In this case, the parameters are passed along to the
program on the web server. Notice that the first parameter is separated from the resource
by a ?.
Pages: 177-178
29. What is a list comprehension?
Ans: The general form of a list comprehension is
[<expression>
for <item1> in <sequence1>
for <item2> in <sequence2>
...
if <condition> ]
Any of the items that correspond to a loop variable in one of the for loops can be used in
the expression. You need to have only one for loop in the list comprehension, and the if is
optional. List comprehensions allow you to easily create one list based on doing some
processing or selection criteria applied to another list.
Page: 181
30. Explain how the following session, which creates a list of cubes, could be re-written
as a single step using list comprehensions.
>>> cubes = []
>>> for x in range(1,11):
cubes.append(x*x*x)
>>> cubes
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
Ans: Using list comprehensions, we can do this in a single step:
>>> cubes = [x*x*x for x in range(1,11)]
>>> cubes
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>>
The variable x takes on the values 1 through 10 as you might expect from the for x
in range(1,11) clause in the list comprehension. Each time through the loop, the
expression x*x*x is computed and added to the list that is being constructed.
Page: 181