You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python_introduction/README.md
+32-19Lines changed: 32 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -220,57 +220,70 @@ You can find a list of all available list methods in this chapter of the Python
220
220
221
221
## Dictionaries
222
222
223
-
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:
223
+
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:
224
224
225
225
>>> {}
226
226
{}
227
227
228
228
This shows that you just created an empty dictionary. Hurray!
229
229
230
-
Now, try writing the following command:
230
+
Now, try writing the following command (try replacing your own information too):
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).
234
+
With this command, you just created a variable named `participant` with three key-value pairs:
235
235
236
-
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:
236
+
- The key `name` points to the value `'Ola'` (a `string` object),
237
+
-`country` points to `'Poland'` (another `string`),
238
+
- and `favorite_numbers` points to `[7, 42, 92]` (a `list` with three numbers in it).
237
239
238
-
>>> print(django_dolls['Dottie'])
239
-
15
240
+
You can check the content of individual keys with this syntax:
241
+
242
+
>>> print(participant['name'])
243
+
Ola
240
244
241
245
See, it's similar to a list. But you don't need to remember the index - just the name.
242
246
247
+
What happens if we ask Python the value of a key that doesn't exist? Can you guess? Let's try it and see!
248
+
249
+
>>> participant['age']
250
+
Traceback (most recent call last):
251
+
File "<stdin>", line 1, in <module>
252
+
KeyError: 'age'
253
+
254
+
Look, another error! This one is a **KeyError**. Python is helpful and tells you that the key `'age'` doesn't exist in this dictionary.
255
+
243
256
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.
244
257
245
258
- Do you just need an ordered sequence of items? Go for a list.
246
259
- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a dictionary.
247
260
248
261
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:
249
262
250
-
>>> django_dolls['Jilly'] = 67
263
+
>>> participant['favorite_language'] = 'Python'
251
264
252
265
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:
253
266
254
-
>>> len(django_dolls)
267
+
>>> len(participant)
255
268
4
256
269
257
-
I hope it makes sense uptil now. :) Ready for some more fun with Dictionaries? Hop on the next line for some amazing things.
270
+
I hope it makes sense uptil now. :) Ready for some more fun with dictionaries? Hop on the next line for some amazing things.
258
271
259
-
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:
272
+
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:
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.
286
+
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.
0 commit comments