{"cells":[{"cell_type":"markdown","id":"c30444b7-1163-4ad4-8366-c9e11499218d","metadata":{},"outputs":[],"source":["
Artist | \n","Album | \n","Released | \n","Length | \n","Genre | \n","Music recording sales (millions) | \n","Claimed sales (millions) | \n","Released | \n","Soundtrack | \n","Rating (friends) | \n","
---|---|---|---|---|---|---|---|---|---|
Michael Jackson | \n","Thriller | \n","1982 | \n","00:42:19 | \n","Pop, rock, R&B | \n","46 | \n","65 | \n","30-Nov-82 | \n","\n"," | 10.0 | \n","
AC/DC | \n","Back in Black | \n","1980 | \n","00:42:11 | \n","Hard rock | \n","26.1 | \n","50 | \n","25-Jul-80 | \n","\n"," | 8.5 | \n","
Pink Floyd | \n","The Dark Side of the Moon | \n","1973 | \n","00:42:49 | \n","Progressive rock | \n","24.2 | \n","45 | \n","01-Mar-73 | \n","\n"," | 9.5 | \n","
Whitney Houston | \n","The Bodyguard | \n","1992 | \n","00:57:44 | \n","Soundtrack/R&B, soul, pop | \n","26.1 | \n","50 | \n","25-Jul-80 | \n","Y | \n","7.0 | \n","
Meat Loaf | \n","Bat Out of Hell | \n","1977 | \n","00:46:33 | \n","Hard rock, progressive rock | \n","20.6 | \n","43 | \n","21-Oct-77 | \n","\n"," | 7.0 | \n","
Eagles | \n","Their Greatest Hits (1971-1975) | \n","1976 | \n","00:43:08 | \n","Rock, soft rock, folk rock | \n","32.2 | \n","42 | \n","17-Feb-76 | \n","\n"," | 9.5 | \n","
Bee Gees | \n","Saturday Night Fever | \n","1977 | \n","1:15:54 | \n","Disco | \n","20.6 | \n","40 | \n","15-Nov-77 | \n","Y | \n","9.0 | \n","
Fleet wood Mac | \n","Rumours | \n","1977 | \n","00:40:01 | \n","Soft rock | \n","27.9 | \n","40 | \n","04-Feb-77 | \n","\n"," | 9.5 | \n","
extend
to add new elements to the list:\n"]},{"cell_type":"code","id":"52ab07d2-69d0-42cb-9ca8-8496c579a9cb","metadata":{},"outputs":[],"source":["# Use extend to add elements to list\n\nL = [ \"Michael Jackson\", 10.2]\nL.extend(['pop', 10])\nL"]},{"cell_type":"markdown","id":"a7ecd8c4-4b47-4995-8061-201b1fe30105","metadata":{},"outputs":[],"source":["Another similar method is append
. If we apply append
instead of extend
, we add one element to the list:\n"]},{"cell_type":"code","id":"dcae0a42-37e1-430b-80b1-df553972d89d","metadata":{},"outputs":[],"source":["# Use append to add elements to list\n\nL = [ \"Michael Jackson\", 10.2]\nL.append(['pop', 10])\nL"]},{"cell_type":"markdown","id":"cc408db9-65b0-426d-b105-b942535b8e63","metadata":{},"outputs":[],"source":["Each time we apply a method, the list changes. If we apply extend
we add two new elements to the list. The list L
is then modified by adding two new elements:\n"]},{"cell_type":"code","id":"35696b79-c288-4752-b991-34d763d7beeb","metadata":{},"outputs":[],"source":["# Use extend to add elements to list\n\nL = [ \"Michael Jackson\", 10.2]\nL.extend(['pop', 10])\nL"]},{"cell_type":"markdown","id":"0e16a1d3-5e0c-4697-a7a3-4eed92481355","metadata":{},"outputs":[],"source":["If we append the list \\['a','b']
we have one new element consisting of a nested list:\n"]},{"cell_type":"code","id":"3f102f43-e80b-4c86-80de-33799cd99d5b","metadata":{},"outputs":[],"source":["# Use append to add elements to list\n\nL.append(['a','b'])\nL"]},{"cell_type":"markdown","id":"e061f0b3-365e-45ec-8a35-c7b0180cd23d","metadata":{},"outputs":[],"source":["As lists are mutable, we can change them. For example, we can change the first element as follows:\n"]},{"cell_type":"code","id":"863c20f1-c61a-4b54-94bc-5846bc7b3d21","metadata":{},"outputs":[],"source":["# Change the element based on the index\n\nA = [\"disco\", 10, 1.2]\nprint('Before change:', A)\nA[0] = 'hard rock'\nprint('After change:', A)"]},{"cell_type":"markdown","id":"b63e3d7d-d3ef-47a1-9766-30c18bbffa92","metadata":{},"outputs":[],"source":["We can also delete an element of a list using the del
command:\n"]},{"cell_type":"code","id":"93f6df4f-9195-46c2-8c02-aa63ce7d694a","metadata":{},"outputs":[],"source":["# Delete the element based on the index\n\nprint('Before change:', A)\ndel(A[0])\nprint('After change:', A)"]},{"cell_type":"markdown","id":"1c2f2555-c9e4-44df-a32e-3ec57fd56d4b","metadata":{},"outputs":[],"source":["We can convert a string to a list using split
. For example, the method split
translates every group of characters separated by a space into an element in a list:\n"]},{"cell_type":"code","id":"501be38e-6f35-4de8-a01c-2ca1d1245219","metadata":{},"outputs":[],"source":["# Split the string, default is by space\n\n'hard rock'.split()"]},{"cell_type":"markdown","id":"6170fe6e-d4fd-4f17-bb3d-e066d09b0f1f","metadata":{},"outputs":[],"source":["We can use the split function to separate strings on a specific character which we call a **delimiter**. We pass the character we would like to split on into the argument, which in this case is a comma. The result is a list, and each element corresponds to a set of characters that have been separated by a comma:\n"]},{"cell_type":"code","id":"2c13e8cb-c308-44cc-a071-1a6634f1a02f","metadata":{},"outputs":[],"source":["# Split the string by comma\n\n'A,B,C,D'.split(',')"]},{"cell_type":"markdown","id":"bbb00c9f-ce1f-410e-a704-369940d49517","metadata":{},"outputs":[],"source":["\n","## Copy and Clone List\n"]},{"cell_type":"markdown","id":"c707ec5e-46a7-4dab-a6ed-8742f8af1b01","metadata":{},"outputs":[],"source":["When we set one variable B equal to A, both A and B are referencing the same list in memory:\n"]},{"cell_type":"code","id":"a54df6a9-55ae-4a26-a6df-ac72a8702fcd","metadata":{},"outputs":[],"source":["# Copy (copy by reference) the list A\n\nA = [\"hard rock\", 10, 1.2]\nB = A\nprint('A:', A)\nprint('B:', B)"]},{"cell_type":"markdown","id":"632fd574-c12e-4c8d-88de-b1eebbfe0a2d","metadata":{},"outputs":[],"source":["a_list
, with the following elements 1
, hello
, \\[1,2,3]
and True
.\n"]},{"cell_type":"code","id":"b1ab480b-f2ae-420c-9518-9b5da41624bd","metadata":{},"outputs":[],"source":["# Write your code below and press Shift+Enter to execute\n"]},{"cell_type":"markdown","id":"495fb969-3f3a-4cb3-9c3b-021f7d1f91a3","metadata":{},"outputs":[],"source":["a_list
.\n"]},{"cell_type":"code","id":"72232d4d-5ee5-4c88-bcd5-bbd5597c1aa4","metadata":{},"outputs":[],"source":["# Write your code below and press Shift+Enter to execute\n"]},{"cell_type":"markdown","id":"792488fb-6f11-412e-8206-accdd2a1030e","metadata":{},"outputs":[],"source":["a_list
.\n"]},{"cell_type":"code","id":"3cbcf013-7f7f-4a61-9c76-4db9a3d9d556","metadata":{},"outputs":[],"source":["# Write your code below and press Shift+Enter to execute\n"]},{"cell_type":"markdown","id":"16668448-36f7-462a-8b98-9c9af5a570f5","metadata":{},"outputs":[],"source":["A = \\[1, 'a']
and B = \\[2, 1, 'd']
:\n"]},{"cell_type":"code","id":"65dd86af-ac15-418b-b789-e347e8a6a01a","metadata":{},"outputs":[],"source":["# Write your code below and press Shift+Enter to execute\n"]},{"cell_type":"markdown","id":"f292e7a0-0b05-4c6a-93c1-e12900bbcd54","metadata":{},"outputs":[],"source":["Congratulations, you have completed your first lesson and hands-on lab in Python. \n","