pyodide: loading…

[concept]Python Basics

Dictionaries

# theory

A dict is a hashmap. Keys to values:

person = {"name": "Alice", "age": 30, "city": "Boston"}

Access with brackets or .get(). The first raises KeyError on missing keys; .get() returns None or a default:

person["name"]               # "Alice"
person.get("job")            # None
person.get("job", "unknown") # "unknown"

Add, modify, delete:

person["job"] = "engineer"
person["age"] = 31
del person["city"]

Useful methods: .keys(), .values(), .items() (iterates pairs).

Nest freely:

employees = {
    "emp001": {"name": "Alice", "dept": "Engineering"},
    "emp002": {"name": "Bob", "dept": "Marketing"},
}
employees["emp001"]["name"]  # "Alice"

Reach for a dict whenever you'd describe the relationship as "look this up by name". Counting occurrences, caching results, indexing rows by id.

# examples [3]

# example 01 · dictionary basics

Creating and accessing dictionary values

1
2
3
4
5
6
7
8
9
10
🐍
Loading PythonSetting up pandas & numpy...
# example 02 · iterating over dictionaries

Different ways to loop through dict contents

1
2
3
4
5
6
7
8
9
10
11
🐍
Loading PythonSetting up pandas & numpy...
# example 03 · nested dictionary access

Working with dictionaries inside dictionaries

1
2
3
4
5
6
7
8
9
10
11
🐍
Loading PythonSetting up pandas & numpy...

# challenges [2]

# challenge 01/02todo
Create a dictionary called 'book' with keys 'title', 'author', and 'year'. Set values to your favorite book. Print each value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🐍
Loading PythonSetting up pandas & numpy...
# challenge 02/02todo
Given grades = {'Alice': 95, 'Bob': 82, 'Carol': 91}, print the average of all grades.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🐍
Loading PythonSetting up pandas & numpy...

# project

# project-challenge

thread: SF Permits Analysis · reward: 50 xp

# brief

Create a quick lookup dictionary that maps permit numbers to their current status. This will power the status check feature in the permit tracking app.

# task

Build Permit Status Lookup

# your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
🐍
Loading PythonSetting up pandas & numpy...