Python by Projects › Checkpoint 3 — Data Wrangler
Questions: Checkpoint 3 — Data Wrangler
A data wrangler tidies and summarizes messy data; use what you learned this term to do the same.
Question 1
Start with word = "wizardry". Use one print(...) call with one f-string to combine the first character (word[0]), the last character (word[-1]), the slice word[2:5], and the reversed word (word[::-1]).
How this is checked: You check this one yourself, against the list below.
The site cannot check this one. Run your program, then tick each item below when your work does it.
Question 2
Start with phrase = " Hello, Data World! ". Build cleaned = phrase.strip().lower().replace(",", ""). Then print the result of "data" in cleaned as a True/False value.
How this is checked: Checked by tests that run your code.
Question 3
Start with scores = [88, 92, 75]. Append 100 to scores, then print the first score (scores[0]) and the last score (scores[-1]).
How this is checked: Checked by tests that run your code.
Question 4
Start with scores = [88, 92, 75, 100]. Use a loop to print each score. Then print len(scores), max(scores), and min(scores). Finally, start total at 0, use another loop over the integers in scores to add each score to total, and print total. Do not calculate an average.
How this is checked: Checked by tests that run your code.
Question 5
Start with scores = [88, 92, 75, 100]. Sort scores in place by calling scores.sort(reverse=True). Then print the top three scores using indexes [0], [1], and [2].
How this is checked: Checked by tests that run your code.
Question 6
Start with prices = {"apple": 3, "pear": 2}. Print prices["pear"], print prices.get("fig", 0), and print the True/False result of "apple" in prices.
Next, write a three-way branch using membership tests only. If "plum" in prices, print its price. Otherwise, use elif to check whether "pear" in prices and print its price. In the else branch, print "sold out".
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: several lines
Question 7
Start with words = ["cat","dog","cat","bird","cat"] and counts = {}. Use exactly one loop over words to build the counts. Inside that loop, if word in counts, use counts[word] = counts[word] + 1; otherwise, use counts[word] = 1.
After the counting loop, use a loop over counts.items() to print each word and its count.
How this is checked: Checked by tests that run your code.
Question 8
This code crashes when it tries to look up a missing price:
prices = {"apple": 3}
print(prices["fig"])
Here is the actual traceback from running that code:
Traceback (most recent call last):
File "checkpoint.py", line 2, in <module>
print(prices["fig"])
~~~~~~^^^^^^^
KeyError: 'fig'
First read the traceback from the bottom up. In a comment in your answer cell, write the error name and missing key. Then rewrite the code safely using the dictionary method you learned so a missing "fig" price becomes 0, and print that price without crashing.
How this is checked: Checked by comparing what your program prints with the expected output.
Answer format: a number