Scripting with Python and SQL Week1

docx

School

Grand Rapids Community College *

*We aren’t endorsed by this school

Course

156

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

3

Uploaded by ConstableWildcatMaster401

Report
Congratulations! You passed! Grade received 100% Latest Submission Grade 100% To pass 80% or higher Go to next item 1. Question 1 How do you retrieve the index number for a string called "file" in a list variable named items ? 1 / 1 point items["file"] items.file(index) items.index("file") Correct Correct! .index() is the method you need to retrieve the index which requires the existent item ( "file" in this case) as an argument. 2. Question 2 How do you append the string "Downloads" to the end of a list named directories ? 1 / 1 point directories.insert(-1, "Downloads") directories.append("Downloads") directories.add(-1, "Downloads") Correct Correct! The .append() method along with the "Downloads" string would add it to the end of the list 3. Question 3 Given the following dictionary: address = {"street": "main", "number": 1044, "province": "Guthenfoord"} What would happen if you used address["Guthenfoord"] ? 1 / 1 point A KeyError would be raised A None value would be returned The string "province" would be returned Correct Correct. A KeyError is raised when trying to retrieve a value for a key that doesn't exist. In this case "Guthenfoord" is not a valid key 4. Question 4 For a dictionary named cpu_specs , for which you don't know what the keys are, what is a way to retrieve the value for "cores" without producing an exception? 1 / 1 point cpu_specs.get("cores") cpu_specs["cores"] cpu_specs.find("cores") Correct
That's right. The .get() method would not raise an exception if "cores" doesn't exist as a key. It would return a None 5. Question 5 How can you retrieve a fallback value of 0 for the dimensions dictionary when the "area" key does not exist? 1 / 1 point dimensions["area", 0] dimensions.get("area", 0) dimensions["area"] or 0 Correct That's correct! The second argument to the .get() method will use it as a return value if "area" doesn't exist. By default this value is a None 6. Question 6 For a dictionary named metadata , what is the right way to start a loop to use both the key and value? 1 / 1 point for key, value in metadata for key, value in metadata.items() for key, value in metadata.all() Correct That's right! .items() is the method you would need to work with both the key and value of every item in the dictionary 7. Question 7 What is one distinct attribute that describes a Python set ? 1 / 1 point It is similar to a list, but immutable It is similar to a list but faster Every item in a set is guaranteed to be unique within the set. Correct Correct! This attribute of Python sets makes it useful when trying to ensure uniqueness across all items within the set. 8. Question 8 What is a way you can open a file with Python that doesn't require you to close the file after loading it? 1 / 1 point with open("dataset.csv") as data open("dataset.csv") data = open("dataset.csv") Correct Correct! Using the with statement would ensure that the file is closed after the indented block ends. This way of reading a file doesn't require you to close the file after you are done reading it. 9. Question 9 What is the right call to serialize a json_string variable containing a JSON string to Python? 1 / 1 point json.loads(json_string)
json.load(json_string) json.dumps(json_string) Correct Correct! The json.loads() call will serialize the JSON string into Python 10. Question 10 What would be a way to save a Python variable named data as JSON to a file? 1 / 1 point with open("data.json", "w") as _f: json.dumps(data, _f) with open("data.json", "w") as _f: json.load(data, _f) with open("data.json", "w") as _f: json.dump(data, _f) Correct That's right! json.dump() is the correct call for this, where data would get serialized to JSON and saved to the previously opened file.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help