Skip to content

Commit 98e4423

Browse files
committed
Merge pull request DjangoGirls#107 from bmispelon/dict-improvement
Fixed DjangoGirls#82 -- Improved the chapter on dictionaries.
2 parents 5bb3ffc + 0a4ccfe commit 98e4423

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

python_introduction/README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,57 +232,70 @@ You can find a list of all available list methods in this chapter of the Python
232232

233233
## Dictionaries
234234

235-
A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. The syntax to define a dictionary is:
235+
A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. The syntax to define an empty dictionary is:
236236

237237
>>> {}
238238
{}
239239

240240
This shows that you just created an empty dictionary. Hurray!
241241

242-
Now, try writing the following command:
242+
Now, try writing the following command (try replacing your own information too):
243243

244-
>>> django_dolls = {'Dottie' : 15, 'Lottie' : 305, 'EmmyLou' : 17}
244+
>>> participant = {'name' : 'Ola', 'country' : 'Poland', 'favorite_numbers' : [7, 42, 92]}
245245

246-
Don't be surprised with the weird names. Go to the link: http://hubpages.com/hub/50-Doll-Names to look for more cute doll names. :P Just Kidding (You should do this if and only if you have a lot of time).
246+
With this command, you just created a variable named `participant` with three key-value pairs:
247247

248-
Above, you just created a variable named `django_dolls` with three key-value pairs. The key Dottie points to the value 15, Lottie points to the value 305, EmmyLou points to the value 17. Do you want to check? Type:
248+
- The key `name` points to the value `'Ola'` (a `string` object),
249+
- `country` points to `'Poland'` (another `string`),
250+
- and `favorite_numbers` points to `[7, 42, 92]` (a `list` with three numbers in it).
249251

250-
>>> print(django_dolls['Dottie'])
251-
15
252+
You can check the content of individual keys with this syntax:
253+
254+
>>> print(participant['name'])
255+
Ola
252256

253257
See, it's similar to a list. But you don't need to remember the index - just the name.
254258

259+
What happens if we ask Python the value of a key that doesn't exist? Can you guess? Let's try it and see!
260+
261+
>>> participant['age']
262+
Traceback (most recent call last):
263+
File "<stdin>", line 1, in <module>
264+
KeyError: 'age'
265+
266+
Look, another error! This one is a **KeyError**. Python is helpful and tells you that the key `'age'` doesn't exist in this dictionary.
267+
255268
When to use a dictionary or a list? Well, a good point to ponder on. Just have a solution in mind before looking at the answer in the next line.
256269

257270
- Do you just need an ordered sequence of items? Go for a list.
258271
- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a dictionary.
259272

260273
Dictionaries are mutable like "lists" meaning that they can be changed after they are created. You can add new key/value pairs to the dictionary after it is created, like:
261274

262-
>>> django_dolls['Jilly'] = 67
275+
>>> participant['favorite_language'] = 'Python'
263276

264277
Like the lists, using `len()` method on the dictionaries, returns the number of key-value pairs in the dictionary. Go ahead and type in the command:
265278

266-
>>> len(django_dolls)
279+
>>> len(participant)
267280
4
268281

269-
I hope it makes sense uptil now. :) Ready for some more fun with Dictionaries? Hop on the next line for some amazing things.
282+
I hope it makes sense uptil now. :) Ready for some more fun with dictionaries? Hop on the next line for some amazing things.
270283

271-
You can use `del` command to delete an item in the dictionary which has particular. Say, if you want to delete the entry corresponding to the key 'Dottie', just type in the following command:
284+
You can use the `del` command to delete an item in the dictionary. Say, if you want to delete the entry corresponding to the key `'favorite_numbers'`, just type in the following command:
272285

273-
>>> del django_dolls['Dottie']
274-
>>> django_dolls
275-
{'Jilly': 67, 'EmmyLou': 17, 'Lottie': 305}
286+
>>> del participant['favorite_numbers']
287+
>>> participant
288+
{'country': 'Poland', 'favorite_language': 'Python', 'name': 'Ola'}
276289

277-
As you can see from the output, the key-value pair corresponding to 'Dottie' key has been deleted.
290+
As you can see from the output, the key-value pair corresponding to 'favorite_numbers' key has been deleted.
278291

279292
Apart from this, you can also change a value associated with an already created key in the dictionary. Type:
280293

281-
>>> django_dolls['Jilly'] = 100
282-
>>> django_dolls
283-
{'Jilly': 100, 'EmmyLou': 17, 'Lottie': 305}
294+
>>> participant['country'] = 'Germany'
295+
>>> participant
296+
{'country': 'Germany', 'favorite_language': 'Python', 'name': 'Ola'}
284297

285-
As you can see, the value of the key 'Jilly' has been altered from *67* to *100*. :) Exciting? Hurrah! You just learnt another amazing thing.
298+
As you can see, the value of the key `'country'` has been altered from `'Poland'` to `'Germany'`. :) Exciting? Hurrah! You just learnt another amazing thing.
286299

287300
### Summary
288301

0 commit comments

Comments
 (0)