Deck 5: Bigger Data: File Io: the While Loop, String Formatting, Reading Online Data in CSV and Json Formats

ملء الشاشة (f)
exit full mode
سؤال
What command is used to open a file named rainfall.txt for reading?

A) r.open("rainfall.txt")
B) rainfall.txt.open();
C) open("rainfall.txt", "w")
D) open("rainfall.txt", "r")
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
How is the newline character represented in Python?

A) \n
B) /n
C) "n"
D) NL
سؤال
How would you use a for loop to read the text from a file named myFile?

A) for line in myFile:
B) for myFile read line:
C) read line myFile for:
D) for myFile : line
سؤال
What statement is used to close a file?

A) exit()
B) stop()
C) close()
D) quit()
سؤال
What string method is used to format values in a string?

A) modulo
B) format
C) close
D) file
سؤال
When using the format method with a string, the ____ character formats a value as an integer.

A) d
B) e
C) f
D) s
سؤال
Case Study 1:
1. >>> item = 'A dozen eggs'
2. >>> itemPrice = 2.4
3. >>> print("{0} costs ${1:.2f}".format(item, itemPrice))
4. ???
5. >>> myDict = {'name':'candy bar', 'price':95}
6. >>> print("The {name} costs {price} cents".format(**myDict))
7. ???
-Refer to the session in the accompanying Case Study 1. What is printed on line 4?

A) A dozen eggs costs $2.4
B) A dozen eggs costs $2.40
C) {0} costs $2.4f
D) The {item} costs {itemPrice} cents
سؤال
Case Study 1:
1. >>> item = 'A dozen eggs'
2. >>> itemPrice = 2.4
3. >>> print("{0} costs ${1:.2f}".format(item, itemPrice))
4. ???
5. >>> myDict = {'name':'candy bar', 'price':95}
6. >>> print("The {name} costs {price} cents".format(**myDict))
7. ???
-Refer to the session in the accompanying Case Study 2. What is printed on line 7?

A) The candy bar costs 95 cents
B) The candy bar costs .95 cents
C) The {name} costs {price} cents
D) The 'name:candy bar' costs 'price:95' cents
سؤال
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) read
B) readline
C) readall
D) readlines
سؤال
What Python module is used to get online data?

A) Uniform Resource Locator
B) urlliB)request
C) url.request
D) get.url
سؤال
Use the ____ function to connect to an address on the Internet.

A) urlopen()
B) open()
C) request()
D) get()
سؤال
The data embedded in a file that describes the data is called:

A) superdata.
B) titledata.
C) metadata.
D) html.
سؤال
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
سؤال
If the condition in a while loop never becomes False, (a/an) _____________ loop results.

A) finite
B) infinite
C) complimentary
D) empty
سؤال
Using JSON, data is formatted most like which Python collection?

A) Tuple
B) String
C) List
D) Dictionary
سؤال
Once a file has been opened, it becomes a Python object just like all other data.
سؤال
The string format method is used to create formatted strings in Python.
سؤال
The read(n) method will read and return a string of n characters, or zero characters if n is not provided.
سؤال
It is possible to read online data using Python.
سؤال
You must have more than one for loop in a list comprehension.
سؤال
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide, centered.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
سؤال
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide, right-justified.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
سؤال
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide, left-justified.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
سؤال
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide with two characters to the right of the decimal point.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
سؤال
Describe the functionality of the string format method in Python.
سؤال
Case Study 2:
>>> with open("rainfall.txt", "r") as inFile:
aLine = inFile.readline()
>>> aLine
'Akron 25.81\n'
>>> with open("rainfall.txt", "r") as inFile:
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']
>>> with open("rainfall.txt", "r") as inFile:
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\nBellePlaine 35.81\nBellevue 34.35\nBlockton 36.28\nBloomfield 38.02\nBoone 36.30\nBrighton 33.59\nBritt 31.54\nBuckeye 33.66\nBurlingtonKBUR 37.94\nBurlington 36.94\nCarroll 33.33\nCascade 33.48\n'
>>>
-Refer to the session in the accompanying Case Study 2 and describe the function of the readlines() method.
سؤال
Case Study 2:
>>> with open("rainfall.txt", "r") as inFile:
aLine = inFile.readline()
>>> aLine
'Akron 25.81\n'
>>> with open("rainfall.txt", "r") as inFile:
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']
>>> with open("rainfall.txt", "r") as inFile:
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\nBellePlaine 35.81\nBellevue 34.35\nBlockton 36.28\nBloomfield 38.02\nBoone 36.30\nBrighton 33.59\nBritt 31.54\nBuckeye 33.66\nBurlingtonKBUR 37.94\nBurlington 36.94\nCarroll 33.33\nCascade 33.48\n'
>>>
-Refer to the session in the accompanying Case Study 2 and describe the function of the readline() method.
سؤال
Describe JSON encoding and explain how Python can be used to decode JSON data read from the Internet.
سؤال
Explain the while loop mechanism and provide the syntax, or template, of the structure.
سؤال
What is an infinite loop?
سؤال
Describe the format of a CSV file.
سؤال
What is a list comprehension?
سؤال
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]
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/33
auto play flashcards
العب
simple tutorial
ملء الشاشة (f)
exit full mode
Deck 5: Bigger Data: File Io: the While Loop, String Formatting, Reading Online Data in CSV and Json Formats
1
What command is used to open a file named rainfall.txt for reading?

