Phase 2: Programming Fundamentals II · ~35 minutes · Python · uv
Dictionaries and Sets — Lookup-Based Collections
prices["mango"] doesn't return None when the key is missing. It stops your program with a KeyError — on purpose.
Hiring signal: Reaches for .get() with an explicit default instead of guessing that a missing key silently returns None
What you will learn
- Create, access, and update dictionaries; use .get() to safely handle missing keys
- Explain why a missing dictionary key raises KeyError instead of returning None
- Use a set for uniqueness and fast membership testing
- Predict whether a given .get() call returns a real value or a default
Introduction
Type: Learn Languages: Python Prerequisites: Lesson 01 (Lists and Tuples) Time: ~35 minutes
Objective
Learning objectives
- Create, access, and update dictionaries; use
.get() to safely handle missing keys - Explain why a missing dictionary key raises
KeyError instead of returning None - Use a set for uniqueness and fast membership testing
- Predict whether a given
.get() call returns a real value or a default
What you're building
A script (word_counter.py) that:
- Takes a real short block of text (at least 3 sentences, hardcoded as a string is fine)
- Builds a dictionary counting how many times each word appears, using the
if word in counts pattern - Rewrites the same counting logic a second time using
.get(word, 0) + 1 instead, producing an identical result - Prints the 3 most frequent words, using
sorted(counts.items(), key=..., reverse=True) - Separately, builds a
set of all unique words and prints how many total words vs. unique words there were
scores = {"Ada": 92, "Grace": 0}. What does scores.get("Grace", "no score") return?
This is the exact trap from this lesson's inventory example. .get()'s fallback only triggers when the key doesn't exist in the dictionary at all. "Grace" absolutely exists as a key here — its value just happens to be 0, which is a completely valid, real value. .get() has no concept of "this value looks empty," only "this key does or doesn't exist."
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy