Difference between revisions of "Python-3.4.3/C3/Dictionaries/English"
Nancyvarkey (Talk | contribs) |
|||
Line 22: | Line 22: | ||
* Check for presence of '''keys''' and | * Check for presence of '''keys''' and | ||
* '''Iterate''' over elements. | * '''Iterate''' over elements. | ||
− | |||
− | |||
|- | |- | ||
Line 34: | Line 32: | ||
* '''Python 3.4.3 '''and | * '''Python 3.4.3 '''and | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 48: | Line 44: | ||
* use''' basic data types '''and''' operators''' | * use''' basic data types '''and''' operators''' | ||
− | If not, see the relevant '''Python''' tutorials on this website. | + | *If not, see the relevant '''Python''' tutorials on this website. |
|- | |- | ||
Line 286: | Line 282: | ||
Hence, '''slicing''' and '''striding''' are not valid on '''dictionaries'''. | Hence, '''slicing''' and '''striding''' are not valid on '''dictionaries'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 308: | Line 301: | ||
Retrieve '''keys''' and '''values''' | Retrieve '''keys''' and '''values''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us see how to retrieve keys and values of a dictionary. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us see how to retrieve keys and values of a dictionary. | ||
Line 315: | Line 306: | ||
* Method '''keys'''() is used for getting a '''list''' of '''keys''' | * Method '''keys'''() is used for getting a '''list''' of '''keys''' | ||
* Method''' values() '''is''' '''used''' '''for getting a '''list''' of '''values'''. | * Method''' values() '''is''' '''used''' '''for getting a '''list''' of '''values'''. | ||
− | |||
− | |||
|- | |- | ||
Line 411: | Line 400: | ||
* Access '''elements''' of a '''dictionary''' using '''keys''' | * Access '''elements''' of a '''dictionary''' using '''keys''' | ||
* Add '''elements''' to a '''dictionary''' by assigning a '''value''' to a '''key''' | * Add '''elements''' to a '''dictionary''' by assigning a '''value''' to a '''key''' | ||
− | |||
− | |||
|- | |- | ||
Line 418: | Line 405: | ||
Summary | Summary | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| * Delete '''elements''' from a '''dictionary''' by using the '''function''' '''del''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| |
+ | * Delete '''elements''' from a '''dictionary''' by using the '''function''' '''del''' | ||
* Retrieve the '''keys''' and '''values''' by using the '''methods''' '''keys'''() and '''values() '''respectively and | * Retrieve the '''keys''' and '''values''' by using the '''methods''' '''keys'''() and '''values() '''respectively and | ||
* Iterate over '''elements''' of a '''dictionary''' using a '''for loop'''. | * Iterate over '''elements''' of a '''dictionary''' using a '''for loop'''. | ||
− | |||
− | |||
|- | |- | ||
Line 435: | Line 421: | ||
# Delete '''value 'b' '''from the '''dictionary d''' | # Delete '''value 'b' '''from the '''dictionary d''' | ||
− | |||
− | |||
|- | |- | ||
Line 442: | Line 426: | ||
Solution of self assessment questions | Solution of self assessment questions | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| And the answers, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| And the answers, | ||
Line 449: | Line 431: | ||
# '''d '''''inside square brackets''''' 2 '''gives the '''value 'b'''' | # '''d '''''inside square brackets''''' 2 '''gives the '''value 'b'''' | ||
# '''del d '''''inside square brackets '''''2 '''deletes the '''value ‘b’''' | # '''del d '''''inside square brackets '''''2 '''deletes the '''value ‘b’''' | ||
− | |||
− | |||
|- | |- |
Latest revision as of 18:04, 16 October 2018
|
|
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
|
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 | Note 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 |
|
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. |