Python/C3/Dictionaries/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

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

Learning objectives

At the end of this tutorial, you will be able to,
  1. Create dictionaries.
  2. Add and delete data from dictionaries.
  3. Retrieve data from dictionaries.
  4. Check for container-ship of keys.
  5. Iterate over elements.


Show Slide 3

Pre-requisite slide

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

Overview of dictionaries

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.
Open the terminal
ipython
We start our ipython interpreter as,
mt_dict = {} Let us start by creating an empty dictionary. Type the following in your IPython interpreter.
move the mouse over curly braces Notice that unlike lists, curly braces are used to define a dictionary.
extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script',
'html' : 'Html document', 'pdf' : 'Portable Document Format'}
Now let us see how to create a non-empty dictionary,
Move the mouse over the commas Notice that each key-value pair is separated by a comma.
Move the mouse over the colon one by one and each key and value are separated using a colon.
extensions 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.

Show Slide 5

Accessing elements

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.
Switch to terminal
print extensions['jpg']
Like in lists, the elements in a dictionary can be accessed using the index, here the index is the key. We type,
print extensions['zip'] It printed JPEG Image. And now try,
extensions['cpp'] = 'C++ code' 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,

del extensions['pdf'] and delete items using the del keyword as,
extensions Let us check the content of the dictionary now,
extensions['cpp'] = 'C++ source code'
extensions
So the changes have been made. Now let us try one more thing,
'py' in extensions
'odt' in extensions
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,

Show Slide 6

Retrieve keys and values

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.
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.
Switch to terminal
extensions.keys()
Let us try them,
extensions.values() It returned the list of keys in the dictionary extensions. And now the other one,
It returned the list of values in the dictionary.

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

Show Slide 7

Assignment 1

Print the keys and values in the dictionary one by one.
Continue from paused state Switch to the terminal
for each in extensions.keys():
    print each, "-->", extensions[each]
Switch to terminal for solution.
Show Slide 8

Summary slide

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.
  2. Access elements in the dictionaries using the keys.
  3. Add elements to a dictionary by assigning a value to a key.
  4. Delete elements from a dictionary by using the function del.
  5. Retrieve the keys and values by using the methods .keys() and .values() respectively.
  6. Iterate over elements of a dictionary using a for loop.


Show Slide 9

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Container-ship of values can be checked in a python dictionary
    • True
    • False
  1. 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?
    Unexpected indentation.
    (1, 2, 3) in x.values().
    • True
    • False
    • Container-ship of values cannot be checked in dictionaries
    • The dictionary is invalid


Show Slide 10

Solution of self assessment questions on slide

And the answers,
  1. False. Container-ship of only keys can be checked in a python dictionary.
  2. True


Show Slide 11

Acknowledgment slide

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

Contributors and Content Editors

Pravin1389