Skip to content

Commit abb396e

Browse files
authored
Upload file
1 parent fb644ec commit abb396e

File tree

1 file changed

+376
-0
lines changed

1 file changed

+376
-0
lines changed
Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"In this lesson I learn about Data Structures in Python"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"Quiz: List Indexing¶\n",
15+
"\n",
16+
"Use list indexing to determine how many days are in a particular month based on the integer variable month, and store that value in the integer variable num_days. For example, if month is 8, num_days should be set to 31, since the eighth month, August, has 31 days.\n",
17+
"\n",
18+
"Remember to account for zero-based indexing!\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": [
30+
"31\n"
31+
]
32+
}
33+
],
34+
"source": [
35+
"month = 8\n",
36+
"days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
37+
"\n",
38+
"# : replace None with appropriate code\n",
39+
"# Use list indexing to determine the number of days in `month`\n",
40+
"num_days = days_in_month[month - 1]\n",
41+
"print(num_days)"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"Quiz: Slicing Lists\n",
49+
"\n",
50+
"Select the three most recent dates from this list using list slicing notation. Hint: negative indexes work in slices!\n"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 3,
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"['November 13, 2012', 'March 20, 2015', 'March 9, 2016']\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003',\n",
68+
" 'March 29, 2006', 'August 1, 2008', 'July 22, 2009',\n",
69+
" 'July 11, 2010', 'November 13, 2012', 'March 20, 2015',\n",
70+
" 'March 9, 2016'] \n",
71+
" \n",
72+
"# Replace None with appropriate code\n",
73+
"# Modify this code so it prints the last three elements of the list\n",
74+
"last_three_dates = eclipse_dates[-3:]\n",
75+
"print(last_three_dates)"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"Quiz: Define a Dictionary¶\n",
83+
"\n",
84+
"Define a dictionary named population that contains this data:\n",
85+
"Keys \tValues\n",
86+
"Shanghai \t17.8\n",
87+
"Istanbul \t13.3\n",
88+
"Karachi \t13.0\n",
89+
"Mumbai \t12.5"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 1,
95+
"metadata": {},
96+
"outputs": [
97+
{
98+
"name": "stdout",
99+
"output_type": "stream",
100+
"text": [
101+
"{'Shanghai': 17.8, 'Istanbul': 13.3, 'Karachi': 13.0, 'Mumbai': 12.5}\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"population = {'Shanghai': 17.8,\n",
107+
" 'Istanbul': 13.3,\n",
108+
" 'Karachi': 13.0,\n",
109+
" 'Mumbai': 12.5}\n",
110+
"print(population)"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"Quiz: Identify the Problem\n",
118+
"\n",
119+
"Run the code below - it should break. Take a look at the error message and try to figure out what the issue is. Then, answer the quiz question below the editor."
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 2,
125+
"metadata": {},
126+
"outputs": [
127+
{
128+
"ename": "TypeError",
129+
"evalue": "unhashable type: 'list'",
130+
"output_type": "error",
131+
"traceback": [
132+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
133+
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
134+
"Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m room_numbers \u001b[39m=\u001b[39m {\n\u001b[0;32m 2\u001b[0m [\u001b[39m'\u001b[39m\u001b[39mFreddie\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mJen\u001b[39m\u001b[39m'\u001b[39m]: \u001b[39m403\u001b[39m,\n\u001b[0;32m 3\u001b[0m [\u001b[39m'\u001b[39m\u001b[39mNed\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mKeith\u001b[39m\u001b[39m'\u001b[39m]: \u001b[39m391\u001b[39m,\n\u001b[0;32m 4\u001b[0m [\u001b[39m'\u001b[39m\u001b[39mKristin\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mJazzmyne\u001b[39m\u001b[39m'\u001b[39m]: \u001b[39m411\u001b[39m,\n\u001b[0;32m 5\u001b[0m [\u001b[39m'\u001b[39m\u001b[39mEugene\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mZach\u001b[39m\u001b[39m'\u001b[39m]: \u001b[39m395\u001b[39m\n\u001b[0;32m 6\u001b[0m }\n",
135+
"\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
136+
]
137+
}
138+
],
139+
"source": [
140+
"room_numbers = {\n",
141+
" ['Freddie', 'Jen']: 403,\n",
142+
" ['Ned', 'Keith']: 391,\n",
143+
" ['Kristin', 'Jazzmyne']: 411,\n",
144+
" ['Eugene', 'Zach']: 395\n",
145+
"}"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 8,
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"data": {
155+
"text/plain": [
156+
"{('Freddie', 'Jen'): 403,\n",
157+
" ('Ned', 'Keith'): 391,\n",
158+
" ('Kristin', 'Jazzmyne'): 411,\n",
159+
" ('Eugene', 'Zach'): 395}"
160+
]
161+
},
162+
"execution_count": 8,
163+
"metadata": {},
164+
"output_type": "execute_result"
165+
}
166+
],
167+
"source": [
168+
"room_numbers = {\n",
169+
" ('Freddie', 'Jen'): 403,\n",
170+
" ('Ned', 'Keith'): 391,\n",
171+
" ('Kristin', 'Jazzmyne'): 411,\n",
172+
" ('Eugene', 'Zach'): 395\n",
173+
"}\n",
174+
"room_numbers"
175+
]
176+
},
177+
{
178+
"cell_type": "markdown",
179+
"metadata": {},
180+
"source": [
181+
"Quiz: Compound Data Structures\n",
182+
"Quiz: Adding Values to Nested Dictionaries\n",
183+
"\n",
184+
"Try your hand at working with nested dictionaries. Add another entry, 'is_noble_gas,' to each dictionary in the `elements` dictionary. After inserting the new entries you should be able to perform these lookups:\n",
185+
"\n",
186+
"```python\n",
187+
">>> print(elements['hydrogen']['is_noble_gas'])\n",
188+
"False\n",
189+
">>> print(elements['helium']['is_noble_gas'])\n",
190+
"True\n",
191+
"```"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": null,
197+
"metadata": {},
198+
"outputs": [],
199+
"source": [
200+
"elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'},\n",
201+
" 'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}\n",
202+
"# Add an 'is_noble_gas' entry to the hydrogen and helium dictionaries\n",
203+
"# hint: helium is a noble gas, hydrogen isn't\n",
204+
"\n",
205+
"elements['hydrogen']['is_noble_gas'] = False\n",
206+
"elements['helium']['is_noble_gas'] = True"
207+
]
208+
},
209+
{
210+
"cell_type": "markdown",
211+
"metadata": {},
212+
"source": [
213+
"Quiz: Count Unique Words\n",
214+
"\n",
215+
"Your task for this quiz is to find the number of unique words in the text. In the code editor below, complete these three steps to get your answer.\n",
216+
"\n",
217+
" Split verse into a list of words. Hint: You can use a string method you learned in the previous lesson.\n",
218+
" Convert the list into a data structure that would keep only the unique elements from the list.\n",
219+
" Print the length of the container."
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {},
226+
"outputs": [],
227+
"source": [
228+
"verse = \"if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise\"\n",
229+
"print(verse, '\\n')\n",
230+
"\n",
231+
"# TODO: replace None with appropriate code\n",
232+
"# split verse into list of words\n",
233+
"verse_list = None\n",
234+
"print(verse_list, '\\n')\n",
235+
"\n",
236+
"# TODO: replace None with appropriate code\n",
237+
"# convert list to a data structure that stores unique elements\n",
238+
"verse_set = None\n",
239+
"print(verse_set, '\\n')\n",
240+
"\n",
241+
"# TODO: replace None with appropriate code\n",
242+
"# find the number of unique words\n",
243+
"num_unique = None\n",
244+
"print(num_unique, '\\n')"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {},
250+
"source": [
251+
"Quiz: Count Unique Words\n",
252+
"\n",
253+
"Your task for this quiz is to find the number of unique words in the text. In the code editor below, complete these three steps to get your answer.\n",
254+
"\n",
255+
"1. Split `verse` into a list of words. **Hint:** You can use a string method you learned in the previous lesson.\n",
256+
"1. Convert the list into a data structure that would keep only the unique elements from the list. \n",
257+
"1. Print the length of the container."
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 9,
263+
"metadata": {},
264+
"outputs": [
265+
{
266+
"name": "stdout",
267+
"output_type": "stream",
268+
"text": [
269+
"if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise \n",
270+
"\n",
271+
"['if', 'you', 'can', 'keep', 'your', 'head', 'when', 'all', 'about', 'you', 'are', 'losing', 'theirs', 'and', 'blaming', 'it', 'on', 'you', 'if', 'you', 'can', 'trust', 'yourself', 'when', 'all', 'men', 'doubt', 'you', 'but', 'make', 'allowance', 'for', 'their', 'doubting', 'too', 'if', 'you', 'can', 'wait', 'and', 'not', 'be', 'tired', 'by', 'waiting', 'or', 'being', 'lied', 'about', 'don’t', 'deal', 'in', 'lies', 'or', 'being', 'hated', 'don’t', 'give', 'way', 'to', 'hating', 'and', 'yet', 'don’t', 'look', 'too', 'good', 'nor', 'talk', 'too', 'wise'] \n",
272+
"\n",
273+
"{'doubting', 'on', 'allowance', 'head', 'to', 'yet', 'about', 'give', 'in', 'look', 'it', 'trust', 'don’t', 'if', 'deal', 'for', 'men', 'doubt', 'being', 'hated', 'lied', 'talk', 'good', 'not', 'blaming', 'waiting', 'can', 'way', 'yourself', 'but', 'lies', 'keep', 'all', 'too', 'and', 'nor', 'hating', 'theirs', 'be', 'wise', 'their', 'losing', 'tired', 'when', 'are', 'make', 'or', 'you', 'by', 'your', 'wait'} \n",
274+
"\n",
275+
"51\n"
276+
]
277+
}
278+
],
279+
"source": [
280+
"verse = \"if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise\"\n",
281+
"print(verse, \"\\n\")\n",
282+
"\n",
283+
"## split verse into list of words\n",
284+
"verse_list = verse.split()\n",
285+
"print(verse_list, '\\n')\n",
286+
"\n",
287+
"## convert list to set to get unique words\n",
288+
"verse_set = set(verse_list)\n",
289+
"print(verse_set, '\\n')\n",
290+
"\n",
291+
"## print the number of unique words\n",
292+
"num_unique = len(verse_set)\n",
293+
"print(num_unique)"
294+
]
295+
},
296+
{
297+
"cell_type": "markdown",
298+
"metadata": {},
299+
"source": [
300+
"Quiz: Verse Dictionary\n",
301+
"\n",
302+
"In the code cell below, you'll find a dictionary containing the unique words of `verse` stored as keys and the number of times they appear in `verse` stored as values. Use this dictionary to answer the following questions. **Submit these answers in the quiz below this Jupyter Notebook.**\n",
303+
"\n",
304+
"Try to answer these using code, rather than inspecting the dictionary manually!\n",
305+
"\n",
306+
"1. How many unique words are in *verse_dict*?\n",
307+
"1. Is the key \"breathe\" in *verse_dict*?\n",
308+
"1. What is the first element in the list created when *verse_dict* is sorted by keys?\n",
309+
"**Hint:** Use the appropriate dictionary method to get a list of its keys, and then sort that list. Use this list of keys to answer the next two questions as well.\n",
310+
"1. Which key (word) has the highest value in *verse_dict*?"
311+
]
312+
},
313+
{
314+
"cell_type": "code",
315+
"execution_count": 10,
316+
"metadata": {},
317+
"outputs": [
318+
{
319+
"name": "stdout",
320+
"output_type": "stream",
321+
"text": [
322+
"{'if': 3, 'you': 6, 'can': 3, 'keep': 1, 'your': 1, 'head': 1, 'when': 2, 'all': 2, 'about': 2, 'are': 1, 'losing': 1, 'theirs': 1, 'and': 3, 'blaming': 1, 'it': 1, 'on': 1, 'trust': 1, 'yourself': 1, 'men': 1, 'doubt': 1, 'but': 1, 'make': 1, 'allowance': 1, 'for': 1, 'their': 1, 'doubting': 1, 'too': 3, 'wait': 1, 'not': 1, 'be': 1, 'tired': 1, 'by': 1, 'waiting': 1, 'or': 2, 'being': 2, 'lied': 1, \"don't\": 3, 'deal': 1, 'in': 1, 'lies': 1, 'hated': 1, 'give': 1, 'way': 1, 'to': 1, 'hating': 1, 'yet': 1, 'look': 1, 'good': 1, 'nor': 1, 'talk': 1, 'wise': 1} \n",
323+
"\n",
324+
"51\n",
325+
"False\n",
326+
"about\n",
327+
"yourself\n"
328+
]
329+
}
330+
],
331+
"source": [
332+
"verse_dict = {'if': 3, 'you': 6, 'can': 3, 'keep': 1, 'your': 1, 'head': 1, 'when': 2, 'all': 2, 'about': 2, 'are': 1, 'losing': 1, 'theirs': 1, 'and': 3, 'blaming': 1, 'it': 1, 'on': 1, 'trust': 1, 'yourself': 1, 'men': 1, 'doubt': 1, 'but': 1, 'make': 1, 'allowance': 1, 'for': 1, 'their': 1, 'doubting': 1, 'too': 3, 'wait': 1, 'not': 1, 'be': 1, 'tired': 1, 'by': 1, 'waiting': 1, 'or': 2, 'being': 2, 'lied': 1, 'don\\'t': 3, 'deal': 1, 'in': 1, 'lies': 1, 'hated': 1, 'give': 1, 'way': 1, 'to': 1, 'hating': 1, 'yet': 1, 'look': 1, 'good': 1, 'nor': 1, 'talk': 1, 'wise': 1}\n",
333+
"print(verse_dict, '\\n')\n",
334+
"\n",
335+
"## find number of unique keys in the dictionary\n",
336+
"num_keys = len(verse_dict)\n",
337+
"print(num_keys)\n",
338+
"\n",
339+
"## find whether 'breathe' is a key in the dictionary\n",
340+
"contains_breathe = \"breathe\" in verse_dict\n",
341+
"print(contains_breathe)\n",
342+
"\n",
343+
"## create and sort a list of the dictionary's keys\n",
344+
"sorted_keys = sorted(verse_dict.keys())\n",
345+
"\n",
346+
"## get the first element in the sorted list of keys\n",
347+
"print(sorted_keys[0])\n",
348+
"\n",
349+
"## find the element with the highest value in the list of keys\n",
350+
"print(sorted_keys[-1]) \n"
351+
]
352+
}
353+
],
354+
"metadata": {
355+
"kernelspec": {
356+
"display_name": "Python 3",
357+
"language": "python",
358+
"name": "python3"
359+
},
360+
"language_info": {
361+
"codemirror_mode": {
362+
"name": "ipython",
363+
"version": 3
364+
},
365+
"file_extension": ".py",
366+
"mimetype": "text/x-python",
367+
"name": "python",
368+
"nbconvert_exporter": "python",
369+
"pygments_lexer": "ipython3",
370+
"version": "3.11.0"
371+
},
372+
"orig_nbformat": 4
373+
},
374+
"nbformat": 4,
375+
"nbformat_minor": 2
376+
}

0 commit comments

Comments
 (0)