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
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -204,12 +204,14 @@ You can find a list of all available list methods in this chapter of the Python
204
204
## Dictionaries
205
205
206
206
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:
207
+
207
208
>>> {}
208
209
{}
209
210
210
211
This shows that you just created an empty dictionary. Hurray!
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).
@@ -222,9 +224,11 @@ When to use a dictionary or a list? Well, a good point to ponder on. Just have a
222
224
- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a dictionary.
223
225
224
226
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:
227
+
225
228
>>> django_dolls['Jilly'] = 67
226
229
227
230
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:
231
+
228
232
>>> len(django_dolls)
229
233
4
230
234
@@ -239,6 +243,7 @@ You can use del() command to delete an item in the dictionary which has particul
239
243
As you can see from the output, the key-value pair corresponding to 'Dottie' key has been deleted.
240
244
241
245
Apart from this, you can also change a value associated with an already created key in the dictionary. Type:
0 commit comments