Python-3.4.3/C3/Dictionaries/English-timed
Time | Narration |
00:01 | Welcome to the spoken tutorial on Dictionaries. |
00:05 | In this tutorial, we will learn to,
Create dictionaries Add and delete data from dictionaries Retrieve data from dictionaries Check for presence of keys and Iterate over elements. |
00:25 | To record this tutorial, I am using
Ubuntu Linux 16.04 operating system Python 3.4.3 and IPython 5.1.0 |
00:40 | To practise this tutorial, you should know how to
use basic data types and operators If not, see the relevant Python tutorials on this website. |
00:54 | First we will learn about dictionaries. |
00:58 | Dictionary is an unordered collection of items which has key:value pairs.
Dictionary is used to look up for a specific key and retrieve the corresponding value. |
01:12 | Keys are unique within a dictionary while values may not be. |
01:18 | The values of a dictionary can be of any data type. |
01:23 | But the keys must be of immutable data type such as strings, numbers or tuples. |
01:31 | Let us start ipython. Open the terminal. |
01:37 | Type ipython3 and press Enter. From here onwards, remember to press the Enter key after typing every command on the terminal. |
01:50 | Let us start by creating an empty dictionary. |
01:54 | Type, empty is equal to open and close curly braces Note that unlike lists, curly braces are used to define a dictionary. |
02:06 | Now let us see how to create a non empty dictionary. |
02:11 | Type as shown. Note that,
Each key:value pair is separated by a comma, Each key is separated from its value by a colon, and The whole thing is enclosed in curly braces. |
02:29 | Here, we have defined four entries in the dictionary student. The keys are name, age, gender, class. |
02:44 | Type, student to see its content. |
02:50 | Next let us learn to access the dictionary elements. |
02:55 | The value of a dictionary can be accessed using the corresponding key. |
03:01 | The syntax is: dictionary underscore name inside square brackets key |
03:07 | Now let us access the value for the key ‘name’. |
03:12 | Type, print inside brackets student inside square brackets inside single quotes name |
03:21 | As expected it printed the output as raj. |
03:26 | Now we will retrieve the value for the key ‘class’ |
03:31 | Type, print inside brackets student inside square brackets inside single quotes class
It displays the output as 10. |
03:43 | If we try to access a value with a wrong key, we get an error. |
03:49 | Type, student inside square brackets inside single quotes height |
03:56 | The interpreter gave us the KeyError: 'height' This is because, key 'height' is not present in the student dictionary. |
04:05 | This is about creating dictionaries. |
04:09 | Next, let us see how to add or delete items in a dictionary. |
04:15 | First let us add an element height to the dictionary student.
Type, student inside square brackets inside single quotes height is equal to 6.2 |
04:29 | Let us now check the content of the dictionary.
Type, student You can see that key-value 'height': 6.2 is added to the dictionary student. |
04:42 | Next let us modify the element class of the dictionary student.
Type, student inside square brackets inside single quotes class is equal to 11 |
04:56 | Now, to check the content of the dictionary, type, student |
05:02 | As you can see, the value for class key is changed to 11 . |
05:06 | Next let us learn to delete age from the dictionary student.
Type, del student inside square brackets inside single quotes age |
05:20 | Type, student You can see that now key-value 'age': 16 is deleted from student. |
05:29 | Next let us see how to check whether a key is present in a dictionary.
For that we can use the method in. |
05:39 | The method in will return True if the key is found in the dictionary. It will return False if key is not present. |
05:48 | Note that we can check only for the presence of the keys in dictionaries and not values. |
05:55 | Keys of a dictionary are not ordered. Hence, slicing and striding are not valid on dictionaries. |
06:03 | Let us try it with an example. |
06:06 | Type, inside single quotes school in student We get False, since the key 'school' is not present in student. |
06:17 | Type, inside single quotes name in student We get True, as expected. |
06:25 | Next let us see how to retrieve keys and values of a dictionary.
Method keys() is used for getting a list of keys Method values() is used for getting a list of values. |
06:40 | Now let us try the above methods with examples. Type, student dot keys open and close brackets It returns the list of keys of the dictionary student. |
06:56 | Type, student dot values open and close brackets
It returns the list of values of the dictionary student. |
07:07 | Next let us see the items method.
Type, student dot items open and close brackets |
07:16 | It returns a list of tuples of key-value pairs of the dictionary student. |
07:22 | The order of items of the list returned by keys(), values() and items() cannot be predicted. |
07:30 | In dictionaries, they are not in the order in which we inserted them. |
07:35 | Pause the video. Try this exercise and then resume the video. |
07:41 | Print the keys and values of the dictionary student one by one. Hint: use the method items and for loop. |
07:50 | Switch back to the terminal for the solution. |
07:54 | Type as shown. It displays the keys and values one by one. |
08:02 | This brings us to the end of this tutorial. Let us summarize. |
08:08 | In this tutorial, we have learnt to,
Create dictionaries Access elements of a dictionary using keys Add elements to a dictionary by assigning a value to a key |
08:22 | Delete elements from a dictionary by using the function del
Retrieve the keys and values by using the methods keys() and values() respectively |
08:34 | and Iterate over elements of a dictionary using a for loop. |
08:39 | Here are some self assessment questions for you to solve
Dictionary d is defined as shown: How do you retrieve the value 'b'? Delete value 'b' from the dictionary d |
08:55 | And the answers,
d inside square brackets 2 gives the value 'b' del d inside square brackets 2 deletes the value ‘b’ |
09:10 | Please post your timed queries in this forum. |
09:14 | Please post your general queries on Python in this forum. |
09:19 | FOSSEE team coordinates the TBC project. |
09:23 | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website. |
09:34 | This is Priya from IIT Bombay, signing off. Thanks for watching. |