A) r.open("rainfall.txt")
B) rainfall.txt.open();
C) open("rainfall.txt", "w")
D) open("rainfall.txt", "r")
D
2
How is the newline character represented in Python?

A) \n
B) /n
C) "n"
D) NL
A
3
How would you use a for loop to read the text from a file named myFile?

A) for line in myFile:
B) for myFile read line:
C) read line myFile for:
D) for myFile : line
A
4
What statement is used to close a file?

A) exit()
B) stop()
C) close()
D) quit()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
5
What string method is used to format values in a string?

A) modulo
B) format
C) close
D) file
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
6
When using the format method with a string, the ____ character formats a value as an integer.

A) d
B) e
C) f
D) s
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
7
Case Study 1:
1. >>> item = 'A dozen eggs'
2. >>> itemPrice = 2.4
3. >>> print("{0} costs ${1:.2f}".format(item, itemPrice))
4. ???
5. >>> myDict = {'name':'candy bar', 'price':95}
6. >>> print("The {name} costs {price} cents".format(**myDict))
7. ???
-Refer to the session in the accompanying Case Study 1. What is printed on line 4?

A) A dozen eggs costs $2.4
B) A dozen eggs costs $2.40
C) {0} costs $2.4f
D) The {item} costs {itemPrice} cents
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
8
Case Study 1:
1. >>> item = 'A dozen eggs'
2. >>> itemPrice = 2.4
3. >>> print("{0} costs ${1:.2f}".format(item, itemPrice))
4. ???
5. >>> myDict = {'name':'candy bar', 'price':95}
6. >>> print("The {name} costs {price} cents".format(**myDict))
7. ???
-Refer to the session in the accompanying Case Study 2. What is printed on line 7?

A) The candy bar costs 95 cents
B) The candy bar costs .95 cents
C) The {name} costs {price} cents
D) The 'name:candy bar' costs 'price:95' cents
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
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) read
B) readline
C) readall
D) readlines
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
10
What Python module is used to get online data?

A) Uniform Resource Locator
B) urlliB)request
C) url.request
D) get.url
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
11
Use the ____ function to connect to an address on the Internet.

A) urlopen()
B) open()
C) request()
D) get()
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
12
The data embedded in a file that describes the data is called:

A) superdata.
B) titledata.
C) metadata.
D) html.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
14
If the condition in a while loop never becomes False, (a/an) _____________ loop results.

A) finite
B) infinite
C) complimentary
D) empty
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
15
Using JSON, data is formatted most like which Python collection?

A) Tuple
B) String
C) List
D) Dictionary
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
16
Once a file has been opened, it becomes a Python object just like all other data.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
17
The string format method is used to create formatted strings in Python.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
18
The read(n) method will read and return a string of n characters, or zero characters if n is not provided.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
19
It is possible to read online data using Python.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
20
You must have more than one for loop in a list comprehension.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
21
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide, centered.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
22
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide, right-justified.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
23
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide, left-justified.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
24
1. Match each of the descriptions with the appropriate formatting option for strings.
-Put the value in a field 20 characters wide with two characters to the right of the decimal point.

A) {0:^20d}
B) {0:>20d}
C) {0:&It;20d}
D) {0:20.2f}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
25
Describe the functionality of the string format method in Python.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
26
Case Study 2:
>>> with open("rainfall.txt", "r") as inFile:
aLine = inFile.readline()
>>> aLine
'Akron 25.81\n'
>>> with open("rainfall.txt", "r") as inFile:
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']
>>> with open("rainfall.txt", "r") as inFile:
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\nBellePlaine 35.81\nBellevue 34.35\nBlockton 36.28\nBloomfield 38.02\nBoone 36.30\nBrighton 33.59\nBritt 31.54\nBuckeye 33.66\nBurlingtonKBUR 37.94\nBurlington 36.94\nCarroll 33.33\nCascade 33.48\n'
>>>
-Refer to the session in the accompanying Case Study 2 and describe the function of the readlines() method.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
27
Case Study 2:
>>> with open("rainfall.txt", "r") as inFile:
aLine = inFile.readline()
>>> aLine
'Akron 25.81\n'
>>> with open("rainfall.txt", "r") as inFile:
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']
>>> with open("rainfall.txt", "r") as inFile:
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\nBellePlaine 35.81\nBellevue 34.35\nBlockton 36.28\nBloomfield 38.02\nBoone 36.30\nBrighton 33.59\nBritt 31.54\nBuckeye 33.66\nBurlingtonKBUR 37.94\nBurlington 36.94\nCarroll 33.33\nCascade 33.48\n'
>>>
-Refer to the session in the accompanying Case Study 2 and describe the function of the readline() method.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
28
Describe JSON encoding and explain how Python can be used to decode JSON data read from the Internet.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
29
Explain the while loop mechanism and provide the syntax, or template, of the structure.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
30
What is an infinite loop?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
31
Describe the format of a CSV file.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
32
What is a list comprehension?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
33
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]
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 33 في هذه المجموعة.