Python/C3/Dictionaries/English-timed

From Script | Spoken-Tutorial
Revision as of 15:10, 28 March 2013 by Sneha (Talk | contribs)

Jump to: navigation, search
Timing Narration
0:01 Hello friends and Welcome to the spoken tutorial on 'dictionaries'.
0:05 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. Iterating over elements.
0:20 Before beginning this tutorial,we would suggest you to complete the tutorial on "Basic datatypes and operators".
0:27 A dictionary in general, is designed to look up for meanings of words.
0:35 Similarly, Python dictionary is also designed to look up for a specific key and retrieve the corresponding value.
0:43 Dictionaries are data structures that provide key-value mappings.
0:47 Dictionaries are similar to lists except that instead of the values having integer indexes, dictionaries have keys or strings as indexes.
0:58 We start our ipython interpreter as, ipython
1:07 Let us start by creating an empty dictionary.
1:12 Type the following in your IPython interpreter,mt underscore dict = closing curly brackets
1:25 Notice that unlike lists, curly braces are used to define a dictionary
1:33 Now let us see how to create a non-empty dictionary,
1:37 so type extensions = open curly brackets in single quotes jpg colon 'JPEG Image' comma 'py' colon 'Python script' comma

'html' colon 'Html document' comma pdf colon Portable Document Format close curly brackets


2:08 Notice that each key-value pair is separated by a comma.
2:17 and each key value are separated using a colon.
2:23 Here, we have defined four entries in the dictionary extensions.
2:29 The keys are jpg comma py comma html comma and pdf.
2:36 Simply type,extensions in the interpreter to see the content of the dictionary.
2:40 So type extensions.
2:45 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.
2:53 Like lists, the elements in a dictionary can be accessed using the index, here the index is the key.
3:06 We type, print extensions in closing brackets in single quotes jpg.
3:25 NOW It printed JPEG Image. And now type print extensions within square brackets zip.
3:42 Well it gave us an error, saying that the key 'zip' is not in the dictionary.
3:48 So Pause here for some time and try few more keys.
3:52 Also try jpg in capital letters.
3:58 <continue from paused state Well that was about creating dictionaries, now how do we add or delete items?
4:09 We can add new items into dictionaries as,
4:13 Type extensions within square brackets in single quotes cpp is equal to in single quotes C++ then code.
4:36 and delete items using the del keyword as, type del space extensions and in square brackets and single quotes pdf.
4:51 Let us check the content of the dictionary now,
4:54 So type extensions.
4:57 So the changes have been made.
4:59 let us try one more thing, type extensions within square brackets and in single quotes cpp is equal to in single quote C++ and source code. then type again extensions.
5:21 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.
5:29 Now let us learn how to check if a particular key is present in the dictionary.
5:33 For that we can use the method in,
5:37 So type within single quotes py in extensions.
5:45 Then type odt in extensions.
5:53 It will return True if the key is found in the dictionary, and will return False if key is not present.
6:00 Note that we can check only for container-ship of keys in dictionaries and not values.
6:07 Now let us see how to retrieve the keys and values.
6:10 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.
6:20 Let us try them, type extensions dot key closing brackets.
6:31 It returned the list of keys in the dictionary extensions.
6:35 And now the other one is extensions dot values and closing brackets.
6:42 It returned the list of values in the dictionary.
6:45 Pause the video here, try out the following exercise and resume the video.
6:49 Print the keys and values in the dictionary one by one.
6:54 Now Switch to terminal for solution.
6:58 Type for each in extensions dot keys closing brackets colon then,
   print each comma within double quotes hyphen hyphen greater symbol, extensions and  in square   brackets each.
7:35 This brings us to the end of this tutorial.
7:39 In this tutorial, we have learn't to,
7:41 1. Create dictionaries namely -- - empty dictionaries - dictionaries with data.
7:45 2. Access elements in the dictionaries using the keys.
7:49 3. Add elements to a dictionary by assigning a key value.
7:55 4. Delete elements from a dictionary by using the function del
8:00 5. Retrieve the keys and values by using the methods .keys() and.values() respectively.
8:06 6. Iterate over elements of a dictionary using a for loop.
8:11 Here are some self assessment questions for you to solve
8:15 1. First one, Container-ship of values can be checked in a python dictionary - true or false.
8:22 2. Second one is, Consider the python dictionary x is equal to within curly brackets 'a' colon ['a','b','c'], 'b' colon (1, 2, 3), 1 colon then again another curly braces 1 colon 'one' comma 2 colon 'two' comma 10 colon within curly brackets 10 colon 'ten', 11 colon 'eleven' and close the curly brackets.
8:52 What will the following code return?
8:57 (1, 2, 3) in x.values(). True. False
9:05 Then container-ship of values cannot be checked in dictionaries
9:10 The dictionary is invalid
9:13 So Lets look at the answers,
9:16 1.The answer of first one, False.
9:19 Container-ship of only keys can be checked in a python dictionary.
9:27 2. Second one is, True
9:31 Hope you have enjoyed this tutorial and found it useful.
9:35 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha