Skip to content

Commit 20b25a5

Browse files
Check Comments & Markdown for more clarity
1 parent 7341733 commit 20b25a5

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed

Diff for: Assignment-5.ipynb

+250
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {},
7+
"source": [
8+
"# Dictionaries\n",
9+
"\n",
10+
"## Its an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. "
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 6,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"{}\n",
23+
"0\n",
24+
"<class 'dict'>\n"
25+
]
26+
}
27+
],
28+
"source": [
29+
"## Empty Dictionaries\n",
30+
"emptyDict = {}\n",
31+
"print(emptyDict)\n",
32+
"print(len(emptyDict))\n",
33+
"print(type(emptyDict))"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 12,
39+
"metadata": {},
40+
"outputs": [
41+
{
42+
"name": "stdout",
43+
"output_type": "stream",
44+
"text": [
45+
"42\n",
46+
"1\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"dict = {'foo' : 42}\n",
52+
"print(dict['foo'])\n",
53+
"print(len(dict))"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"## Differences between list and Dictonary \n",
61+
" - To access values in list you have to give index, whereas in dictonaries you have to give keys.\n",
62+
" - List stores data sequentially, whereas Dictonary doesnot stores data sequentially thats why we access values with help of key.\n"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 18,
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"ename": "KeyError",
72+
"evalue": "'foo'",
73+
"output_type": "error",
74+
"traceback": [
75+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
76+
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
77+
"\u001b[1;32m<ipython-input-18-23eff5f25f73>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mspam\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'bar'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m## we will get key error as we haven't defined any key:value pair named foo\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mspam\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'foo'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
78+
"\u001b[1;31mKeyError\u001b[0m: 'foo'"
79+
]
80+
}
81+
],
82+
"source": [
83+
"spam = {'bar':100}\n",
84+
"## we will get key error as we haven't defined any key:value pair named foo\n",
85+
"spam['foo']"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 23,
91+
"metadata": {},
92+
"outputs": [
93+
{
94+
"data": {
95+
"text/plain": [
96+
"True"
97+
]
98+
},
99+
"execution_count": 23,
100+
"metadata": {},
101+
"output_type": "execute_result"
102+
}
103+
],
104+
"source": [
105+
"spam = {'cat':'meow','dog':'bow'}\n",
106+
"'cat' in spam.keys()"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 24,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"text/plain": [
117+
"True"
118+
]
119+
},
120+
"execution_count": 24,
121+
"metadata": {},
122+
"output_type": "execute_result"
123+
}
124+
],
125+
"source": [
126+
"'cat' in spam"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"## There is no difference proved in above code as in operator checks whether a value exists as a key in the dictionary or not."
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 25,
139+
"metadata": {},
140+
"outputs": [
141+
{
142+
"data": {
143+
"text/plain": [
144+
"True"
145+
]
146+
},
147+
"execution_count": 25,
148+
"metadata": {},
149+
"output_type": "execute_result"
150+
}
151+
],
152+
"source": [
153+
"'cat' in spam"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 26,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"data": {
163+
"text/plain": [
164+
"False"
165+
]
166+
},
167+
"execution_count": 26,
168+
"metadata": {},
169+
"output_type": "execute_result"
170+
}
171+
],
172+
"source": [
173+
"'cat' in spam.values()"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"## In the above 2 blocks of code 'cat' in spam finds if there is a key named 'cat' whereas 'cat' in spam.values() finds 'cat' in values of dictonary"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 36,
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"data": {
190+
"text/plain": [
191+
"{'cat': 'meow', 'dog': 'bow', 'color': 'black'}"
192+
]
193+
},
194+
"execution_count": 36,
195+
"metadata": {},
196+
"output_type": "execute_result"
197+
}
198+
],
199+
"source": [
200+
"## shortcut for the following code?\n",
201+
"## if 'color' not in spam:\n",
202+
"## spam['color'] = 'black'\n",
203+
"spam.setdefault('color', 'black')\n",
204+
"spam['color']\n",
205+
"spam"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 70,
211+
"metadata": {},
212+
"outputs": [
213+
{
214+
"name": "stdout",
215+
"output_type": "stream",
216+
"text": [
217+
"{ 'cat': 'meow',\n",
218+
" 'color': 'black',\n",
219+
" 'dog': 'bow'}\n"
220+
]
221+
}
222+
],
223+
"source": [
224+
"import pprint\n",
225+
"pprint.pprint(spam,indent = 50,width=10, compact=False)"
226+
]
227+
}
228+
],
229+
"metadata": {
230+
"kernelspec": {
231+
"display_name": "Python 3",
232+
"language": "python",
233+
"name": "python3"
234+
},
235+
"language_info": {
236+
"codemirror_mode": {
237+
"name": "ipython",
238+
"version": 3
239+
},
240+
"file_extension": ".py",
241+
"mimetype": "text/x-python",
242+
"name": "python",
243+
"nbconvert_exporter": "python",
244+
"pygments_lexer": "ipython3",
245+
"version": "3.8.5"
246+
}
247+
},
248+
"nbformat": 4,
249+
"nbformat_minor": 4
250+
}

0 commit comments

Comments
 (0)