-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
299 lines (233 loc) · 9.9 KB
/
example.py
File metadata and controls
299 lines (233 loc) · 9.9 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Dictionaries — Example Code
=============================
Run this file:
python3 example.py
A complete tour of Python dictionaries — creating them, reading from them,
modifying them, and all the handy tricks you'll use every day.
"""
# -----------------------------------------------------------------------------
# 1. Creating dictionaries
# -----------------------------------------------------------------------------
# Literal syntax — the most common way
person = {"name": "Alice", "age": 30, "city": "Portland"}
print("Literal syntax:", person)
# dict() constructor with keyword arguments
person2 = dict(name="Bob", age=25, city="Seattle")
print("dict() constructor:", person2)
# From a list of tuples
person3 = dict([("name", "Carol"), ("age", 28), ("city", "Denver")])
print("From tuples:", person3)
# From two lists using zip()
keys = ["name", "age", "city"]
values = ["Dave", 35, "Austin"]
person4 = dict(zip(keys, values))
print("From zip():", person4)
# Empty dictionary
empty = {}
print("Empty dict:", empty, "— length:", len(empty))
# -----------------------------------------------------------------------------
# 2. Accessing values: [] vs .get()
# -----------------------------------------------------------------------------
user = {"name": "Alice", "age": 30, "email": "alice@example.com"}
# Square brackets — fast and direct, but crashes on missing keys
print("\nuser['name']:", user["name"])
print("user['email']:", user["email"])
# .get() — returns None if the key is missing (no crash!)
print("user.get('name'):", user.get("name"))
print("user.get('phone'):", user.get("phone")) # None
print("user.get('phone', 'N/A'):", user.get("phone", "N/A")) # Custom default
# This would crash: user["phone"] — uncomment to see the KeyError
# print(user["phone"])
# -----------------------------------------------------------------------------
# 3. Adding, updating, and deleting entries
# -----------------------------------------------------------------------------
print("\n--- Adding and updating ---")
inventory = {"apples": 5, "bananas": 3}
print("Starting inventory:", inventory)
# Add a new key
inventory["oranges"] = 8
print("After adding oranges:", inventory)
# Update an existing key
inventory["apples"] = 10
print("After updating apples:", inventory)
# del — removes a key entirely
del inventory["bananas"]
print("After deleting bananas:", inventory)
# .pop() — removes a key and gives you the value back
orange_count = inventory.pop("oranges")
print(f"Popped oranges ({orange_count}):", inventory)
# .pop() with a default — no crash if key is missing
missing = inventory.pop("grapes", 0)
print(f"Popped grapes (default {missing}):", inventory)
# .popitem() — removes and returns the last inserted pair
inventory["pears"] = 4
inventory["kiwis"] = 6
print("Before popitem():", inventory)
last_item = inventory.popitem()
print(f"popitem() returned: {last_item}")
print("After popitem():", inventory)
# -----------------------------------------------------------------------------
# 4. Checking membership with `in`
# -----------------------------------------------------------------------------
print("\n--- Membership checking ---")
colors = {"red": "#FF0000", "green": "#00FF00", "blue": "#0000FF"}
# `in` checks KEYS, not values
print("'red' in colors:", "red" in colors) # True
print("'yellow' in colors:", "yellow" in colors) # False
print("'#FF0000' in colors:", "#FF0000" in colors) # False — that's a value!
# To check values, use .values()
print("'#FF0000' in colors.values():", "#FF0000" in colors.values()) # True
# -----------------------------------------------------------------------------
# 5. Iterating: .keys(), .values(), .items()
# -----------------------------------------------------------------------------
print("\n--- Iterating ---")
scores = {"Alice": 92, "Bob": 85, "Carol": 97}
# Iterating over keys (default behavior)
print("Keys:")
for name in scores:
print(f" {name}")
# Iterating over values
print("Values:")
for score in scores.values():
print(f" {score}")
# Iterating over key-value pairs — the most useful one!
print("Items (key-value pairs):")
for name, score in scores.items():
print(f" {name} scored {score}")
# You can also get these as lists
print("Keys as list:", list(scores.keys()))
print("Values as list:", list(scores.values()))
# -----------------------------------------------------------------------------
# 6. Useful methods: .update() and .setdefault()
# -----------------------------------------------------------------------------
print("\n--- .update() ---")
defaults = {"color": "blue", "size": "medium", "theme": "light"}
user_prefs = {"color": "red", "font": "mono"}
print("Defaults:", defaults)
print("User prefs:", user_prefs)
defaults.update(user_prefs)
print("After update:", defaults)
# "color" was overwritten, "font" was added, others stayed the same
print("\n--- .setdefault() ---")
config = {"debug": True}
print("Starting config:", config)
# .setdefault() only sets the value if the key doesn't already exist
config.setdefault("debug", False) # Does nothing — "debug" is already True
config.setdefault("verbose", False) # Adds "verbose" since it's not there yet
print("After setdefault():", config)
# It also returns the value (existing or newly set)
level = config.setdefault("log_level", "INFO")
print(f"setdefault returned: '{level}'")
print("Final config:", config)
# -----------------------------------------------------------------------------
# 7. Dictionary unpacking with **
# -----------------------------------------------------------------------------
print("\n--- Unpacking with ** ---")
# Merging two dicts into a new one (originals unchanged)
base = {"color": "blue", "size": "medium"}
extras = {"color": "red", "font": "mono"}
merged = {**base, **extras}
print("Base:", base)
print("Extras:", extras)
print("Merged:", merged) # "color" comes from extras (last one wins)
# The | operator does the same thing (Python 3.9+)
merged2 = base | extras
print("Merged with |:", merged2)
# Unpacking into function arguments
def make_greeting(name, style="formal"):
if style == "formal":
return f"Good day, {name}."
return f"Hey, {name}!"
params = {"name": "Alice", "style": "casual"}
print(make_greeting(**params))
# -----------------------------------------------------------------------------
# 8. Nested dictionaries
# -----------------------------------------------------------------------------
print("\n--- Nested dictionaries ---")
gradebook = {
"Alice": {"math": 92, "english": 88, "science": 95},
"Bob": {"math": 78, "english": 85, "science": 80},
"Carol": {"math": 90, "english": 91, "science": 87},
}
# Access nested values by chaining keys
print(f"Alice's math score: {gradebook['Alice']['math']}")
# Iterate over the nested structure
for student, grades in gradebook.items():
average = sum(grades.values()) / len(grades)
print(f" {student}: average = {average:.1f}")
# Update a nested value
gradebook["Bob"]["math"] = 82
print(f"Bob's updated math score: {gradebook['Bob']['math']}")
# Add a new student
gradebook["Dave"] = {"math": 88, "english": 76, "science": 93}
print(f"New student Dave: {gradebook['Dave']}")
# -----------------------------------------------------------------------------
# 9. Dictionary ordering (insertion order guaranteed since Python 3.7)
# -----------------------------------------------------------------------------
print("\n--- Insertion order ---")
ordered = {}
ordered["first"] = 1
ordered["second"] = 2
ordered["third"] = 3
ordered["fourth"] = 4
# Items always come out in the order they were inserted
print("Keys in order:", list(ordered.keys()))
# Output: ['first', 'second', 'third', 'fourth']
# Updating a value does NOT change its position
ordered["second"] = 99
print("After updating 'second':", list(ordered.items()))
# 'second' stays in its original position
# -----------------------------------------------------------------------------
# 10. What can be a key? (hashable types only)
# -----------------------------------------------------------------------------
print("\n--- Valid key types ---")
mixed_keys = {
"name": "a string key",
42: "an integer key",
3.14: "a float key",
True: "a boolean key",
(1, 2): "a tuple key",
}
for key, value in mixed_keys.items():
print(f" {key!r:>12} ({type(key).__name__:>5}) -> {value}")
# These would fail — uncomment to see the errors:
# bad = {[1, 2]: "nope"} # TypeError: unhashable type: 'list'
# bad = {{"a": 1}: "nope"} # TypeError: unhashable type: 'dict'
# -----------------------------------------------------------------------------
# 11. Common patterns
# -----------------------------------------------------------------------------
# --- Counting occurrences ---
print("\n--- Pattern: counting ---")
sentence = "the cat sat on the mat and the cat saw the dog"
words = sentence.split()
word_counts = {}
for word in words:
word_counts[word] = word_counts.get(word, 0) + 1
print(f"Words: {sentence}")
print("Counts:", word_counts)
# --- Grouping items ---
print("\n--- Pattern: grouping ---")
animals = ["ant", "bear", "cat", "antelope", "cobra", "beaver", "crow"]
by_letter = {}
for animal in animals:
first = animal[0]
by_letter.setdefault(first, []).append(animal)
print("Grouped by first letter:")
for letter, group in sorted(by_letter.items()):
print(f" {letter}: {group}")
# --- Inverting a dictionary ---
print("\n--- Pattern: inverting ---")
country_codes = {"US": 1, "UK": 44, "DE": 49, "JP": 81}
code_to_country = {code: country for country, code in country_codes.items()}
print("Original:", country_codes)
print("Inverted:", code_to_country)
# -----------------------------------------------------------------------------
# 12. Putting it all together
# -----------------------------------------------------------------------------
print()
print("=" * 50)
print(" DICTIONARIES EXAMPLE COMPLETE!")
print("=" * 50)
print()
print("Try modifying this file and run it again to experiment!")