summaryrefslogtreecommitdiff
path: root/dictionaries/script.rst
blob: e272e07535cd27f01a3347258bc91c9450c48559 (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
.. Objectives
.. ----------

.. At the end of this tutorial, you will be able to 

.. 1. Create dictionaries
.. #. Add and delete data from dictionaries
.. #. Retrieve data from dictionaries
.. #. Check for container-ship of keys
.. #. Iterate over elements

.. Prerequisites
.. -------------

..   1. should have ``ipython``  installed. 
..   #. getting started with ``ipython``.
..   #. basic datatypes.
     
.. Author              : Anoop Jacob Thomas <anoop@fossee.in>
   Internal Reviewer   : Puneeth
   External Reviewer   :
   Language Reviewer   : Bhanukiran
   Checklist OK?       : <11-11-2010, Anand, OK> [2010-10-05]


============
Dictionaries
============

.. L1

{{{ Show the  first slide containing title, name of the production
team along with the logo of MHRD }}}

.. R1

Hello friends and Welcome to the spoken tutorial on 'dictionaries'.

.. L2

{{{ switch to slide containing objectives }}}

.. R2

At the end of this tutorial, you will be able to,

 1. Create dictionaries.
 #. Add and delete data from dictionaries.
 #. Retrieve data from dictionaries.
 #. Check for container-ship of keys.
 #. Iterate over elements.

.. L3

{{{ Switch to the pre-requisite slide }}}

.. R3

Before beginning this tutorial,we would suggest you to complete the 
tutorial on "Basic datatypes and operators".

.. L4

{{{ switch to next slide on overview of dictionaries }}}

.. R4

A dictionary in general, is designed to look up for meanings of
words. Similarly, Python dictionary is also designed to look up for a
specific key and retrieve the corresponding value. Dictionaries are
data structures that provide key-value mappings.  Dictionaries are
similar to lists except that instead of the values having integer
indexes, dictionaries have keys or strings as indexes.

.. R5

We start our ipython interpreter as,

.. L5

{{{ Open the terminal }}}
::

    ipython

.. R6

Let us start by creating an empty dictionary. Type the following in
your IPython interpreter.

.. L6
::

    mt_dict = {}    

.. R7

Notice that unlike lists, curly braces are used to define a ``dictionary``.

.. L7

{{{ move the mouse over curly braces }}}

.. R8

Now let us see how to create a non-empty dictionary,

.. L8
::

    extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script', 
    'html' : 'Html document', 'pdf' : 'Portable Document Format'}

.. R9

Notice that each key-value pair is separated by a comma.

.. L9

{{{ move the mouse over the commas }}}

.. R10

and each key and value are separated using a colon.

.. L10

{{{ move the mouse over the colon one by one  }}}

.. R11

Here, we have defined four entries in the dictionary extensions. 
The keys are

jpg, py, html, and pdf.

Simply type,extensions in the interpreter to see the content 
of the dictionary.

.. L11
::

    extensions

.. R12

Notice that, in dictionaries, the order cannot be predicted and you can 
see that the values are not in the order that we entered in.

.. L12

{{{ switch to next slide, accessing elements }}}

.. R13

Like in lists, the elements in a dictionary can be accessed using the
index, here the index is the key. We type,

.. L13

{{{ Switch to terminal }}}
::

    print extensions['jpg']

.. R14

It printed JPEG Image. And now try,

.. L14
::

    print extensions['zip']

.. R15

Well it gave us an error, saying that the key 'zip' is not in the
dictionary.

Pause here for some time and try few more keys. Also try jpg in
capital letters.
<continue from paused state>
Well that was about creating dictionaries, now how do we add or delete
items. We can add new items into dictionaries as,

.. L15
::

    extensions['cpp'] = 'C++ code'

.. R16

and delete items using the ``del`` keyword as,

.. L16
::

    del extensions['pdf']

.. R17

Let us check the content of the dictionary now,

.. L17
::

    extensions

.. R18

So the changes have been made. Now let us try one more thing,

.. L18
::

    extensions['cpp'] = 'C++ source code'
    extensions

.. R19

As you can see, it neither added a new thing nor gave an error, but it
simply replaced the existing value with the new one.

Now let us learn how to check if a particular key is present in the
dictionary. For that we can use the method ``in``,

.. L19
::

    'py' in extensions
    'odt' in extensions

.. R20

It will return ``True`` if the key is found in the dictionary, and
will return ``False`` if key is not present. Note that we can check
only for container-ship of keys in dictionaries and not values.

.. L20

{{{ switch to next slide, Retrieve keys and values }}}

.. R21

Now let us see how to retrieve the keys and values. We can use the
method ``keys()`` for getting a list of the keys in a particular
dictionary and the method ``values()`` for getting a list of
values. 

.. R22

Let us try them,

.. L22
 
{{{ Switch to terminal }}}
::

    extensions.keys()

.. R23

It returned the ``list`` of keys in the dictionary extensions. And now
the other one,

.. L23
::

    extensions.values()

.. R24

It returned the ``list`` of values in the dictionary.

Pause the video here, try out the following exercise and resume the video.

.. L24

.. L25

{{{ Show slide with exercise 1 }}}

.. R25

 Print the keys and values in the dictionary one by one.

.. R26

Switch to terminal for solution.

.. L26

{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::

    for each in extensions.keys():
        print each, "-->", extensions[each]


.. L27

{{{ Show summary slide }}}

.. R27

This brings us to the end of this tutorial.In this tutorial, 
we have learnt to,

 1. Create dictionaries namely --
    - empty dictionaries
    - dictionaries with data.
 #. Access elements in the dictionaries using the keys.
 #. Add elements to a dictionary by assigning a value to a key.
 #. Delete elements from a dictionary by using the function ``del``.
 #. Retrieve the keys and values by using the methods ``.keys()`` and 
    ``.values()`` respectively.
 #. Iterate over elements of a dictionary using a ``for`` loop.
 
.. L28

{{{Show self assessment questions slide}}}

.. R28

Here are some self assessment questions for you to solve


1. Container-ship of values can be checked in a python dictionary

   - True
   - False

2. Consider the python dictionary ``x = {'a' : ['a','b','c'], 'b' :
   (1, 2, 3), 1 : {1 : 'one', 2 : 'two'}, 10 : {10 : 'ten', 11 :
   'eleven'}}``. What will the following code return? 
     ``(1, 2, 3) in x.values()``.

   - True
   - False
   - Container-ship of values cannot be checked in dictionaries
   - The dictionary is invalid

.. L29

{{{solution of self assessment questions on slide}}}

.. R29

And the answers,

1. False.Container-ship of only keys can be checked in a python 
   dictionary.

2. True 

.. L30

{{{ switch to thank you slide }}}

.. R30

Hope you have enjoyed this tutorial and found it useful.
Thank you!