Lab, Practical Projects
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.
Write count_words(words), which returns a dictionary from each word to how many times it appears, using get. Print the result for ["a", "b", "a", "c", "a"].
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 ___ with a key so words are ranked by count, highest first, with ties in alphabetical order.
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.
stats should return (0, 0) for a text with no words instead of crashing, so the program prints (3, 4.0) and (0, 0). Fix it.
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 word and longest each time one of them changes, once both exist.
longest = ""
for word in ["ox", "zebra", "cat", "giraffe", "emu"]:
if len(word) > len(longest):
longest = wordSign 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: Count with collections.Counter and use most_common: no for loop.
This code works, but it counts with a hand-written loop. Rewrite it with collections.Counter and its most_common method, with no for loop. 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.
Write report(text), which returns the report below as one string: the word count, the number of unique words, the longest word (the first one if several tie) and the top two words with ties broken alphabetically. Words are lower-case runs of letters and apostrophes. Save the starter's text to notes.txt, read it back and print the report. The output should be:
Words: 9 Unique: 7 Longest: honestly Top: the 2, we 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.