|
24 | 24 | }, |
25 | 25 | { |
26 | 26 | "cell_type": "code", |
27 | | - "execution_count": 21, |
| 27 | + "execution_count": 5, |
28 | 28 | "metadata": {}, |
29 | 29 | "outputs": [ |
30 | 30 | { |
|
65 | 65 | }, |
66 | 66 | { |
67 | 67 | "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, |
76 | 69 | "metadata": {}, |
77 | 70 | "outputs": [ |
78 | 71 | { |
79 | 72 | "name": "stdout", |
80 | 73 | "output_type": "stream", |
81 | 74 | "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" |
83 | 80 | ] |
84 | 81 | } |
85 | 82 | ], |
86 | 83 | "source": [ |
87 | 84 | "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", |
89 | 89 | "## Fill in the rest of the print statements below for duration, play_count, & is_favorite " |
90 | 90 | ] |
91 | 91 | }, |
|
98 | 98 | }, |
99 | 99 | { |
100 | 100 | "cell_type": "code", |
101 | | - "execution_count": null, |
| 101 | + "execution_count": 7, |
102 | 102 | "metadata": {}, |
103 | 103 | "outputs": [], |
104 | 104 | "source": [ |
105 | 105 | "## Run this cell without changes \n", |
106 | 106 | "\n", |
107 | | - "playlist = [\"Thriller\", 1982, \"Michael Jackson\", {'Budget': 500000}]" |
| 107 | + "playlist = [\"Thriller\", 1982, \"Michael Jackson\", {'Budget': 500000}]\n" |
108 | 108 | ] |
109 | 109 | }, |
110 | 110 | { |
111 | 111 | "cell_type": "code", |
112 | | - "execution_count": null, |
| 112 | + "execution_count": 8, |
113 | 113 | "metadata": {}, |
114 | 114 | "outputs": [ |
115 | 115 | { |
|
125 | 125 | ], |
126 | 126 | "source": [ |
127 | 127 | "## Before running this cell what item for the list will print out?\n", |
128 | | - "playlist[1]" |
| 128 | + "playlist[1] #1982" |
129 | 129 | ] |
130 | 130 | }, |
131 | 131 | { |
132 | 132 | "cell_type": "code", |
133 | | - "execution_count": null, |
| 133 | + "execution_count": 9, |
134 | 134 | "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 | + ], |
136 | 147 | "source": [ |
137 | 148 | "## Return the last item of the list -- try negative indexing \n", |
138 | | - "\n", |
139 | | - "None " |
| 149 | + "playlist[-1]" |
140 | 150 | ] |
141 | 151 | }, |
142 | 152 | { |
143 | 153 | "cell_type": "code", |
144 | | - "execution_count": null, |
| 154 | + "execution_count": 10, |
145 | 155 | "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 | + ], |
147 | 168 | "source": [ |
148 | 169 | "## Return the type of the last item in the list \n", |
149 | 170 | "\n", |
150 | | - "None " |
| 171 | + "type(playlist[3])" |
151 | 172 | ] |
152 | 173 | }, |
153 | 174 | { |
154 | 175 | "cell_type": "code", |
155 | | - "execution_count": null, |
| 176 | + "execution_count": 11, |
156 | 177 | "metadata": {}, |
157 | 178 | "outputs": [ |
158 | 179 | { |
|
168 | 189 | ], |
169 | 190 | "source": [ |
170 | 191 | "## Run this -- why did it return what it returned? \n", |
171 | | - "\n", |
| 192 | + "# it prints the item before index 2\n", |
172 | 193 | "playlist[:2]" |
173 | 194 | ] |
174 | 195 | }, |
|
181 | 202 | }, |
182 | 203 | { |
183 | 204 | "cell_type": "code", |
184 | | - "execution_count": null, |
| 205 | + "execution_count": 12, |
185 | 206 | "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 | + ], |
187 | 217 | "source": [ |
188 | 218 | "# Numeric data: durations of songs\n", |
189 | 219 | "durations = [3.45, 4.02, 3.50, 4.10]\n", |
190 | 220 | "\n", |
191 | 221 | "# Non-numeric data: genres of songs\n", |
192 | 222 | "genres = [\"Pop\", \"R&B\", \"Pop\", \"R&B\"]\n", |
193 | 223 | "\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" |
196 | 234 | ] |
197 | 235 | }, |
198 | 236 | { |
|
204 | 242 | }, |
205 | 243 | { |
206 | 244 | "cell_type": "code", |
207 | | - "execution_count": null, |
| 245 | + "execution_count": 13, |
208 | 246 | "metadata": {}, |
209 | 247 | "outputs": [ |
210 | 248 | { |
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" |
219 | 255 | ] |
220 | 256 | } |
221 | 257 | ], |
|
228 | 264 | "}\n", |
229 | 265 | "\n", |
230 | 266 | "# 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", |
233 | 269 | "\n", |
234 | 270 | "# 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])" |
236 | 272 | ] |
237 | 273 | }, |
238 | 274 | { |
|
266 | 302 | "name": "python", |
267 | 303 | "nbconvert_exporter": "python", |
268 | 304 | "pygments_lexer": "ipython3", |
269 | | - "version": "3.12.1" |
| 305 | + "version": "3.12.7" |
270 | 306 | } |
271 | 307 | }, |
272 | 308 | "nbformat": 4, |
|
0 commit comments