-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #709 from Varshni11/basics-python-ipynb
Create jupyter notebooks for python basics lecture files
- Loading branch information
Showing
42 changed files
with
3,444 additions
and
0 deletions.
There are no files selected for viewing
295 changes: 295 additions & 0 deletions
295
Basics of the Python/Basic of Python/2.TUPLES_LECTURE/DICTIONARIES.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,295 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h1 style=\"color:blue\">DICTIONARIES</h1>\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<ul type=\"disc\">\n", | ||
"<li>Consists of keys and key values separated by a colon</li>\n", | ||
"<li> Dictionaries, also called dict can contain,\n", | ||
"<ul type=\"square\">\n", | ||
"<li>Strings and numbers</li>\n", | ||
"<li>Tuples</li>\n", | ||
"<li>lists</li>\n", | ||
"<li>sets</li>\n", | ||
"<li>dataframes</li>\n", | ||
"<li>series</li>\n", | ||
"<li>nested dictionaries</li>\n", | ||
"</ul>\n", | ||
"</li>\n", | ||
"</ul>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"dict" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"d1={}\n", | ||
"type(d1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h2>Adding elements into dictionary</h2>\n", | ||
"<p> Elements can be added to dictionary as a key, value pairs in the following way:</p>\n", | ||
"<code> dictionary[key]=value </code>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{'A': 10, 'B': 20, 'C': 30}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"d1['A']=10\n", | ||
"d1['B']=20\n", | ||
"d1['C']=30\n", | ||
"print(d1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h2>Changing values in dictionary</h2>\n", | ||
"<p>Dictionaries are mutable, meaning the values referenced by dictionary keys can be changed. This is illustrated in the following example...</p>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Dictionaries before changing value of 'C' {'A': 10, 'B': 20, 'C': 1000}\n", | ||
"Dictionaries after changing value of 'C' {'A': 10, 'B': 20, 'C': 1000}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(\"Dictionaries before changing value of 'C'\",d1)\n", | ||
"d1[\"C\"] = 1000\n", | ||
"print(\"Dictionaries after changing value of 'C'\",d1)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<hr><br>We can also update a dictionary with elements of another dictionary as follows..." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'robot': 40, 'car': 50, 'A': 10, 'B': 20, 'C': 1000}" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"toys = {\"robot\": 40, \"car\": 50}\n", | ||
"toys.update(d1)\n", | ||
"toys\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h2> Accessing the keys and values seperately</h2>\n", | ||
"We can get the keys and values seperately using keys() and values() methods respectively. They can converted to a list by wrapping it inside the list() method" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"dict_values([40, 50, 20, 1000])" | ||
] | ||
}, | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"toys.values()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"dict_keys(['robot', 'car', 'A', 'B', 'C'])" | ||
] | ||
}, | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"list(toys.keys())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h2>Deleting an element from Dictionary</h2>\n", | ||
"<p>To remove an key-value pair, we use pop(key)</p>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'robot': 40, 'car': 50, 'B': 20, 'C': 1000}" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"\n", | ||
"toys.pop(\"A\")\n", | ||
"toys\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"More examples..." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'k1': [0, 1, 2, 3],\n", | ||
" 'k2': (4, 5, 6, 7),\n", | ||
" 'k3': (1, 2, 3, {'k4': [1, 2, 3, 'found you!', 4, 5]})}" | ||
] | ||
}, | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"maze = {\"k1\": list(range(4)), \"k2\": tuple(range(4,8)),\n", | ||
"\"k3\": (1,2, 3, {\"k4\": [1,2,3, \"found you!\", 4, 5]})}\n", | ||
"maze\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'found you!'" | ||
] | ||
}, | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"maze[\"k3\"][3][\"k4\"][3]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "vars", | ||
"language": "python", | ||
"name": "vars" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.