4. Functions, Dictionaries & Data Drills
Practice Lab 2: Functions & Data Drills
Practice writing clean, reusable Python functions that process lists, dictionaries, and text—verified automatically with built-in assert checks right in your browser.
- Build clean, single-purpose functions returning lists, dicts, and totals
- Combine sets, dictionaries, and loops to clean tags and count word frequencies
- Verify edge cases (such as empty inputs) using live assert tests
Welcome to Practice Lab 2! Here you will write 4 small, real-world Python functions that combine everything you learned in Modules 3 and 4 (lists, sets, dictionaries, and functions with return).
Replace pass inside each function and click Run Code until every assert passes!
Drill 1: Remove Duplicates While Preserving Clean Title Case
Write clean_tags(tags) that takes a list like [" python ", "SQL", "python", " git "] and returns a sorted list of unique lowercase tags (["git", "python", "sql"]).
Click “Run Code” to execute this snippet right in your browser.
Drill 2: Word Frequency Counter
Write count_words(sentence) that splits a sentence on spaces (lowercased) and returns a dictionary counting how many times each word appears!
Example: count_words("Red fish blue fish") → {"red": 1, "fish": 2, "blue": 1}
Click “Run Code” to execute this snippet right in your browser.
Drill 3: Safe Phonebook Lookup
Write find_phone(contacts, name) where contacts is a dictionary like {"Alex": "555-0101", "Maya": "555-0199"}. It should return the phone number if found, or "Not found" if missing (never crashing with KeyError!).
Click “Run Code” to execute this snippet right in your browser.
Drill 4 (Challenge): Shopping Cart Subtotal
Write cart_total(items) where items is a list of dictionaries, each with "price" and "qty". Return the rounded total (round(..., 2)).
Click “Run Code” to execute this snippet right in your browser.