Python-3.4.3/C3/Dictionaries/English
|
|
Show Slide Containing title | Welcome to the spoken tutorial on Dictionaries. |
Show Slide
Objectives
|
In this tutorial, we will learn to,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite
|
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show Slide
|
First we will learn about dictionaries.
|
Show Slide
|
Keys are unique within a dictionary while values may not be.
|
Open the terminal | Let us start ipython.
|
Type ipython3 and press Enter | Type ipython3 and press Enter.
|
empty = {}
Highlight curly braces |
Let us start by creating an empty dictionary.
|
student = {'name': 'raj',
'age': 16, 'gender': 'male', 'class': 10} |
Now let us see how to create a non empty dictionary.
|
Highlight the key value | Notice that,
Here, we have defined four entries in the dictionary student.
|
Type, student | Type, student to see its content. |
Show Slide
Accessing elements dictionary_name[key] |
Next let us learn to access the dictionary elements.
dictionary underscore name inside square brackets key |
Type, print (student['name'])
|
Now let us access the value for the key ‘name’.
print inside brackets student inside square brackets inside single quotes name
|
Type, print (student['class']) | Now we will retrieve the value for the key ‘class’
print inside brackets student inside square brackets inside single quotes class
|
Type, student['height']
|
If we try to access a value with a wrong key, we get an error.
|
Type, student['height'] = 6.2 | Next, let us see how to add or delete items in a dictionary.
First let us add an element height to the dictionary student.
|
Type, student
Highlight 'height': 6.2 |
Let us now check the content of the dictionary.
|
Type, student['class'] = 11
|
Next let us modify the element class of the dictionary student.
student inside square brackets inside single quotes class is equal to 11 |
Type, student
|
Now, to check the content of the dictionary, type, student
|
Type, del student['age'] | Next let us learn to delete age from the dictionary student.
del student inside square brackets inside single quotes age |
Type, student
|
Type, student
|
Next let us see how to check whether a key is present in a dictionary.
For that we can use the method in. | |
Slide: Usage of in
|
The method in will return True if the key is found in the dictionary.
|
Type, 'school' in student | Let us try it with an example.
Type, inside single quotes school in student We get False, since the key 'school' is not present in student. |
Type, 'name' in student | Type, inside single quotes name in student
We get True, as expected. |
Show Slide
Retrieve keys and values
|
Next let us see how to retrieve keys and values of a dictionary.
|
Type, student.keys()
|
Now let us try the above methods with examples.
|
Type, student.values()
|
Type,
student dot values open and close brackets
|
Type, student.items()
|
Next let us see the items method.
Type, student dot items open and close brackets
|
Pause the video.
| |
Show Slide
Exercise 1
|
Print the keys and values of the dictionary student one by one.
|
Switch to terminal | Switch back to the terminal for the solution. |
Type,
for key, val in student.items(): print (key, val)
|
Type as shown.
|
Show Slide
Summary |
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Summary |
* Delete elements from a dictionary by using the function del
|
Show Slide
Self assessment questions |
Here are some self assessment questions for you to solve
How do you retrieve the value 'b'?
|
Show Slide
Solution of self assessment questions
|
And the answers,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Slide Textbook Companion | FOSSEE team coordinates the TBC project. |
Show Slide Acknowledgment | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website. |
Previous slide | This is Priya from IIT Bombay, signing off. Thanks for watching. |