1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "provenance" : []
7+ },
8+ "kernelspec" : {
9+ "name" : " python3" ,
10+ "display_name" : " Python 3"
11+ },
12+ "language_info" : {
13+ "name" : " python"
14+ }
15+ },
16+ "cells" : [
17+ {
18+ "cell_type" : " markdown" ,
19+ "source" : [
20+ " # **Exercise: Python Lists**"
21+ ],
22+ "metadata" : {
23+ "id" : " 6cCW1Oz8gFww"
24+ }
25+ },
26+ {
27+ "cell_type" : " markdown" ,
28+ "source" : [
29+ " 1.Let us say your expense for every month are listed below,\n " ,
30+ " \n " ,
31+ " January - 2200\n " ,
32+ " \n " ,
33+ " February - 2350\n " ,
34+ " \n " ,
35+ " March - 2600\n " ,
36+ " \n " ,
37+ " April - 2130\n " ,
38+ " \n " ,
39+ " May - 2190\n " ,
40+ " \n " ,
41+ " Create a list to store these monthly expenses and using that find out,\n " ,
42+ " \n " ,
43+ " 1. In Feb, how many dollars you spent extra compare to January?\n " ,
44+ " 2. Find out your total expense in first quarter (first three months) of the year.\n " ,
45+ " 3. Find out if you spent exactly 2000 dollars in any month\n " ,
46+ " 4. June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list\n " ,
47+ " 5. You returned an item that you bought in a month of April and\n " ,
48+ " got a refund of 200$. Make a correction to your monthly expense list\n " ,
49+ " based on this"
50+ ],
51+ "metadata" : {
52+ "id" : " vWSYW7IogPwH"
53+ }
54+ },
55+ {
56+ "cell_type" : " code" ,
57+ "execution_count" : 2 ,
58+ "metadata" : {
59+ "colab" : {
60+ "base_uri" : " https://localhost:8080/"
61+ },
62+ "id" : " GZimmogPfx2N" ,
63+ "outputId" : " 80a12abd-6fa1-4f74-f025-d2e455819cd5"
64+ },
65+ "outputs" : [
66+ {
67+ "output_type" : " stream" ,
68+ "name" : " stdout" ,
69+ "text" : [
70+ " the dollor spent extra compare to january is: 150\n " ,
71+ " the total expense in first quarter is 7150\n " ,
72+ " Did I spend 2000$ in any month? False\n " ,
73+ " the june expense is: [2200, 2350, 2600, 2130, 2190, 1980]\n " ,
74+ " the april expense is: [2200, 2350, 2600, 1930, 2190, 1980]\n "
75+ ]
76+ }
77+ ],
78+ "source" : [
79+ " expenses = [2200, 2350, 2600, 2130, 2190]\n " ,
80+ " #how many dollors you spent extra compare to january?\n " ,
81+ " print(\" the dollor spent extra compare to january is:\" ,expenses[1]-expenses[0])\n " ,
82+ " #Find out your total expense in first quarter (first three months) of the year.\n " ,
83+ " print(\" the total expense in first quarter is\" ,expenses[0]+expenses[1]+expenses[2])\n " ,
84+ " #Find out if you spent exactly 2000 dollars in any month\n " ,
85+ " print(\" Did I spend 2000$ in any month?\" ,2000 in expenses)\n " ,
86+ " #June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list\n " ,
87+ " expenses.append(1980)\n " ,
88+ " print(\" the june expense is:\" ,expenses)\n " ,
89+ " #you returned an item that you bought in a month of April and got a refund of 200$. Make a correction to your monthly expense list based on this\n " ,
90+ " expenses[3]=expenses[3]-200\n " ,
91+ " print(\" the april expense is:\" ,expenses)\n "
92+ ]
93+ },
94+ {
95+ "cell_type" : " markdown" ,
96+ "source" : [
97+ " **Answer:**Extra spent in February compared to January: 150\n " ,
98+ " \n " ,
99+ " Total expense in first quarter: 7150\n " ,
100+ " \n " ,
101+ " Did I spend exactly $2000 in any month? False\n " ,
102+ " \n " ,
103+ " Expenses after adding June: [2200, 2350, 2600, 2130, 2190, 1980]\n " ,
104+ " \n " ,
105+ " Expenses after April refund: [2200, 2350, 2600, 1930, 2190, 1980]\n " ,
106+ " \n " ,
107+ " **explanation:**\n " ,
108+ " expenses = [2200, 2350, 2600, 2130, 2190]\n " ,
109+ " \n " ,
110+ " stores monthly expenses from Jan to May.\n " ,
111+ " \n " ,
112+ " \n " ,
113+ " expenses[1] - expenses[0] compares February to January.\n " ,
114+ " \n " ,
115+ " \n " ,
116+ " sum(expenses[0:3]) sums up Jan, Feb, and Mar.\n " ,
117+ " \n " ,
118+ " \n " ,
119+ " 2000 in expenses checks if 2000 is present in the list.\n " ,
120+ " \n " ,
121+ " \n " ,
122+ " expenses.append(1980) adds June's expense.\n " ,
123+ " \n " ,
124+ " \n " ,
125+ " expenses[3] = expenses[3] - 200 subtracts $200 refund from April's expense."
126+ ],
127+ "metadata" : {
128+ "id" : " o29oxulTi_Gd"
129+ }
130+ },
131+ {
132+ "cell_type" : " markdown" ,
133+ "source" : [
134+ " 2.You have a list of your favourite marvel super heros."
135+ ],
136+ "metadata" : {
137+ "id" : " MNJZgCCPj3SA"
138+ }
139+ },
140+ {
141+ "cell_type" : " code" ,
142+ "source" : [
143+ " heros=['spider man','thor','hulk','iron man','captain america']"
144+ ],
145+ "metadata" : {
146+ "id" : " jFXWDiKjkDYX"
147+ },
148+ "execution_count" : null ,
149+ "outputs" : []
150+ },
151+ {
152+ "cell_type" : " markdown" ,
153+ "source" : [
154+ " Using this find out,"
155+ ],
156+ "metadata" : {
157+ "id" : " RCBWobJzkHSq"
158+ }
159+ },
160+ {
161+ "cell_type" : " code" ,
162+ "source" : [
163+ " 1. Length of the list\n " ,
164+ " 2. Add 'black panther' at the end of this list\n " ,
165+ " 3. You realize that you need to add 'black panther' after 'hulk',\n " ,
166+ " so remove it from the list first and then add it after 'hulk'\n " ,
167+ " 4. Now you don't like thor and hulk because they get angry easily :)\n " ,
168+ " So you want to remove thor and hulk from list and replace them with doctor strange (because he is cool).\n " ,
169+ " Do that with one line of code.\n " ,
170+ " 5. Sort the heros list in alphabetical order (Hint. Use dir() functions to list down all functions available in list)"
171+ ],
172+ "metadata" : {
173+ "id" : " UnlddeetkL_x"
174+ },
175+ "execution_count" : null ,
176+ "outputs" : []
177+ },
178+ {
179+ "cell_type" : " code" ,
180+ "source" : [
181+ " heros=['spider man','thor','hulk','iron man','captain america']\n " ,
182+ " #Length of the list\n " ,
183+ " len(heros)\n "
184+ ],
185+ "metadata" : {
186+ "colab" : {
187+ "base_uri" : " https://localhost:8080/"
188+ },
189+ "id" : " 8uIQZGiCkQAP" ,
190+ "outputId" : " 333b576c-84ea-4360-c572-95144c2c7e85"
191+ },
192+ "execution_count" : 9 ,
193+ "outputs" : [
194+ {
195+ "output_type" : " execute_result" ,
196+ "data" : {
197+ "text/plain" : [
198+ " 5"
199+ ]
200+ },
201+ "metadata" : {},
202+ "execution_count" : 9
203+ }
204+ ]
205+ },
206+ {
207+ "cell_type" : " markdown" ,
208+ "source" : [
209+ " **Explanation:**\n " ,
210+ " \n " ,
211+ " The list heros contains 5 elements.\n " ,
212+ " \n " ,
213+ " len(heros) returns the total number of items in the list."
214+ ],
215+ "metadata" : {
216+ "id" : " S2k8rxWMnRs5"
217+ }
218+ },
219+ {
220+ "cell_type" : " code" ,
221+ "source" : [
222+ " #Add 'black panther' at the end of this list\n " ,
223+ " heros.append(\" black panther\" )\n " ,
224+ " print(heros)"
225+ ],
226+ "metadata" : {
227+ "colab" : {
228+ "base_uri" : " https://localhost:8080/"
229+ },
230+ "id" : " fslozzK5k4y5" ,
231+ "outputId" : " 26b9060f-2e9d-49c1-d41b-3048f411796e"
232+ },
233+ "execution_count" : 10 ,
234+ "outputs" : [
235+ {
236+ "output_type" : " stream" ,
237+ "name" : " stdout" ,
238+ "text" : [
239+ " ['spider man', 'thor', 'hulk', 'iron man', 'captain america', 'black panther']\n "
240+ ]
241+ }
242+ ]
243+ },
244+ {
245+ "cell_type" : " markdown" ,
246+ "source" : [
247+ " **Explanation:**\n " ,
248+ " \n " ,
249+ " append(\" black panther\" ) adds the new hero to the end of the list.\n " ,
250+ " \n " ,
251+ " The list now contains 6 heroes including \" black panther\" at the last position."
252+ ],
253+ "metadata" : {
254+ "id" : " -VX_3PLDneBe"
255+ }
256+ },
257+ {
258+ "cell_type" : " code" ,
259+ "source" : [
260+ " #You realize that you need to add 'black panther' after 'hulk',so remove it from the list first and then add it after 'hulk'\n " ,
261+ " heros = ['spider man', 'thor', 'hulk', 'iron man', 'captain america', 'black panther']\n " ,
262+ " heros.remove(\" black panther\" )\n " ,
263+ " heros.insert(3,\" black panther\" )\n " ,
264+ " print(heros)"
265+ ],
266+ "metadata" : {
267+ "colab" : {
268+ "base_uri" : " https://localhost:8080/"
269+ },
270+ "id" : " MHQJZPryk6vd" ,
271+ "outputId" : " 3be7f0b6-68d7-47cc-cef5-a794bf5fc686"
272+ },
273+ "execution_count" : 13 ,
274+ "outputs" : [
275+ {
276+ "output_type" : " stream" ,
277+ "name" : " stdout" ,
278+ "text" : [
279+ " ['spider man', 'thor', 'hulk', 'black panther', 'iron man', 'captain america']\n "
280+ ]
281+ }
282+ ]
283+ },
284+ {
285+ "cell_type" : " markdown" ,
286+ "source" : [
287+ " **Explanation:**\n " ,
288+ " \n " ,
289+ " 'hulk' is at index 2, so inserting \" black panther\" at index 3 places it right after hulk.\n " ,
290+ " \n " ,
291+ " First, remove() deletes \" black panther\" from the end.\n " ,
292+ " \n " ,
293+ " Then insert(3, \" black panther\" ) places it correctly."
294+ ],
295+ "metadata" : {
296+ "id" : " 50ElONUhnn1w"
297+ }
298+ },
299+ {
300+ "cell_type" : " code" ,
301+ "source" : [
302+ " #Now you don't like thor and hulk because they get angry easily :)So you want to remove thor and hulk from list and replace them with doctor strange (because he is cool). Do that with one line of code.\n " ,
303+ " heros = ['spider man', 'thor', 'hulk', 'iron man', 'captain america', 'black panther']\n " ,
304+ " heros[1:3] = ['doctor strange']\n " ,
305+ " print(heros)"
306+ ],
307+ "metadata" : {
308+ "colab" : {
309+ "base_uri" : " https://localhost:8080/"
310+ },
311+ "id" : " BRf8ifVallef" ,
312+ "outputId" : " f15283c5-b4f6-453b-c3bf-3f8f1f791778"
313+ },
314+ "execution_count" : 14 ,
315+ "outputs" : [
316+ {
317+ "output_type" : " stream" ,
318+ "name" : " stdout" ,
319+ "text" : [
320+ " ['spider man', 'doctor strange', 'iron man', 'captain america', 'black panther']\n "
321+ ]
322+ }
323+ ]
324+ },
325+ {
326+ "cell_type" : " markdown" ,
327+ "source" : [
328+ " **Explanation:**\n " ,
329+ " \n " ,
330+ " heros[1:3] selects the items at index 1 and 2 ('thor' and 'hulk').\n " ,
331+ " \n " ,
332+ " Replacing that slice with ['doctor strange'] removes both and adds one in their place.\n " ,
333+ " \n " ,
334+ " Clean, short, and effective!"
335+ ],
336+ "metadata" : {
337+ "id" : " IIFFAMFCn4U4"
338+ }
339+ },
340+ {
341+ "cell_type" : " code" ,
342+ "source" : [
343+ " #Sort the heros list in alphabetical order (Hint. Use dir() functions to list down all functions available in list)\n " ,
344+ " heros = ['spider man', 'thor', 'hulk', 'iron man', 'captain america', 'black panther']\n " ,
345+ " heros.sort()\n " ,
346+ " print(heros)"
347+ ],
348+ "metadata" : {
349+ "colab" : {
350+ "base_uri" : " https://localhost:8080/"
351+ },
352+ "id" : " eSJC2MVymcov" ,
353+ "outputId" : " 74e2670d-7335-4edc-eeaa-68bd92d4239c"
354+ },
355+ "execution_count" : 18 ,
356+ "outputs" : [
357+ {
358+ "output_type" : " stream" ,
359+ "name" : " stdout" ,
360+ "text" : [
361+ " ['black panther', 'captain america', 'hulk', 'iron man', 'spider man', 'thor']\n "
362+ ]
363+ }
364+ ]
365+ },
366+ {
367+ "cell_type" : " markdown" ,
368+ "source" : [
369+ " **Explanation:**\n " ,
370+ " \n " ,
371+ " .sort() is an in-place method that sorts the list in alphabetical order.\n " ,
372+ " \n " ,
373+ " You can use dir(list) to explore all available methods for list objects, including append, insert, remove, sort, etc."
374+ ],
375+ "metadata" : {
376+ "id" : " AOkbmMM5oDSm"
377+ }
378+ }
379+ ]
380+ }
0 commit comments