forked from milaan9/02_Python_Datatypes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
2,188 additions
and
0 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
005_Python_Dictionary_Methods/001_Python_Dictionary_clear().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,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<small><small><i>\n", | ||
"All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/02_Python_Datatypes/tree/main/005_Python_Dictionary_Methods)**\n", | ||
"</i></small></small>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Python Dictionary `clear()`\n", | ||
"\n", | ||
"The **`clear()`** method removes all items from the dictionary.\n", | ||
"\n", | ||
"**Syntax**:\n", | ||
"\n", | ||
"```python\n", | ||
"dict.clear()\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## `clear()` Parameters\n", | ||
"\n", | ||
"The **`clear()`** method doesn't take any parameters." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Return Value from `clear()`\n", | ||
"\n", | ||
"The **`clear()`** method doesn't return any value (returns **`None`**)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2021-06-09T18:09:38.615061Z", | ||
"start_time": "2021-06-09T18:09:38.599481Z" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"d = {}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Example 1: How clear() method works for dictionaries?\n", | ||
"\n", | ||
"d = {1: \"one\", 2: \"two\"}\n", | ||
"\n", | ||
"d.clear()\n", | ||
"print('d =', d)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"hide_input": false, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"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.8" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": true, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": true, | ||
"toc_window_display": false | ||
}, | ||
"varInspector": { | ||
"cols": { | ||
"lenName": 16, | ||
"lenType": 16, | ||
"lenVar": 40 | ||
}, | ||
"kernels_config": { | ||
"python": { | ||
"delete_cmd_postfix": "", | ||
"delete_cmd_prefix": "del ", | ||
"library": "var_list.py", | ||
"varRefreshCmd": "print(var_dic_list())" | ||
}, | ||
"r": { | ||
"delete_cmd_postfix": ") ", | ||
"delete_cmd_prefix": "rm(", | ||
"library": "var_list.r", | ||
"varRefreshCmd": "cat(var_dic_list()) " | ||
} | ||
}, | ||
"types_to_exclude": [ | ||
"module", | ||
"function", | ||
"builtin_function_or_method", | ||
"instance", | ||
"_Feature" | ||
], | ||
"window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
235 changes: 235 additions & 0 deletions
235
005_Python_Dictionary_Methods/002_Python_Dictionary_copy().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,235 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<small><small><i>\n", | ||
"All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/02_Python_Datatypes/tree/main/005_Python_Dictionary_Methods)**\n", | ||
"</i></small></small>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Python Dictionary `copy()`\n", | ||
"\n", | ||
"The **`copy()`** method returns a shallow copy of the dictionary.\n", | ||
"\n", | ||
"**Syntax**:\n", | ||
"\n", | ||
"```python\n", | ||
"dict.copy()\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## `copy()` Parameters\n", | ||
"\n", | ||
"The **`copy()`** method doesn't take any parameters." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Return Value from `copy()`\n", | ||
"\n", | ||
"The **`copy()`** method method returns a shallow copy of the dictionary. It doesn't modify the original dictionary." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2021-06-09T18:09:50.137840Z", | ||
"start_time": "2021-06-09T18:09:50.119339Z" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Orignal: {1: 'one', 2: 'two'}\n", | ||
"New: {1: 'one', 2: 'two'}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Example 1: How copy works for dictionaries?\n", | ||
"\n", | ||
"original = {1:'one', 2:'two'}\n", | ||
"new = original.copy()\n", | ||
"\n", | ||
"print('Orignal: ', original)\n", | ||
"print('New: ', new)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Difference in Using `copy()` method, and `=` Operator to Copy Dictionaries\n", | ||
"\n", | ||
"When **`copy()`** method is used, a new dictionary is created which is filled with a copy of the references from the original dictionary.\n", | ||
"\n", | ||
"When **`=`** operator is used, a new reference to the original dictionary is created." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2021-06-09T18:09:52.023653Z", | ||
"start_time": "2021-06-09T18:09:52.010019Z" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"new: {}\n", | ||
"original: {}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Example 2: Using = Operator to Copy Dictionaries\n", | ||
"\n", | ||
"original = {1:'one', 2:'two'}\n", | ||
"new = original\n", | ||
"\n", | ||
"# removing all elements from the list\n", | ||
"new.clear()\n", | ||
"\n", | ||
"print('new: ', new)\n", | ||
"print('original: ', original)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"**Note:** Here, when **`new`** dictionary is cleared, **`original`** dictionary is also cleared." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2021-06-09T18:09:55.590777Z", | ||
"start_time": "2021-06-09T18:09:55.568376Z" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"new: {}\n", | ||
"original: {1: 'one', 2: 'two'}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Example 3: Using copy() to Copy Dictionaries\n", | ||
"\n", | ||
"original = {1:'one', 2:'two'}\n", | ||
"new = original.copy()\n", | ||
"\n", | ||
"# removing all elements from the list\n", | ||
"new.clear()\n", | ||
"\n", | ||
"print('new: ', new)\n", | ||
"print('original: ', original)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"**Note:** Here, when **`new`** dictionary is cleared, **`original`** dictionary remains unchanged." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"hide_input": false, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"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.8" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": true, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": true, | ||
"toc_window_display": false | ||
}, | ||
"varInspector": { | ||
"cols": { | ||
"lenName": 16, | ||
"lenType": 16, | ||
"lenVar": 40 | ||
}, | ||
"kernels_config": { | ||
"python": { | ||
"delete_cmd_postfix": "", | ||
"delete_cmd_prefix": "del ", | ||
"library": "var_list.py", | ||
"varRefreshCmd": "print(var_dic_list())" | ||
}, | ||
"r": { | ||
"delete_cmd_postfix": ") ", | ||
"delete_cmd_prefix": "rm(", | ||
"library": "var_list.r", | ||
"varRefreshCmd": "cat(var_dic_list()) " | ||
} | ||
}, | ||
"types_to_exclude": [ | ||
"module", | ||
"function", | ||
"builtin_function_or_method", | ||
"instance", | ||
"_Feature" | ||
], | ||
"window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.