@@ -29,34 +29,39 @@ avoid using it as a variable name.
2929\index{dict function}
3030\index{function!dict}
3131
32- >>> eng2sp = dict()
33- >>> print(eng2sp)
34- {}
32+ ~~~~ {.python .trinket}
33+ >>> eng2sp = dict()
34+ >>> print(eng2sp)
35+ {}
36+ ~~~~
3537
3638The curly brackets, ` {} ` , represent an empty dictionary. To add items to
3739the dictionary, you can use square brackets:
3840
3941\index{squiggly bracket}
4042\index{bracket!squiggly}
4143
42- >>> eng2sp['one'] = 'uno'
44+ ~~~~ {.python}
45+ >>> eng2sp['one'] = 'uno'
46+ ~~~~
4347
4448This line creates an item that maps from the key ` 'one' ` to
4549the value "uno". If we print the dictionary again, we see a key-value
4650pair with a colon between the key and value:
4751
48- >>> print(eng2sp)
49- {'one': 'uno'}
52+ ~~~~ {.python}
53+ >>> print(eng2sp)
54+ {'one': 'uno'}
55+ ~~~~
5056
5157This output format is also an input format. For example, you can create
52- a new dictionary with three items:
53-
54- >>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
55-
56- But if you print ` eng2sp ` , you might be surprised:
58+ a new dictionary with three items. But if you print ` eng2sp ` , you might be surprised:
5759
58- >>> print(eng2sp)
59- {'one': 'uno', 'three': 'tres', 'two': 'dos'}
60+ ~~~~ {.python}
61+ >>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}
62+ >>> print(eng2sp)
63+ {'one': 'uno', 'three': 'tres', 'two': 'dos'}
64+ ~~~~
6065
6166The order of the key-value pairs is not the same. In fact, if you type
6267the same example on your computer, you might get a different result. In
@@ -66,8 +71,10 @@ But that's not a problem because the elements of a dictionary are never
6671indexed with integer indices. Instead, you use the keys to look up the
6772corresponding values:
6873
69- >>> print(eng2sp['two'])
70- 'dos'
74+ ~~~~ {.python}
75+ >>> print(eng2sp['two'])
76+ 'dos'
77+ ~~~~
7178
7279The key ` 'two' ` always maps to the value "dos" so the order
7380of the items doesn't matter.
@@ -77,17 +84,21 @@ If the key isn't in the dictionary, you get an exception:
7784\index{exception!KeyError}
7885\index{KeyError}
7986
80- >>> print(eng2sp['four'])
81- KeyError: 'four'
87+ ~~~~ {.python}
88+ >>> print(eng2sp['four'])
89+ KeyError: 'four'
90+ ~~~~
8291
8392The ` len ` function works on dictionaries; it returns the
8493number of key-value pairs:
8594
8695\index{len function}
8796\index{function!len}
8897
89- >>> len(eng2sp)
90- 3
98+ ~~~~ {.python}
99+ >>> len(eng2sp)
100+ 3
101+ ~~~~
91102
92103The ` in ` operator works on dictionaries; it tells you whether
93104something appears as a * key* in the dictionary (appearing as a value is
@@ -97,10 +108,12 @@ not good enough).
97108\index{in operator}
98109\index{operator!in}
99110
100- >>> 'one' in eng2sp
101- True
102- >>> 'uno' in eng2sp
103- False
111+ ~~~~ {.python}
112+ >>> 'one' in eng2sp
113+ True
114+ >>> 'uno' in eng2sp
115+ False
116+ ~~~~
104117
105118To see whether something appears as a value in a dictionary, you can use
106119the method ` values ` , which returns the values as a list, and
@@ -109,9 +122,11 @@ then use the `in` operator:
109122\index{values method}
110123\index{method!values}
111124
112- >>> vals = list(eng2sp.values())
113- >>> 'uno' in vals
114- True
125+ ~~~~ {.python}
126+ >>> vals = list(eng2sp.values())
127+ >>> 'uno' in vals
128+ True
129+ ~~~~
115130
116131The ` in ` operator uses different algorithms for lists and
117132dictionaries. For lists, it uses a linear search algorithm. As the list
@@ -171,14 +186,16 @@ for the letters that do appear.
171186
172187Here is what the code might look like:
173188
174- word = 'brontosaurus'
175- d = dict()
176- for c in word:
177- if c not in d:
178- d[c] = 1
179- else:
180- d[c] = d[c] + 1
181- print(d)
189+ ~~~~ {.python .trinket}
190+ word = 'brontosaurus'
191+ d = dict()
192+ for c in word:
193+ if c not in d:
194+ d[c] = 1
195+ else:
196+ d[c] = d[c] + 1
197+ print(d)
198+ ~~~~
182199
183200We are effectively computing a * histogram* , which is a
184201statistical term for a set of counters (or frequencies).
@@ -197,7 +214,9 @@ dictionary we increment `d[c]`.
197214
198215Here's the output of the program:
199216
200- {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}
217+ ~~~~ {.python}
218+ {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}
219+ ~~~~
201220
202221The histogram indicates that the letters ` 'a' ` and "b"
203222appear once; "o" appears twice, and so on.
@@ -210,22 +229,26 @@ a default value. If the key appears in the dictionary, `get`
210229returns the corresponding value; otherwise it returns the default value.
211230For example:
212231
213- >>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
214- >>> print(counts.get('jan', 0))
215- 100
216- >>> print(counts.get('tim', 0))
217- 0
232+ ~~~~ {.python .trinket}
233+ >>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
234+ >>> print(counts.get('jan', 0))
235+ 100
236+ >>> print(counts.get('tim', 0))
237+ 0
238+ ~~~~
218239
219240We can use ` get ` to write our histogram loop more concisely.
220241Because the ` get ` method automatically handles the case where
221242a key is not in a dictionary, we can reduce four lines down to one and
222243eliminate the ` if ` statement.
223244
224- word = 'brontosaurus'
225- d = dict()
226- for c in word:
227- d[c] = d.get(c,0) + 1
228- print(d)
245+ ~~~~ {.python}
246+ word = 'brontosaurus'
247+ d = dict()
248+ for c in word:
249+ d[c] = d.get(c,0) + 1
250+ print(d)
251+ ~~~~
229252
230253The use of the ` get ` method to simplify this counting loop
231254ends up being a very commonly used "idiom" in Python and we will use it
@@ -424,7 +447,7 @@ We will not specify the `table` but we will use the
424447will even let Python tell us the list of characters that it considers
425448"punctuation":
426449
427- ~~~~ {.python trinket }
450+ ~~~~ {.python}
428451>>> import string
429452>>> string.punctuation
430453'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
0 commit comments