Skip to content

Commit 6d772cf

Browse files
committed
upadating lecture 7 to github
1 parent 4433550 commit 6d772cf

File tree

1 file changed

+77
-41
lines changed

1 file changed

+77
-41
lines changed

Mod0/Lecture7-DataTypes.ipynb

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
{
2626
"cell_type": "code",
27-
"execution_count": 21,
27+
"execution_count": 5,
2828
"metadata": {},
2929
"outputs": [
3030
{
@@ -65,27 +65,27 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": null,
69-
"metadata": {},
70-
"outputs": [],
71-
"source": []
72-
},
73-
{
74-
"cell_type": "code",
75-
"execution_count": null,
68+
"execution_count": 6,
7669
"metadata": {},
7770
"outputs": [
7871
{
7972
"name": "stdout",
8073
"output_type": "stream",
8174
"text": [
82-
"Data type of song_title: <class 'str'>\n"
75+
"Data type of song_title: <class 'str'>\n",
76+
"Data type of artist: <class 'str'>\n",
77+
"Data type of duration: <class 'float'>\n",
78+
"Data type of play_count: <class 'int'>\n",
79+
"Data type of is_favorite: <class 'bool'>\n"
8380
]
8481
}
8582
],
8683
"source": [
8784
"print(\"Data type of song_title:\", type(song_title))\n",
88-
"print(\"Data type of :\", type())\n",
85+
"print(\"Data type of artist:\", type(artist))\n",
86+
"print(\"Data type of duration:\", type(duration))\n",
87+
"print(\"Data type of play_count:\", type(play_count))\n",
88+
"print(\"Data type of is_favorite:\", type(is_favorite))\n",
8989
"## Fill in the rest of the print statements below for duration, play_count, & is_favorite "
9090
]
9191
},
@@ -98,18 +98,18 @@
9898
},
9999
{
100100
"cell_type": "code",
101-
"execution_count": null,
101+
"execution_count": 7,
102102
"metadata": {},
103103
"outputs": [],
104104
"source": [
105105
"## Run this cell without changes \n",
106106
"\n",
107-
"playlist = [\"Thriller\", 1982, \"Michael Jackson\", {'Budget': 500000}]"
107+
"playlist = [\"Thriller\", 1982, \"Michael Jackson\", {'Budget': 500000}]\n"
108108
]
109109
},
110110
{
111111
"cell_type": "code",
112-
"execution_count": null,
112+
"execution_count": 8,
113113
"metadata": {},
114114
"outputs": [
115115
{
@@ -125,34 +125,55 @@
125125
],
126126
"source": [
127127
"## Before running this cell what item for the list will print out?\n",
128-
"playlist[1]"
128+
"playlist[1] #1982"
129129
]
130130
},
131131
{
132132
"cell_type": "code",
133-
"execution_count": null,
133+
"execution_count": 9,
134134
"metadata": {},
135-
"outputs": [],
135+
"outputs": [
136+
{
137+
"data": {
138+
"text/plain": [
139+
"{'Budget': 500000}"
140+
]
141+
},
142+
"execution_count": 9,
143+
"metadata": {},
144+
"output_type": "execute_result"
145+
}
146+
],
136147
"source": [
137148
"## Return the last item of the list -- try negative indexing \n",
138-
"\n",
139-
"None "
149+
"playlist[-1]"
140150
]
141151
},
142152
{
143153
"cell_type": "code",
144-
"execution_count": null,
154+
"execution_count": 10,
145155
"metadata": {},
146-
"outputs": [],
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"dict"
161+
]
162+
},
163+
"execution_count": 10,
164+
"metadata": {},
165+
"output_type": "execute_result"
166+
}
167+
],
147168
"source": [
148169
"## Return the type of the last item in the list \n",
149170
"\n",
150-
"None "
171+
"type(playlist[3])"
151172
]
152173
},
153174
{
154175
"cell_type": "code",
155-
"execution_count": null,
176+
"execution_count": 11,
156177
"metadata": {},
157178
"outputs": [
158179
{
@@ -168,7 +189,7 @@
168189
],
169190
"source": [
170191
"## Run this -- why did it return what it returned? \n",
171-
"\n",
192+
"# it prints the item before index 2\n",
172193
"playlist[:2]"
173194
]
174195
},
@@ -181,18 +202,35 @@
181202
},
182203
{
183204
"cell_type": "code",
184-
"execution_count": null,
205+
"execution_count": 12,
185206
"metadata": {},
186-
"outputs": [],
207+
"outputs": [
208+
{
209+
"name": "stdout",
210+
"output_type": "stream",
211+
"text": [
212+
"Average duration: 3.7674999999999996\n",
213+
"Another way to find average duration: 3.7675\n"
214+
]
215+
}
216+
],
187217
"source": [
188218
"# Numeric data: durations of songs\n",
189219
"durations = [3.45, 4.02, 3.50, 4.10]\n",
190220
"\n",
191221
"# Non-numeric data: genres of songs\n",
192222
"genres = [\"Pop\", \"R&B\", \"Pop\", \"R&B\"]\n",
193223
"\n",
194-
"# Calculating the average duration of all songs \n",
195-
"average_duration = None "
224+
"# Calculating the average duration of all songs\n",
225+
"total_duration = 0.0\n",
226+
"for value in durations:\n",
227+
" total_duration += value\n",
228+
"\n",
229+
"average_duration = total_duration/len(durations)\n",
230+
"print(\"Average duration:\", average_duration)\n",
231+
"\n",
232+
"print(\"Another way to find average duration:\",sum(durations)/len(durations))\n",
233+
"\n"
196234
]
197235
},
198236
{
@@ -204,18 +242,16 @@
204242
},
205243
{
206244
"cell_type": "code",
207-
"execution_count": null,
245+
"execution_count": 13,
208246
"metadata": {},
209247
"outputs": [
210248
{
211-
"ename": "KeyError",
212-
"evalue": "'Song Titles'",
213-
"output_type": "error",
214-
"traceback": [
215-
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
216-
"\u001b[31mKeyError\u001b[39m Traceback (most recent call last)",
217-
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[13]\u001b[39m\u001b[32m, line 9\u001b[39m\n\u001b[32m 2\u001b[39m playlist_info = {\n\u001b[32m 3\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mSong Items\u001b[39m\u001b[33m\"\u001b[39m: playlist,\n\u001b[32m 4\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mDurations\u001b[39m\u001b[33m\"\u001b[39m: durations,\n\u001b[32m 5\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mGenres\u001b[39m\u001b[33m\"\u001b[39m: genres\n\u001b[32m 6\u001b[39m }\n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m# Accessing data from the dictionary\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m9\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mAll song items:\u001b[39m\u001b[33m\"\u001b[39m, \u001b[43mplaylist_info\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mSong Titles\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m)\n\u001b[32m 10\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mFirst song duration:\u001b[39m\u001b[33m\"\u001b[39m, playlist_info[\u001b[33m\"\u001b[39m\u001b[33mDurations\u001b[39m\u001b[33m\"\u001b[39m][\u001b[32m0\u001b[39m])\n\u001b[32m 12\u001b[39m \u001b[38;5;66;03m# Print the last genre in the genres list use the above print statements to guide you \u001b[39;00m\n",
218-
"\u001b[31mKeyError\u001b[39m: 'Song Titles'"
249+
"name": "stdout",
250+
"output_type": "stream",
251+
"text": [
252+
"All song items: ['Thriller', 1982, 'Michael Jackson', {'Budget': 500000}]\n",
253+
"First song duration: 3.45\n",
254+
"Last genre: R&B\n"
219255
]
220256
}
221257
],
@@ -228,11 +264,11 @@
228264
"}\n",
229265
"\n",
230266
"# Accessing data from the dictionary\n",
231-
"print(\"All song items:\", playlist_info[\"Song Titles\"])\n",
232-
"print(\"First song duration:\", playlist_info[\"Durations\"][0])\n",
267+
"print(\"All song items:\", playlist[:])\n",
268+
"print(\"First song duration:\", durations[0])\n",
233269
"\n",
234270
"# Print the last genre in the genres list use the above print statements to guide you \n",
235-
"None "
271+
"print(\"Last genre:\", genres[-1])"
236272
]
237273
},
238274
{
@@ -266,7 +302,7 @@
266302
"name": "python",
267303
"nbconvert_exporter": "python",
268304
"pygments_lexer": "ipython3",
269-
"version": "3.12.1"
305+
"version": "3.12.7"
270306
}
271307
},
272308
"nbformat": 4,

0 commit comments

Comments
 (0)