{"id":5153,"date":"2022-03-12T15:36:44","date_gmt":"2022-03-12T14:36:44","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5153"},"modified":"2025-05-05T17:12:17","modified_gmt":"2025-05-05T15:12:17","slug":"lists","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/03\/12\/lists\/","title":{"rendered":"Lists"},"content":{"rendered":"<div class=\"small-12 large-6 columns\">\n<p>1. The <strong>list is a type of data<\/strong> in Python used to <strong>store multiple objects<\/strong>. It is an <strong>ordered and mutable collection<\/strong> of comma-separated items between square brackets, e.g.:<\/p>\n<\/div>\n<p><!--more--><\/p>\n<div class=\"small-12 large-6 columns\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true\">my_list = [1, None, True, \"I am a string\", 256, 0]<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>2. Lists can be <strong>indexed and updated<\/strong>, e.g.:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [1, None, True, 'I am a string', 256, 0]\r\nprint(my_list[3]) # outputs: I am a string\r\nprint(my_list[-1]) # outputs: 0\r\n\r\nmy_list[1] = '?'\r\nprint(my_list) # outputs: [1, '?', True, 'I am a string', 256, 0]\r\n\r\nmy_list.insert(0, \"first\")\r\nmy_list.append(\"last\")\r\nprint(my_list) # outputs: ['first', 1, '?', True, 'I am a string', 256, 0, 'last']<\/pre>\n<p>&nbsp;<\/p>\n<p>3. Lists can be <strong>nested<\/strong>, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true\">my_list = [1, 'a', [\"list\", 64, [0, 1], False]]<\/pre>\n<p>&nbsp;<\/p>\n<p>4. List elements and lists can be <strong>deleted<\/strong>, e.g.:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [1, 2, 3, 4]\r\ndel my_list[2]\r\nprint(my_list) # outputs: [1, 2, 4]\r\n\r\ndel my_list # deletes the whole list<\/pre>\n<p>&nbsp;<\/p>\n<p>5. Lists can be <strong>iterated<\/strong> through using the <code>for<\/code> loop, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [\"white\", \"purple\", \"blue\", \"yellow\", \"green\"]\r\n\r\nfor color in my_list:\r\n  print(color)<\/pre>\n<p>&nbsp;<\/p>\n<p>6. The <code>len()<\/code> function may be used to <strong>check the list&#8217;s length<\/strong>, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [\"white\", \"purple\", \"blue\", \"yellow\", \"green\"]\r\nprint(len(my_list)) # outputs 5\r\n\r\ndel my_list[2]\r\nprint(len(my_list)) # outputs 4<\/pre>\n<p>&nbsp;<\/p>\n<p>7. A typical <strong>function<\/strong> invocation looks as follows: <code>result = function(arg)<\/code>, while a typical <strong>method<\/strong> invocation looks like this:<code>result = data.method(arg)<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>8. You can use the sort() method to sort elements of a list, e.g.:<\/p>\n<pre class=\"lang:default decode:true\">lst = [5, 3, 1, 2, 4]\r\nprint(lst)\r\n\r\nlst.sort()\r\nprint(lst) # outputs: [1, 2, 3, 4, 5]<\/pre>\n<ul>\n<li>Both <code><strong>sort()<\/strong><\/code> and <code><strong>sorted()<\/strong><\/code> sort a list in ascending order.<\/li>\n<li><code><strong>sort()<\/strong><\/code> is a method and <code><strong>sorted()<\/strong><\/code> is a <strong>built-in<\/strong> function.<\/li>\n<li><code><strong>sort()<\/strong><\/code> changes the list in place, while <code><strong>sorted()<\/strong><\/code> creates a new list<\/li>\n<li><code><strong>sort()<\/strong><\/code> is defined only for lists, while <code><strong>sorted()<\/strong><\/code> accepts any iterable (such as strings and tuples)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>9. There is also a list method called reverse(), which you can use to reverse the list, e.g.:<\/p>\n<pre class=\"lang:default decode:true\">lst = [5, 3, 1, 2, 4]\r\nprint(lst)\r\n\r\nlst.reverse()\r\nprint(lst) # outputs: [4, 2, 1, 3, 5]<\/pre>\n<p>or<\/p>\n<pre class=\"lang:default decode:true\">lst = [2, 3, 7, 11, 26]\r\nprint(lst[::-1]) # outputs: [26, 11, 7, 3, 2] \r\n<\/pre>\n<p>In Python, the <strong>slice<\/strong> notation <code><strong>[::-1]<\/strong><\/code> returns the elements of a data collection, such as a list, in <strong>reverse<\/strong> order.<\/p>\n<p>&nbsp;<\/p>\n<p>10. If you have a list <code>list1<\/code>, then the following assignment: <code>list2 = list1<\/code> does not make a copy of the <code>list1<\/code> list, but makes the variables <code>list1<\/code> and <code>list2<\/code> <strong>point to one and the same list in memory<\/strong>. For example:<\/p>\n<pre class=\"lang:default decode:true\">vehicles_one = ['car', 'bicycle', 'motor']\r\nprint(vehicles_one) # outputs: ['car', 'bicycle', 'motor']\r\n\r\nvehicles_two = vehicles_one\r\ndel vehicles_one[0] # deletes 'car'\r\nprint(vehicles_two) # outputs: ['bicycle', 'motor']<\/pre>\n<p>&nbsp;<\/p>\n<p>11. If you want to copy a list or part of the list, you can do it by performing <strong>slicing<\/strong>:<code class=\"codep syntax-color copy\"><\/code><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">colors = ['red', 'green', 'orange']\r\n\r\ncopy_whole_colors = colors[:] # copy the entire list\r\ncopy_part_colors = colors[0:2] # copy part of the list<\/pre>\n<p>&nbsp;<\/p>\n<p>12. You can use <strong>negative indices<\/strong> to perform slices, too. For example:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">sample_list = [\"A\", \"B\", \"C\", \"D\", \"E\"]\r\nnew_list = sample_list[2:-1]\r\nprint(new_list) # outputs: ['C', 'D']<\/pre>\n<p>&nbsp;<\/p>\n<p>13. The <code>start<\/code> and <code>end<\/code> parameters are <strong>optional<\/strong> when performing a slice: <code>list[start:end]<\/code>, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [1, 2, 3, 4, 5]\r\nslice_one = my_list[2: ]\r\nslice_two = my_list[ :2]\r\nslice_three = my_list[-2: ]\r\n\r\nprint(slice_one) # outputs: [3, 4, 5]\r\nprint(slice_two) # outputs: [1, 2]\r\nprint(slice_three) # outputs: [4, 5]<\/pre>\n<p>&nbsp;<\/p>\n<p>14. You can <strong>delete slices<\/strong> using the <code>del<\/code> instruction:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [1, 2, 3, 4, 5]\r\ndel my_list[0:2]\r\nprint(my_list) # outputs: [3, 4, 5]\r\n\r\ndel my_list[:]\r\nprint(my_list) # deletes the list content, outputs: []<\/pre>\n<p>&nbsp;<\/p>\n<p>15. You can test if some items <strong>exist in a list or not<\/strong> using the keywords <code>in<\/code> and <code>not in<\/code>, e.g.:<\/p>\n<\/div>\n<\/div>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [\"A\", \"B\", 1, 2]\r\n\r\nprint(\"A\" in my_list) # outputs: True\r\nprint(\"C\" not in my_list) # outputs: True\r\nprint(2 not in my_list) # outputs: False<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>16. <strong>List comprehension<\/strong> allows you to create new lists from existing ones in a concise and elegant way. The syntax of a list comprehension looks as follows:<\/p>\n<div class=\"small-12 large-6 columns\">\n<pre class=\"lang:default decode:true\">[expression for element in list if conditional]<\/pre>\n<p>which is actually an equivalent of the following code:<\/p>\n<pre class=\"lang:default decode:true\">for element in list:\r\n   if conditional:\r\n      expression<\/pre>\n<p>Let&#8217;s assume that we&#8217;re able to use the selected numbers to represent any chess piece. We can also assume that every row on the chessboard is a list.<\/p>\n<p>Look at the code below:<\/p>\n<pre class=\"lang:default decode:true\">row = []\r\nfor i in range(8):\r\n   row.append(WHITE_PAWN)<\/pre>\n<p>It builds a list containing eight elements representing the second row of the chessboard &#8211; the one filled with pawns (assume that WHITE_PAWN is a predefined symbol representing a white pawn).<\/p>\n<p>The same effect may be achieved by means of a list comprehension, the special syntax used by Python in order to fill massive lists.<\/p>\n<pre class=\"lang:default decode:true\">row = [WHITE_PAWN for i in range(8)]<\/pre>\n<p>The part of the code placed inside the brackets specifies:<\/p>\n<p>the data to be used to fill the list (WHITE_PAWN)<br \/>\nthe clause specifying how many times the data occurs inside the list (for i in range(8))<\/p>\n<p>Here&#8217;s an example of a list comprehension \u2012 the code creates a five-element list filled with the first five natural numbers raised to the power of 3:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">cubed = [num ** 3 for num in range(5)]\r\nprint(cubed) # outputs: [0, 1, 8, 27, 64]<\/pre>\n<p>&nbsp;<\/p>\n<p>17. Swapping the lists&#8217;s elements.<\/p>\n<p>you can easily <strong>swap<\/strong> the list&#8217;s elements to <strong>reverse their order<\/strong>:<code class=\"codep syntax-color\"><\/code><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">my_list = [10, 1, 8, 3, 5]\r\nmy_list[0], my_list[4] = my_list[4], my_list[0]\r\nmy_list[1], my_list[3] = my_list[3], my_list[1]\r\n\r\nprint(my_list)<\/pre>\n<p>Run the snippet. Its output should look like this:<\/p>\n<\/div>\n<\/div>\n<p><code class=\"codep \">[5, 3, 8, 1, 10]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>Will it still be acceptable with a list containing 100 elements? No, it won&#8217;t. You use the <code>for<\/code> loop to do the same thing automatically, irrespective of the list&#8217;s length.<\/p>\n<hr \/>\n<p>This is how we&#8217;ve done it:<\/p>\n<pre class=\"lang:default decode:true \">my_list = [10, 1, 8, 3, 5] \r\nlength = len(my_list) \r\nfor i in range(length \/\/ 2): \r\n   my_list[i], my_list[length - i - 1] = my_list[length - i - 1], my_list[i] \r\nprint(my_list)<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<p>&nbsp;<\/p>\n<div class=\"ace_line\">Let us show you some other <strong>list comprehension examples<\/strong>:<\/div>\n<\/div>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<p><em>Example #1:<\/em><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true \">squares = [x ** 2 for x in range(10)]<\/pre>\n<p>The snippet produces a ten-element list filled with squares of ten integer numbers starting from zero (0, 1, 4, 9, 16, 25, 36, 49, 64, 81)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><em>Example #2:<\/em><\/p>\n<pre class=\"lang:default decode:true \">twos = [2 ** i for i in range(8)]<\/pre>\n<p>The snippet creates an eight-element array containing the first eight powers of two (1, 2, 4, 8, 16, 32, 64, 128)<\/p>\n<p><em>Example #3:<\/em><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<div class=\"ace_line\">\n<pre class=\"lang:default decode:true\">odds = [x for x in squares if x % 2 != 0 ]<\/pre>\n<p>The snippet makes a list with only the odd elements of the <code>squares<\/code> list.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div class=\"ace_static_highlight\">18. You can use <strong>nested lists<\/strong> in Python to create <strong>matrices<\/strong> (i.e., two-dimensional lists). For example:<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"lightbox\" src=\"https:\/\/edube.org\/uploads\/media\/default\/0001\/01\/9c20da970fc3b140efa05e507050d181482477ab.png\" alt=\"Table - a two-dimensional array\" width=\"65%\" \/><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\"># A four-column\/four-row table \u2012 a two dimensional array (4x4)\r\ntable = [[\":(\", \":)\", \":(\", \":)\"],\r\n         [\":)\", \":(\", \":)\", \":)\"],\r\n         [\":(\", \":)\", \":)\", \":(\"],\r\n         [\":)\", \":)\", \":)\", \":(\"]]\r\n\r\nprint(table)\r\nprint(table[0][0]) # outputs: ':('\r\nprint(table[0][3]) # outputs: ':)'<\/pre>\n<p>&nbsp;<\/p>\n<p>18. You can nest as many lists in lists as you want, thereby creating n-dimensional lists, e.g., three-, four- or even sixty-four-dimensional arrays. For example:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<p><img decoding=\"async\" class=\"lightbox\" src=\"https:\/\/edube.org\/uploads\/media\/default\/0001\/01\/c2db5fba6dddfb6606c305c21a0445c516c99a7f.png\" alt=\"Cube - a three-dimensional array\" width=\"65%\" \/><\/p>\n<p><code class=\"codep syntax-color\"><\/code><\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\"># Cube - a three-dimensional array (3x3x3)\r\n\r\ncube = [[[':(', 'x', 'x'],\r\n[':)', 'x', 'x'],\r\n[':(', 'x', 'x']],\r\n\r\n[[':)', 'x', 'x'],\r\n[':(', 'x', 'x'],\r\n[':)', 'x', 'x']],\r\n\r\n[[':(', 'x', 'x'],\r\n[':)', 'x', 'x'],\r\n[':)', 'x', 'x']]]\r\n\r\nprint(cube)\r\nprint(cube[0][0][0]) # outputs: ':('\r\nprint(cube[2][2][0]) # outputs: ':)'<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"ace_line\"><strong>Exercise 1<\/strong><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"small-12 large-6 columns\">\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">lst = [1, 2, 3, 4, 5]\r\nlst.insert(1, 6)\r\ndel lst[0]\r\nlst.append(1)\r\n\r\nprint(lst)\r\n\r\n# Output: [6, 2, 3, 4, 5, 1]<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p><strong>Exercise 2<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">lst = [1, 2, 3, 4, 5]\r\nlst_2 = []\r\nadd = 0\r\n\r\nfor number in lst:\r\n    add += number\r\n    lst_2.append(add)\r\n\r\nprint(lst_2)\r\n\r\n# Output: [1, 3, 6, 10, 15]\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 3<\/strong><\/p>\n<p>What happens when you run the following snippet?<code class=\"codep \"><\/code><\/p>\n<pre class=\"lang:default decode:true\">lst = []\r\ndel lst\r\nprint(lst)\r\n\r\n# Output: NameError: name 'lst' is not defined<code class=\"codep \"><\/code><\/pre>\n<div align=\"right\"><\/div>\n<p><strong>Exercise 4<\/strong><\/p>\n<p>What is the output of the following snippet?<code class=\"codep \"><\/code><\/p>\n<pre class=\"lang:default decode:true\">lst = [1, [2, 3], 4]\r\nprint(lst[1])\r\nprint(len(lst))\r\n\r\n# Output:\r\n# [2, 3]\r\n# 3<code class=\"codep \"><\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 5<br \/>\n<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">list_1 = [\"A\", \"B\", \"C\"]\r\nlist_2 = list_1\r\nlist_3 = list_2\r\n\r\ndel list_1[0]\r\ndel list_2[0]\r\nprint(list_3)\r\n\r\n# Output: ['C']<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 6<br \/>\n<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">list_1 = [\"A\", \"B\", \"C\"]\r\nlist_2 = list_1\r\nlist_3 = list_2\r\n\r\ndel list_1[0]\r\ndel list_2\r\nprint(list_3)\r\n\r\n['B', 'C']<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 7<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true\">list_1 = [\"A\", \"B\", \"C\"]\r\nlist_2 = list_1\r\nlist_3 = list_2\r\n\r\ndel list_1[0]\r\ndel list_2[:]\r\n\r\nprint(list_3)\r\n\r\n[]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 8<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">list_1 = [\"A\", \"B\", \"C\"]\r\nlist_2 = list_1[:]\r\nlist_3 = list_2[:]\r\n\r\ndel list_1[0]\r\ndel list_2[0]\r\n\r\nprint(list_3)\r\n\r\n['A', 'B', 'C']<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 9<\/strong><\/p>\n<p>Insert in or not in instead of ??? so that the code outputs the expected result.<\/p>\n<pre class=\"lang:default decode:true \">my_list = [1, 2, \"in\", True, \"ABC\"]\r\n\r\nprint(1 ??? my_list) # outputs True\r\nprint(\"A\" ??? my_list) # outputs True\r\nprint(3 ??? my_list) # outputs True\r\nprint(False ??? my_list) # outputs False\r\n\r\nmy_list = [1, 2, \"in\", True, \"ABC\"]\r\n\r\nprint(1 in my_list) # outputs True\r\nprint(\"A\" not in my_list) # outputs True\r\nprint(3 not in my_list) # outputs True\r\nprint(False in my_list) # outputs False<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 10<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">lst = [\"D\", \"F\", \"A\", \"Z\"]\r\nlst.sort()\r\n\r\nprint(lst)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol\"><strong><code class=\"codep \">['A', 'D', 'F', 'Z']<\/code><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 11<br \/>\n<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">a = 3\r\nb = 1\r\nc = 2\r\n\r\nlst = [a, c, b]\r\nlst.sort()\r\n\r\nprint(lst)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol2\"><strong><code class=\"codep \">[1, 2, 3]<\/code><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exercise 12<\/strong><\/p>\n<p>What is the output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">a = \"A\"\r\nb = \"B\"\r\nc = \"C\"\r\nd = \" \"\r\n\r\nlst = [a, b, c, d]\r\nlst.reverse()\r\n\r\nprint(lst)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol3\"><strong><code class=\"codep \">[' ', 'C', 'B', 'A']<\/code><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 13<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true\">d1 = {1: 'one', 2: 'two', 3: 'three'}\r\n\r\nd2 = d1\r\ndel d1\r\n\r\nprint(d2)<\/pre>\n<p>&nbsp;<\/p>\n<p><code>{1: 'one', 2: 'two', 3: 'three'}<\/code><\/p>\n<ul>\n<li>After <strong>line 3<\/strong> of the code, <code><strong>d1<\/strong><\/code> and <code><strong>d2<\/strong><\/code> refer to the same dictionary. In other words, <code><strong>d1<\/strong><\/code> and <code><strong>d2<\/strong><\/code> are two variables that point to the same dictionary in memory and not two separate copies of it.<\/li>\n<\/ul>\n<ul>\n<li>The instruction in <strong>line 4<\/strong> removes the reference <code><strong>d1<\/strong><\/code>, but the variable <code><strong>d2<\/strong><\/code> will continue to point to the starting dictionary. Therefore, in <strong>line 6<\/strong>, the <code><strong>print()<\/strong><\/code>function will print the entire dictionary.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Ex. 14<\/p>\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true\">lst = [3, 2, 0, 4]\r\n\r\nfor n in lst:\r\n  if n != 2 and n != 4:\r\n    print(lst[n], end=\" \")<\/pre>\n<p>&nbsp;<\/p>\n<p><code>4 3<\/code><\/p>\n<ul>\n<li>The program executes the <code><strong>for<\/strong><\/code> loop <code><strong>4<\/strong><\/code> times, according to the four elements of the <code><strong>lst<\/strong><\/code> list. Due to the <code><strong>if<\/strong><\/code> condition, the program will execute the <code><strong>print<\/strong><\/code> statement only for the values <code><strong>3<\/strong><\/code> and <code><strong>0<\/strong><\/code> in the list.<\/li>\n<li>In the <code><strong>print<\/strong><\/code> statement, <code><strong>n<\/strong><\/code> is used as an index to get the corresponding value in <code><strong>lst<\/strong><\/code>.<\/li>\n<li>Due to the presence of the <code><strong>end=\" \"<\/strong><\/code> parameter the numbers are printed on a single line separated by a space.<\/li>\n<li>Here&#8217;s how the for loop works:\n<ul>\n<li><strong>First iteration<\/strong>: <code><strong>n = 3<\/strong><\/code>.<br \/>\nThe <code><strong>if<\/strong><\/code> condition is <code><strong>True<\/strong><\/code> because <code><strong>3<\/strong><\/code> is different from <code><strong>2<\/strong><\/code> and <code><strong>4<\/strong><\/code>.<br \/>\nThe <code><strong>print<\/strong><\/code> statement outputs <code><strong>4<\/strong><\/code> to the screen:<code><strong>print(lst[3], end=\" \")<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>print(4, end=\" \")<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>4<\/strong><\/code>.(The list index starts with <code><strong>0<\/strong><\/code>).<\/li>\n<li><strong>Second iteration<\/strong>: <code><strong>n = 2<\/strong><\/code>.<br \/>\nThe <code><strong>if<\/strong><\/code> condition is <code><strong>False<\/strong><\/code>.<\/li>\n<li><strong>Third iteration<\/strong>: <code><strong>n = 3<\/strong><\/code>.<br \/>\nThe <code><strong>if<\/strong><\/code> condition is <code><strong>True<\/strong><\/code> because <code><strong>0<\/strong><\/code> is different from <code><strong>2<\/strong><\/code> and <code><strong>4<\/strong><\/code>.<br \/>\nThe <code><strong>print<\/strong><\/code> statement outputs <code><strong>3<\/strong><\/code> to the screen:<code><strong>print(lst[0], end=\" \")<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>print(3, end=\" \")<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>3<\/strong><\/code>.<\/li>\n<li><strong>Fourth iteration<\/strong>: <code><strong>n = 4<\/strong><\/code>.<br \/>\nThe <code><strong>if<\/strong><\/code> condition is <code><strong>False<\/strong><\/code> and the program ends.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Ex.15<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"lang:default decode:true\">lst = ['a', 'C', \" \", 'b', 'Z']\r\nlst.sort()\r\nprint(lst)<\/pre>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>['a', 'b', 'C', 'Z', ' ']<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[' ', 'a', 'b', 'C', 'Z']<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[' ', 'C', 'Z', 'a', 'b']<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>['C', 'Z', 'a', 'b', ' ']<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div><\/div>\n<div>\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>[' ', 'C', 'Z', 'a', 'b']<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>sort()<\/strong><\/code> method changes the order of the elements of the original list. It sorts a list in <strong>ascending<\/strong> order by default. Alphabetical sorting is based on the <strong>ASCII <\/strong>value of the elements. The space (<code><strong>\" \"<\/strong><\/code>) has the lowest <strong>ASCII<\/strong> value and is listed first.<\/li>\n<li><strong>Uppercase<\/strong> letters have a lower <strong>ASCII<\/strong> value than <strong>lowercase<\/strong> letters and are listed first.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 16<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true\">lst = [1, 2, 3, 4, 5]\r\n\u00a0\r\nlst.append([6, 7])\r\nlst.extend(['a', 'b'])\r\nlst.pop()\r\n\u00a0\r\nprint(lst)<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[1, 2, 3, 4, 5, ['a', 'b']]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[1, 2, 3, 4, 5, [6, 7]]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[1, 2, 3, 4, 5, [6, 7], 'a']<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[1, 2, 3, 4, 5, 6, 7]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>[1, 2, 3, 4, 5, [6, 7], 'a']<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>append(element)<\/strong><\/code> method adds an element, of any type, to the end of the list. <code><strong>append()<\/strong><\/code> adds the element as a single item. In our code, the element is the list <code><strong>[6, 7]<\/strong><\/code>.\u00a0 Thus, we have:<code><strong>lst.append([6, 7])<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>[1, 2, 3, 4, 5, [6, 7]]<\/strong><\/code>.<\/li>\n<\/ul>\n<ul>\n<li>The <code><strong>extend()<\/strong><\/code> method also adds elements to the end of a list, but the argument must be an iterable object (list, tuple, range, &#8230;). Also, <code><strong>extend()<\/strong><\/code> appends each item generated by the iterable as a single entry to the end of the list:<code><strong>lst.extend(['a', 'b'])<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>[1, 2, 3, 4, 5, [6, 7], 'a', 'b']<\/strong><\/code>.<\/li>\n<\/ul>\n<ul>\n<li>The <code><strong>pop(position)<\/strong><\/code> method removes the element at the specified position. The default is <code><strong>-1<\/strong><\/code>, so <code><strong>pop()<\/strong><\/code> removes the last item in a list:<code><strong>lst.pop()<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>lst.pop(-1)<\/strong><\/code> <strong>-&gt;<\/strong> <code><strong>[1, 2, 3, 4, 5, [6, 7], 'a']<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex.17<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">str = \"\"\"\r\nPython\"\"\"\r\n\r\nprint(len(str))<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>7<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The code is correct and does not generate any errors. In Python, <strong>multiline<\/strong> strings enclosed in <strong>triple quote<\/strong>s are allowed.<\/li>\n<\/ul>\n<ul>\n<li>The <code><strong>len()<\/strong><\/code> function counts <code><strong>7<\/strong><\/code> characters from <code><strong>str<\/strong><\/code> because it also adds the hidden <strong>newline<\/strong> character <code><strong>\\n<\/strong><\/code> to the total.<\/li>\n<\/ul>\n<ul>\n<li>In other words, the contents of the string <code><strong>str<\/strong><\/code> is equivalent to <code><strong>\"\"\"\\nPython\"\"\"<\/strong><\/code>, where the <strong>escape<\/strong> character <code><strong>\\n<\/strong><\/code> is counted as <code><strong>1<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 18<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">print(list(range(-3)))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[0, -1, -2]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>A <code>ValueError<\/code> exception<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[-1, -2, -3] <\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div><\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>[]<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The syntax of the <code><strong>range()<\/strong><\/code> function is <code><strong>range(start, stop, step)<\/strong><\/code>.<\/li>\n<li><code><strong>range()<\/strong><\/code> returns a sequence of numbers containing all values from <strong>start<\/strong> to <strong>stop<\/strong> (excluded), where <strong>step<\/strong> specifies the <strong>increment<\/strong>.<\/li>\n<li>The start, stop and step values must be <strong>integers<\/strong>.<\/li>\n<li>start and step are <strong>optional<\/strong>.<\/li>\n<li><code><strong>0<\/strong><\/code> and <code><strong>1<\/strong><\/code> are the <strong>default<\/strong> values of start and step parameters, respectively.<\/li>\n<\/ul>\n<ul>\n<li>Based on the above, <code><strong>range(-3)<\/strong><\/code> is equal to <code><strong>range(0, -3, 1)<\/strong><\/code>.<\/li>\n<li>When the <strong>start<\/strong> parameter (<code><strong>0<\/strong><\/code>) is greater than the <strong>stop<\/strong> parameter (<code><strong>-3<\/strong><\/code>), and the <strong>step<\/strong> parameter (<code><strong>1<\/strong><\/code>) is positive, the <code><strong>range()<\/strong><\/code> function cannot generate any values.<\/li>\n<li>Therefore, the <code><strong>list()<\/strong><\/code> function returns an <strong>empty<\/strong> list.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 19<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<pre class=\"lang:default decode:true \">def fun():\r\n  lst[0] = 8\r\n\u00a0\r\nlst = [3, 7, 1, 5, 9]\r\nfun()\r\n\u00a0\r\nprint(lst)<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[8, 7, 1, 5, 9]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[0, 8, 7, 1, 5, 9]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>[3, 7, 1, 5, 9]<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>A <code>SyntaxError<\/code> exception<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>[8, 7, 1, 5, 9]<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul>\n<li>The <code><strong>lst<\/strong><\/code> list is defined in the <strong>global<\/strong> scope and is accessible from anywhere in the code, even within the <code><strong>fun()<\/strong><\/code> function. Therefore, the function changes <code><strong>lst<\/strong><\/code> by replacing the <strong>first<\/strong> element <code><strong>3<\/strong><\/code> (index <code><strong>0<\/strong><\/code>) with the new value <code><strong>8<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 20<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">l3d = [[['A','B','C'],\r\n        ['X','Y','Z'],\r\n        ['J','K','L']],\r\n\r\n       [[100,200,300],\r\n        [400,500,600],\r\n        [700,800,900]],\r\n\r\n       [['@','$','\u00a7'],\r\n        ['?','#','&amp;'],\r\n        ['*','!','=']]]\r\n\r\nprint(l3d[2][1])\r\nprint(l3d[2][1][1])<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><strong><code><span class=\"pun\">[<\/span><span class=\"str\">'?'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'#'<\/span><span class=\"pun\">,<\/span> <span class=\"str\">'&amp;'<\/span><span class=\"pun\">]<\/span><\/code><\/strong><\/p>\n<p><strong><code><span class=\"com\">#<\/span><\/code><\/strong><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>This is an example of a 3-dimensional list. It is composed of a series of nested lists that create a 3x3x3 &#8216;cube&#8217; with 27 elements in total.<\/li>\n<\/ul>\n<ul>\n<li>To locate an element in the <code>3d<\/code> list, three &#8216;coordinates&#8217; are needed: <code>l3d[x][y][z]<\/code>.The <code>x<\/code> coordinate identifies one of the main 3&#215;3 matrixes that make up the list.\n<p>The <code>y<\/code> coordinate locates one of the lists that form the rows of the matrix.<\/p>\n<p>The last coordinate, <code>z<\/code>, specifies the single element of a row.<\/li>\n<\/ul>\n<ul>\n<li>Here&#8217;s how the code works.<code>ls[2][1]<\/code> &#8211; It locates the row of index 1 in the matrix of index 2.\n<p>Step by step:<\/p>\n<p>Step 1 &#8211;\u00a0 <code>print(l3d[2][1])<\/code><\/p>\n<p>Step 2 &#8211; <code>print([['@','$','\u00a7'], ['?','#','&amp;'], ['*','!','=']][1])<\/code><\/p>\n<p>Output &#8211;<code>['?','#','&amp;']<\/code><\/p>\n<p><code>ls[2][1][1]<\/code> &#8211; It locates the element of index 1, in row 1, of matrix 2.<\/p>\n<p>Step by step:<\/p>\n<p>Step 1 &#8211; <code>print(l3d[2][1][1])<\/code><\/p>\n<p>Step 2 &#8211; <code>print([['@','$','\u00a7'], ['?','#','&amp;'], ['*','!','=']][1][1])<\/code><\/p>\n<p>Step 3 &#8211; <code>print(['?','#','&amp;'][1])<\/code><\/p>\n<p>Output &#8211; <code>#<\/code><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div align=\"right\"><\/div>\n<div class=\"small-12 large-6 columns\">\n<h2><strong>Scenario<\/strong><\/h2>\n<p>Imagine a list &#8211; not very long, not very complicated, just a simple list containing some integer numbers. Some of these numbers may be repeated, and this is the clue. We don&#8217;t want any repetitions. We want them to be removed.<\/p>\n<p>Your task is to write a program which removes all the number repetitions from the list. The goal is to have a list in which all the numbers appear not more than once.<\/p>\n<p>Note: assume that the source list is hard-coded inside the code &#8211; you don&#8217;t have to enter it from the keyboard. Of course, you can improve the code and add a part that can carry out a conversation with the user and obtain all the data from her\/him.<\/p>\n<p>Hint: we encourage you to create a new list as a temporary work area &#8211; you don&#8217;t need to update the list in situ.<\/p>\n<\/div>\n<pre class=\"lang:default decode:true\">my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]\r\nnew_list = []\r\n\r\nfor number in my_list:\r\n    if number not in new_list:\r\n        new_list.append(number)\r\n    \r\nprint(\"The list with unique elements only:\")\r\nprint(new_list)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. The list is a type of data in Python used to store multiple objects. It is an ordered and mutable collection of comma-separated items between square brackets, e.g.:<\/p>\n","protected":false},"author":2,"featured_media":5928,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5153"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5153"}],"version-history":[{"count":36,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5153\/revisions"}],"predecessor-version":[{"id":5929,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5153\/revisions\/5929"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/5928"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5153"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}