Lesson 9: Dictionaries & Sets

1. Dictionaries – Key-Value Pairs

Dictionaries store data in key:value pairs — like a real dictionary (word → definition).

Very useful in ML for storing hyperparameters, model configs, or data mappings.

student = {
    "name": "Alex",
    "age": 22,
    "grade": "A",
    "skills": ["Python", "ML"]
}
print(student["name"])  # Alex

Keys must be unique and immutable (strings, numbers, tuples).

Exercise 1

Let

config = {"learning_rate": 0.01, "epochs": 100}
config["batch_size"] = 32
print(config["learning_rate"]) → 
print(config["epochs"]) → 

2. Common Dictionary Methods

Example:

model = {"type": "CNN", "layers": 5}
model.get("optimizer", "Adam")  # Adam (default if missing)

Exercise 2

What is the output?
d = {"a": 1, "b": 2}
print(d.get("c", 0))

Exercise 3

Which are valid dictionary keys?
← Previous Lesson (8) Next Lesson (10) →