Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a45ee5f

Browse files
Exception handling
1 parent 1791980 commit a45ee5f

File tree

4 files changed

+688
-0
lines changed

4 files changed

+688
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 4
6+
}

‎4. Data_Types/5. Dictionary.ipynb‎

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## 1. Dictionary\n",
8+
"Python `dictionary` is an `unordered collection of items`. Each item of a dictionary has a `key/value` pair.<br>\n",
9+
"Dictionaries are optimized to retrieve values when the key is known."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"## 2. Creating a Python dictionary\n",
17+
"Creating a dictionary is as simple as placing items inside curly braces {} separated by commas."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 1,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"# empty dictionary\n",
27+
"my_dict = {}\n",
28+
"\n",
29+
"# dictionary with integer keys\n",
30+
"my_dict = {1: 'apple', 2: 'ball'}\n",
31+
"\n",
32+
"# dictionary with mixed keys\n",
33+
"my_dict = {'name': 'John', 1: [2, 4, 3]}\n",
34+
"\n",
35+
"# using dict()\n",
36+
"my_dict = dict({1:'apple', 2:'ball'})\n",
37+
"\n",
38+
"# from sequence having each item as a pair\n",
39+
"my_dict = dict([(1,'apple'), (2,'ball')])"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"## 3. Accessing Elements from Dictionary\n",
47+
"While indexing is used with other data types to access values, a dictionary uses keys. Keys can be used either inside square brackets [] or with the get() method.\n",
48+
"\n",
49+
"If we use the square brackets [], KeyError is raised in case a key is not found in the dictionary. On the other hand, the get() method returns None if the key is not found."
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 2,
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"Jack\n",
62+
"26\n",
63+
"None\n"
64+
]
65+
},
66+
{
67+
"ename": "KeyError",
68+
"evalue": "'address'",
69+
"output_type": "error",
70+
"traceback": [
71+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
72+
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
73+
"\u001b[0;32m<ipython-input-2-2ec6dc01caba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# KeyError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'address'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
74+
"\u001b[0;31mKeyError\u001b[0m: 'address'"
75+
]
76+
}
77+
],
78+
"source": [
79+
"# get vs [] for retrieving elements\n",
80+
"my_dict = {'name': 'Jack', 'age': 26}\n",
81+
"\n",
82+
"# Output: Jack\n",
83+
"print(my_dict['name'])\n",
84+
"\n",
85+
"# Output: 26\n",
86+
"print(my_dict.get('age'))\n",
87+
"\n",
88+
"# Trying to access keys which doesn't exist throws error\n",
89+
"# Output None\n",
90+
"print(my_dict.get('address'))\n",
91+
"\n",
92+
"# KeyError\n",
93+
"print(my_dict['address'])"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"## 4. Changing and Adding Dictionary elements\n",
101+
"Dictionaries are mutable. We can add new items or change the value of existing items using an assignment operator. <br>\n",
102+
"If the key is already present, then the existing value gets updated. In case the key is not present, a new (key: value) pair is added to the dictionary."
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 3,
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"name": "stdout",
112+
"output_type": "stream",
113+
"text": [
114+
"{'name': 'Jack', 'age': 27}\n",
115+
"{'name': 'Jack', 'age': 27, 'address': 'Downtown'}\n"
116+
]
117+
}
118+
],
119+
"source": [
120+
"# Changing and adding Dictionary Elements\n",
121+
"my_dict = {'name': 'Jack', 'age': 26}\n",
122+
"\n",
123+
"# update value\n",
124+
"my_dict['age'] = 27\n",
125+
"\n",
126+
"#Output: {'age': 27, 'name': 'Jack'}\n",
127+
"print(my_dict)\n",
128+
"\n",
129+
"# add item\n",
130+
"my_dict['address'] = 'Downtown'\n",
131+
"\n",
132+
"# Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}\n",
133+
"print(my_dict)"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"## 5. Removing elements from Dictionary\n",
141+
"We can remove a particular item in a dictionary by using the pop() method. This method removes an item with the provided key and returns the value.<br>\n",
142+
"The popitem() method can be used to remove and return an arbitrary (key, value) item pair from the dictionary. All the items can be removed at once, using the clear() method.<br>\n",
143+
"We can also use the del keyword to remove individual items or the entire dictionary itself."
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 4,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"16\n",
156+
"{1: 1, 2: 4, 3: 9, 5: 25}\n",
157+
"(5, 25)\n",
158+
"{1: 1, 2: 4, 3: 9}\n",
159+
"{}\n"
160+
]
161+
},
162+
{
163+
"ename": "NameError",
164+
"evalue": "name 'squares' is not defined",
165+
"output_type": "error",
166+
"traceback": [
167+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
168+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
169+
"\u001b[0;32m<ipython-input-4-d119a0a5b684>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0;31m# Throws Error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 30\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
170+
"\u001b[0;31mNameError\u001b[0m: name 'squares' is not defined"
171+
]
172+
}
173+
],
174+
"source": [
175+
"# Removing elements from a dictionary\n",
176+
"\n",
177+
"# create a dictionary\n",
178+
"squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n",
179+
"\n",
180+
"# remove a particular item, returns its value\n",
181+
"# Output: 16\n",
182+
"print(squares.pop(4))\n",
183+
"\n",
184+
"# Output: {1: 1, 2: 4, 3: 9, 5: 25}\n",
185+
"print(squares)\n",
186+
"\n",
187+
"# remove an arbitrary item, return (key,value)\n",
188+
"# Output: (5, 25)\n",
189+
"print(squares.popitem())\n",
190+
"\n",
191+
"# Output: {1: 1, 2: 4, 3: 9}\n",
192+
"print(squares)\n",
193+
"\n",
194+
"# remove all items\n",
195+
"squares.clear()\n",
196+
"\n",
197+
"# Output: {}\n",
198+
"print(squares)\n",
199+
"\n",
200+
"# delete the dictionary itself\n",
201+
"del squares\n",
202+
"\n",
203+
"# Throws Error\n",
204+
"print(squares)"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"## 6. Python Dictionary Comprehension\n",
212+
"Dictionary comprehension is an elegant and concise way to create a new dictionary from an iterable in Python."
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": 5,
218+
"metadata": {},
219+
"outputs": [
220+
{
221+
"name": "stdout",
222+
"output_type": "stream",
223+
"text": [
224+
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
225+
]
226+
}
227+
],
228+
"source": [
229+
"# Dictionary Comprehension\n",
230+
"squares = {x: x*x for x in range(6)}\n",
231+
"\n",
232+
"print(squares)"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"metadata": {},
238+
"source": [
239+
"## 7. Dictionary Membership Test\n",
240+
"We can test if a key is in a dictionary or not using the keyword in. Notice that the membership test is only for the keys and not for the values."
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 6,
246+
"metadata": {},
247+
"outputs": [
248+
{
249+
"name": "stdout",
250+
"output_type": "stream",
251+
"text": [
252+
"True\n",
253+
"True\n",
254+
"False\n"
255+
]
256+
}
257+
],
258+
"source": [
259+
"# Membership Test for Dictionary Keys\n",
260+
"squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}\n",
261+
"\n",
262+
"# Output: True\n",
263+
"print(1 in squares)\n",
264+
"\n",
265+
"# Output: True\n",
266+
"print(2 not in squares)\n",
267+
"\n",
268+
"# membership tests for key only not value\n",
269+
"# Output: False\n",
270+
"print(49 in squares)"
271+
]
272+
},
273+
{
274+
"cell_type": "markdown",
275+
"metadata": {},
276+
"source": [
277+
"## 8. Iterating Through a Dictionary\n",
278+
"We can iterate through each key in a dictionary using a for loop."
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": 7,
284+
"metadata": {},
285+
"outputs": [
286+
{
287+
"name": "stdout",
288+
"output_type": "stream",
289+
"text": [
290+
"1\n",
291+
"9\n",
292+
"25\n",
293+
"49\n",
294+
"81\n"
295+
]
296+
}
297+
],
298+
"source": [
299+
"# Iterating through a Dictionary\n",
300+
"squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}\n",
301+
"for i in squares:\n",
302+
" print(squares[i])"
303+
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": null,
308+
"metadata": {},
309+
"outputs": [],
310+
"source": []
311+
}
312+
],
313+
"metadata": {
314+
"kernelspec": {
315+
"display_name": "Python 3",
316+
"language": "python",
317+
"name": "python3"
318+
},
319+
"language_info": {
320+
"codemirror_mode": {
321+
"name": "ipython",
322+
"version": 3
323+
},
324+
"file_extension": ".py",
325+
"mimetype": "text/x-python",
326+
"name": "python",
327+
"nbconvert_exporter": "python",
328+
"pygments_lexer": "ipython3",
329+
"version": "3.8.5"
330+
}
331+
},
332+
"nbformat": 4,
333+
"nbformat_minor": 4
334+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /