- Dictionaries: creating, accessing, updating, removing
- Dictionary methods:
.get(),.keys(),.values(),.items(),.update() - Iterating through dictionaries
- Nested dictionaries and lists of dictionaries
- Sets: unique collections and set operations (
|,&,-,^) - Using dictionaries and sets to solve practical problems
Create a dictionary grades with at least 5 students (name -> numeric score). Then:
- Print each student and their grade
- Print the class average
- Print the highest-scoring student
Write a function count_letters(text) that returns a dictionary mapping each letter to how many times it appears in text. Ignore spaces and treat upper/lowercase as the same letter.
Test with: "Hello World"
Two people made shopping lists mapping item -> quantity. Write merge_lists(list1, list2) that returns a combined dictionary where quantities are summed for items that appear in both.
list1 = {"milk": 2, "bread": 1, "eggs": 12}
list2 = {"bread": 2, "cheese": 1, "milk": 1}
# Expected: {"milk": 3, "bread": 3, "eggs": 12, "cheese": 1}Build a small contact manager where each contact is a dictionary with keys: name, phone, email. Store them all in a list called contacts. Write these functions:
add_contact(contacts, name, phone, email)find_by_name(contacts, name)- returns the contact dict orNoneall_emails(contacts)- returns a list of every email address
Add 3 contacts and test each function.
Two classrooms submitted their favorite movies. Using sets, find:
- Movies liked by BOTH classrooms
- Movies liked by only ONE of them
- The total number of distinct movies
class_a = ["Inception", "Matrix", "Interstellar", "Joker", "Matrix"]
class_b = ["Matrix", "Titanic", "Joker", "Avatar", "Titanic"]Create a dictionary that maps English words to another language (at least 8 words). Write translate(sentence, dictionary) that returns the sentence with each known word replaced. Unknown words stay unchanged.
translator = {"hello": "bonjour", "cat": "chat", "dog": "chien"}
translate("hello my cat and dog", translator)
# -> "bonjour my chat and chien"- Complete all tasks in
homework.py - Push your work to your GitHub repository
- Share the repository link with the instructor
python homework.py