Skip to content

Commit e1321b6

Browse files
authored
Add files via upload
1 parent 982004c commit e1321b6

11 files changed

Lines changed: 2099 additions & 0 deletions
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<small><small><i>\n",
8+
"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",
9+
"</i></small></small>"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Python Dictionary `clear()`\n",
17+
"\n",
18+
"The **`clear()`** method removes all items from the dictionary.\n",
19+
"\n",
20+
"**Syntax**:\n",
21+
"\n",
22+
"```python\n",
23+
"dict.clear()\n",
24+
"```"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"## `clear()` Parameters\n",
32+
"\n",
33+
"The **`clear()`** method doesn't take any parameters."
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Return Value from `clear()`\n",
41+
"\n",
42+
"The **`clear()`** method doesn't return any value (returns **`None`**)."
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 1,
48+
"metadata": {
49+
"ExecuteTime": {
50+
"end_time": "2021-06-09T18:09:38.615061Z",
51+
"start_time": "2021-06-09T18:09:38.599481Z"
52+
}
53+
},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"d = {}\n"
60+
]
61+
}
62+
],
63+
"source": [
64+
"# Example 1: How clear() method works for dictionaries?\n",
65+
"\n",
66+
"d = {1: \"one\", 2: \"two\"}\n",
67+
"\n",
68+
"d.clear()\n",
69+
"print('d =', d)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": null,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": []
78+
}
79+
],
80+
"metadata": {
81+
"hide_input": false,
82+
"kernelspec": {
83+
"display_name": "Python 3",
84+
"language": "python",
85+
"name": "python3"
86+
},
87+
"language_info": {
88+
"codemirror_mode": {
89+
"name": "ipython",
90+
"version": 3
91+
},
92+
"file_extension": ".py",
93+
"mimetype": "text/x-python",
94+
"name": "python",
95+
"nbconvert_exporter": "python",
96+
"pygments_lexer": "ipython3",
97+
"version": "3.8.3"
98+
},
99+
"varInspector": {
100+
"cols": {
101+
"lenName": 16,
102+
"lenType": 16,
103+
"lenVar": 40
104+
},
105+
"kernels_config": {
106+
"python": {
107+
"delete_cmd_postfix": "",
108+
"delete_cmd_prefix": "del ",
109+
"library": "var_list.py",
110+
"varRefreshCmd": "print(var_dic_list())"
111+
},
112+
"r": {
113+
"delete_cmd_postfix": ") ",
114+
"delete_cmd_prefix": "rm(",
115+
"library": "var_list.r",
116+
"varRefreshCmd": "cat(var_dic_list()) "
117+
}
118+
},
119+
"types_to_exclude": [
120+
"module",
121+
"function",
122+
"builtin_function_or_method",
123+
"instance",
124+
"_Feature"
125+
],
126+
"window_display": false
127+
}
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 2
131+
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<small><small><i>\n",
8+
"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",
9+
"</i></small></small>"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Python Dictionary `copy()`\n",
17+
"\n",
18+
"The **`copy()`** method returns a shallow copy of the dictionary.\n",
19+
"\n",
20+
"**Syntax**:\n",
21+
"\n",
22+
"```python\n",
23+
"dict.copy()\n",
24+
"```"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"## `copy()` Parameters\n",
32+
"\n",
33+
"The **`copy()`** method doesn't take any parameters."
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Return Value from `copy()`\n",
41+
"\n",
42+
"The **`copy()`** method method returns a shallow copy of the dictionary. It doesn't modify the original dictionary."
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 1,
48+
"metadata": {
49+
"ExecuteTime": {
50+
"end_time": "2021-06-09T18:09:50.137840Z",
51+
"start_time": "2021-06-09T18:09:50.119339Z"
52+
}
53+
},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"Orignal: {1: 'one', 2: 'two'}\n",
60+
"New: {1: 'one', 2: 'two'}\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"# Example 1: How copy works for dictionaries?\n",
66+
"\n",
67+
"original = {1:'one', 2:'two'}\n",
68+
"new = original.copy()\n",
69+
"\n",
70+
"print('Orignal: ', original)\n",
71+
"print('New: ', new)"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"## Difference in Using `copy()` method, and `=` Operator to Copy Dictionaries\n",
79+
"\n",
80+
"When **`copy()`** method is used, a new dictionary is created which is filled with a copy of the references from the original dictionary.\n",
81+
"\n",
82+
"When **`=`** operator is used, a new reference to the original dictionary is created."
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 2,
88+
"metadata": {
89+
"ExecuteTime": {
90+
"end_time": "2021-06-09T18:09:52.023653Z",
91+
"start_time": "2021-06-09T18:09:52.010019Z"
92+
}
93+
},
94+
"outputs": [
95+
{
96+
"name": "stdout",
97+
"output_type": "stream",
98+
"text": [
99+
"new: {}\n",
100+
"original: {}\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"# Example 2: Using = Operator to Copy Dictionaries\n",
106+
"\n",
107+
"original = {1:'one', 2:'two'}\n",
108+
"new = original\n",
109+
"\n",
110+
"# removing all elements from the list\n",
111+
"new.clear()\n",
112+
"\n",
113+
"print('new: ', new)\n",
114+
"print('original: ', original)"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"**Note:** Here, when **`new`** dictionary is cleared, **`original`** dictionary is also cleared."
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 3,
127+
"metadata": {
128+
"ExecuteTime": {
129+
"end_time": "2021-06-09T18:09:55.590777Z",
130+
"start_time": "2021-06-09T18:09:55.568376Z"
131+
}
132+
},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"new: {}\n",
139+
"original: {1: 'one', 2: 'two'}\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"# Example 3: Using copy() to Copy Dictionaries\n",
145+
"\n",
146+
"original = {1:'one', 2:'two'}\n",
147+
"new = original.copy()\n",
148+
"\n",
149+
"# removing all elements from the list\n",
150+
"new.clear()\n",
151+
"\n",
152+
"print('new: ', new)\n",
153+
"print('original: ', original)"
154+
]
155+
},
156+
{
157+
"cell_type": "markdown",
158+
"metadata": {},
159+
"source": [
160+
"**Note:** Here, when **`new`** dictionary is cleared, **`original`** dictionary remains unchanged."
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": []
169+
}
170+
],
171+
"metadata": {
172+
"hide_input": false,
173+
"kernelspec": {
174+
"display_name": "Python 3",
175+
"language": "python",
176+
"name": "python3"
177+
},
178+
"language_info": {
179+
"codemirror_mode": {
180+
"name": "ipython",
181+
"version": 3
182+
},
183+
"file_extension": ".py",
184+
"mimetype": "text/x-python",
185+
"name": "python",
186+
"nbconvert_exporter": "python",
187+
"pygments_lexer": "ipython3",
188+
"version": "3.8.3"
189+
},
190+
"varInspector": {
191+
"cols": {
192+
"lenName": 16,
193+
"lenType": 16,
194+
"lenVar": 40
195+
},
196+
"kernels_config": {
197+
"python": {
198+
"delete_cmd_postfix": "",
199+
"delete_cmd_prefix": "del ",
200+
"library": "var_list.py",
201+
"varRefreshCmd": "print(var_dic_list())"
202+
},
203+
"r": {
204+
"delete_cmd_postfix": ") ",
205+
"delete_cmd_prefix": "rm(",
206+
"library": "var_list.r",
207+
"varRefreshCmd": "cat(var_dic_list()) "
208+
}
209+
},
210+
"types_to_exclude": [
211+
"module",
212+
"function",
213+
"builtin_function_or_method",
214+
"instance",
215+
"_Feature"
216+
],
217+
"window_display": false
218+
}
219+
},
220+
"nbformat": 4,
221+
"nbformat_minor": 2
222+
}

0 commit comments

Comments
 (0)