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.
Create a dictionary capitals mapping "France" to "Paris" and "Japan" to "Tokyo". Add "Kenya" with "Nairobi". Then print The capital of Japan is Tokyo using a lookup, and print how many countries there are: 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 03 .Fill the marked gaps in the starter code.
Replace each ___ so the program prints each subject and mark, then the average: maths 72, art 90, music 84, Average: 82.0.
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 crashes with a KeyError when a product is not in the price list. Fix it so a missing product costs 0 and the program prints Basket: 4.5.
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.
Each row shows colour and counts each time either changes, once both exist. Write dictionaries the way Python prints them.
counts = {}
for colour in ["red", "blue", "red"]:
counts[colour] = counts.get(colour, 0) + 1Sign 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 counts.get(animal, 0) + 1 instead of an if-else.
This counter uses an if-else to handle new keys. Rewrite the loop body as one line using get() with a default, with no if. The output must not change.
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.
Count how often each word appears in sentence, storing the counts in the dictionary counts. Then print each word and its count, in alphabetical order, one pair per line (for example and 2).
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.