Lab, Dictionaries and Sets
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Use a defaultdict(list) called groups to group the words in words by their length, then print each length and its words in order of length:
3 ['cat', 'dog'] 4 ['bird'] 5 ['horse', 'zebra']
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace each ___ with a deque method so the program serves the first customer and lets a late VIP jump the queue, printing Served: Ana and Next: ['VIP', 'Bo', 'Cy'].
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
This program should total minutes of practice per instrument and print piano 50 then violin 15, but it crashes. Fix the line that creates totals.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
The deque keeps only the last two readings. Each row shows reading and window each time either changes, once both exist. A deque prints as deque([...], maxlen=2).
from collections import deque
window = deque(maxlen=2)
for reading in [5, 8, 3]:
window.append(reading)Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Use Counter and most_common instead of counting and searching by hand.
This program counts colours by hand. Rewrite it with Counter and most_common(1), with no loop. The output must stay Most popular: blue 3.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Each survey answer is a Answer named tuple with name and pet. Print the two most common pets with their counts, then each pet with its owners' names, in alphabetical order of pet:
cat 3 dog 2 cat ['Ana', 'Cy', 'Eli'] dog ['Bo', 'Fay'] fish ['Dee']
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.