Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
milaan9 authored Jun 9, 2021
1 parent 982004c commit e1321b6
Show file tree
Hide file tree
Showing 11 changed files with 2,099 additions and 0 deletions.
131 changes: 131 additions & 0 deletions 005_Python_Dictionary_Methods/001_Python_Dictionary_clear().ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"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.3"
},
"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
}
222 changes: 222 additions & 0 deletions 005_Python_Dictionary_Methods/002_Python_Dictionary_copy().ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"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.3"
},
"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
}
Loading

0 comments on commit e1321b6

Please sign in to comment.