Difference between revisions of "Python/C3/Dictionaries/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 63: Line 63:
 
'html'  colon  'Html document' comma pdf  colon  Portable Document Format close curly brackets
 
'html'  colon  'Html document' comma pdf  colon  Portable Document Format close curly brackets
 
   
 
   
 
 
|-
 
|-
 
| 02:08
 
| 02:08

Revision as of 15:35, 10 July 2014

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

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

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha