Created
January 4, 2021 17:41
-
-
Save emilfh/e64d753f71bf97f2f8ef903715e2853f to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a href=\"http://cocl.us/topNotebooksPython101Coursera\"><img src = \"https://ibm.box.com/shared/static/yfe6h4az47ktg2mm9h05wby2n7e8kei3.png\" width = 750, align = \"center\"></a>\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a href=\"https://www.bigdatauniversity.com\"><img src = \"https://ibm.box.com/shared/static/ugcqz6ohbvff804xp84y4kqnvvk3bq1g.png\" width = 300, align = \"center\"></a>\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"<h1 align=center><font size = 5>LISTS IN PYTHON</font></h1>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"## Table of Contents\n", | |
"\n", | |
"\n", | |
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n", | |
"<li><a href=\"#ref0\">About the Dataset</a></li>\n", | |
"<li><a href=\"#ref1\">Lists</a></li>\n", | |
"<li><a href=\"#ref2\">Quiz</a></li>\n", | |
"<br>\n", | |
"<p></p>\n", | |
"Estimated Time Needed: <strong>15 min</strong>\n", | |
"</div>\n", | |
"\n", | |
"<hr>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<a id=\"ref0\"></a>\n", | |
"<center><h2>About the Dataset</h2></center>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Imagine you received many music recommendations from your friends and compiled all of the recommendations into a table, with specific information about each movie.\n", | |
"\n", | |
"The table has one row for each album and several columns:\n", | |
"\n", | |
"- **artist** - Name of the artist\n", | |
"- **album** - Name of the album\n", | |
"- **released_year** - Year the album was released\n", | |
"- **length_min_sec** - Length of the album (hours,minutes,seconds)\n", | |
"- **genre** - Genre of the album\n", | |
"- **music_recording_sales_millions** - Music recording sales (millions in USD) on [SONG://DATABASE](http://www.song-database.com/)\n", | |
"- **claimed_sales_millions** - Album's claimed sales (millions in USD) on [SONG://DATABASE](http://www.song-database.com/)\n", | |
"- **date_released** - Date on which the album was released\n", | |
"- **soundtrack** - Indicates if the album is the movie soundtrack (Y) or (N)\n", | |
"- **rating_of_friends** - Indicates the rating from your friends from 1 to 10\n", | |
"<br>\n", | |
"<br>\n", | |
"\n", | |
"The dataset can be seen below:\n", | |
"\n", | |
"<font size=\"1\">\n", | |
"<table font-size:xx-small style=\"width:25%\">\n", | |
" <tr>\n", | |
" <th>Artist</th>\n", | |
" <th>Album</th> \n", | |
" <th>Released</th>\n", | |
" <th>Length</th>\n", | |
" <th>Genre</th> \n", | |
" <th>Music recording sales (millions)</th>\n", | |
" <th>Claimed sales (millions)</th>\n", | |
" <th>Released</th>\n", | |
" <th>Soundtrack</th>\n", | |
" <th>Rating (friends)</th>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Michael Jackson</td>\n", | |
" <td>Thriller</td> \n", | |
" <td>1982</td>\n", | |
" <td>00:42:19</td>\n", | |
" <td>Pop, rock, R&B</td>\n", | |
" <td>46</td>\n", | |
" <td>65</td>\n", | |
" <td>30-Nov-82</td>\n", | |
" <td></td>\n", | |
" <td>10.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>AC/DC</td>\n", | |
" <td>Back in Black</td> \n", | |
" <td>1980</td>\n", | |
" <td>00:42:11</td>\n", | |
" <td>Hard rock</td>\n", | |
" <td>26.1</td>\n", | |
" <td>50</td>\n", | |
" <td>25-Jul-80</td>\n", | |
" <td></td>\n", | |
" <td>8.5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Pink Floyd</td>\n", | |
" <td>The Dark Side of the Moon</td> \n", | |
" <td>1973</td>\n", | |
" <td>00:42:49</td>\n", | |
" <td>Progressive rock</td>\n", | |
" <td>24.2</td>\n", | |
" <td>45</td>\n", | |
" <td>01-Mar-73</td>\n", | |
" <td></td>\n", | |
" <td>9.5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Whitney Houston</td>\n", | |
" <td>The Bodyguard</td> \n", | |
" <td>1992</td>\n", | |
" <td>00:57:44</td>\n", | |
" <td>Soundtrack/R&B, soul, pop</td>\n", | |
" <td>26.1</td>\n", | |
" <td>50</td>\n", | |
" <td>25-Jul-80</td>\n", | |
" <td>Y</td>\n", | |
" <td>7.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Meat Loaf</td>\n", | |
" <td>Bat Out of Hell</td> \n", | |
" <td>1977</td>\n", | |
" <td>00:46:33</td>\n", | |
" <td>Hard rock, progressive rock</td>\n", | |
" <td>20.6</td>\n", | |
" <td>43</td>\n", | |
" <td>21-Oct-77</td>\n", | |
" <td></td>\n", | |
" <td>7.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Eagles</td>\n", | |
" <td>Their Greatest Hits (1971-1975)</td> \n", | |
" <td>1976</td>\n", | |
" <td>00:43:08</td>\n", | |
" <td>Rock, soft rock, folk rock</td>\n", | |
" <td>32.2</td>\n", | |
" <td>42</td>\n", | |
" <td>17-Feb-76</td>\n", | |
" <td></td>\n", | |
" <td>9.5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Bee Gees</td>\n", | |
" <td>Saturday Night Fever</td> \n", | |
" <td>1977</td>\n", | |
" <td>1:15:54</td>\n", | |
" <td>Disco</td>\n", | |
" <td>20.6</td>\n", | |
" <td>40</td>\n", | |
" <td>15-Nov-77</td>\n", | |
" <td>Y</td>\n", | |
" <td>9.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <td>Fleetwood Mac</td>\n", | |
" <td>Rumours</td> \n", | |
" <td>1977</td>\n", | |
" <td>00:40:01</td>\n", | |
" <td>Soft rock</td>\n", | |
" <td>27.9</td>\n", | |
" <td>40</td>\n", | |
" <td>04-Feb-77</td>\n", | |
" <td></td>\n", | |
" <td>9.5</td>\n", | |
" </tr>\n", | |
"</table></font>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<hr>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<a id=\"ref1\"></a>\n", | |
"<center><h2>Lists</h2></center>\n", | |
"\n", | |
"We are going to take a look at lists in Python. A list is a sequenced collection of different objects such as integers, strings, and other lists as well. The address of each element within a list is called an 'index'. An index is used to access and refer to items within a list.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<a ><img src = \"https://ibm.box.com/shared/static/eln445fv5nzv3wlm4u8dnfhbrcrv0hff.png\" width = 1000, align = \"center\"></a>\n", | |
" <h4 align=center> Representation of a list \n", | |
" </h4> \n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" To create a list, type the list within square brackets **[ ]**, with your content inside the parenthesis and separated by commas. Let’s try it!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.1, 1982]" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L=[\"Michael Jackson\" , 10.1,1982]\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can use negative and regular indexing with a list :" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a ><img src = \"https://ibm.box.com/shared/static/a7ac9lnvmcaz29n86ffez4as27fl3n9m.png\" width = 1000, align = \"center\"></a>\n", | |
" <h4 align=center> Representation of a list \n", | |
" </h4> \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"the same element using negative and positive indexing:\n", | |
" Postive: Michael Jackson \n", | |
" Negative: Michael Jackson\n", | |
"the same element using negative and positive indexing:\n", | |
" Postive: 10.1 \n", | |
" Negative: 10.1\n", | |
"the same element using negative and positive indexing:\n", | |
" Postive: 1982 \n", | |
" Negative: 1982\n" | |
] | |
} | |
], | |
"source": [ | |
"print('the same element using negative and positive indexing:\\n Postive:',L[0],\n", | |
"'\\n Negative:' , L[-3] )\n", | |
"print('the same element using negative and positive indexing:\\n Postive:',L[1],\n", | |
"'\\n Negative:' , L[-2] )\n", | |
"print('the same element using negative and positive indexing:\\n Postive:',L[2],\n", | |
"'\\n Negative:' , L[-1] )" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest tuples and other data structures. The same indexing conventions apply for nesting: \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.1, 1982, [1, 2], ('A', 1)]" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[ \"Michael Jackson\", 10.1,1982,[1,2],(\"A\",1) ]\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" We can also perform slicing in lists. For example, if we want the last two elements, we use the following command:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.1, 1982, 'MJ', 1]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L=[ \"Michael Jackson\", 10.1,1982,\"MJ\",1]\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"<a ><img src = \"https://ibm.box.com/shared/static/pt3pfp1sg5okwuwwpy0dnj8e94fl2mwy.png\" width = 1000, align = \"center\"></a>\n", | |
" <h4 align=center> Representation of a list \n", | |
" </h4> " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['MJ', 1]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L[3:5]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can use the method \"extend\" to add new elements to the list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, 'pop', 10]" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L=[ \"Michael Jackson\", 10.2]\n", | |
"L.extend(['pop',10])\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Another similar method is 'appended'. If we apply 'appended' instead of 'extended', we add one element to the list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, ['pop', 10]]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L=[ \"Michael Jackson\", 10.2]\n", | |
"L.append(['pop',10])\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"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:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, 'pop', 10]" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L=[ \"Michael Jackson\", 10.2]\n", | |
"L.extend(['pop',10])\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If we append the list **['a','b']** we have one new element consisting of a nested list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, 'pop', 10, ['a', 'b']]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"L.append(['a','b'])\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"As lists are mutable, we can change them. For example, we can change the first element as follows:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before change: ['disco', 10, 1.2]\n", | |
"After change: ['hard rock', 10, 1.2]\n" | |
] | |
} | |
], | |
"source": [ | |
"A=[\"disco\",10,1.2]\n", | |
"print('Before change:', A)\n", | |
"A[0]='hard rock'\n", | |
"print('After change:', A)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" We can also delete an element of a list using the **del** command:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before change: ['hard rock', 10, 1.2]\n", | |
"After change: [10, 1.2]\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Before change:', A)\n", | |
"del(A[0])\n", | |
"print('After change:', A)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"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", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['hard', 'rock']" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'hard rock'.split()\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can use the split function to separate strings on a specific character. 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: " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['A', 'B', 'C', 'D']" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'A,B,C,D'.split(',')\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" When we set one variable **B** equal to **A**; both **A** and **B** are referencing the same list in memory :" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A: ['hard rock', 10, 1.2]\n", | |
"B: ['hard rock', 10, 1.2]\n" | |
] | |
} | |
], | |
"source": [ | |
"A=[\"hard rock\",10,1.2]\n", | |
"B=A\n", | |
"print('A:',A)\n", | |
"print('B:',B)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a ><img src = 'https://ibm.box.com/shared/static/7g2u8hqqb4birdwn7m9uir4s9wfj8mko.png' width = 1000, align = \"center\"></a>\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" Initially, the value of the first element in ** B** is set as hard rock. If we change the first element in **A** to 'banana', we get an unexpected side effect. As **A** and **B ** are referencing the same list, if we change list **A**, then list **B** also changes. If we check the first element of **B** we get banana instead of hard rock:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"B[0]: hard rock\n", | |
"B[0]: banana\n" | |
] | |
} | |
], | |
"source": [ | |
"print('B[0]:',B[0])\n", | |
"A[0]=\"banana\"\n", | |
"print('B[0]:',B[0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This is demonstrated in the following figure: " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a ><img src = https://ibm.box.com/shared/static/thdu6y5pzh99qpun4tu2fjvj86st0hbu.gif width = 1000, align = \"center\"></a>\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can clone list **A** by using the following syntax:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['hard rock', 10, 1.2]" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"B=A[:]\n", | |
"B" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" Variable **B** references a new copy or clone of the original list; this is demonstrated in the following figure:" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
" <a ><img src = https://ibm.box.com/shared/static/gwx86gaoeizqjvx7xj96cb8i9hn684ei.gif width = 1000, align = \"center\"></a>\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now if you change **A**, **B** will not change: \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"B[0]: hard rock\n", | |
"B[0]: hard rock\n" | |
] | |
} | |
], | |
"source": [ | |
"print('B[0]:',B[0])\n", | |
"A[0]=\"banana\"\n", | |
"print('B[0]:',B[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a id=\"ref2\"></a>\n", | |
"<center><h2>Quiz</h2></center>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Create a list 'a_list' , with the following elements 1, “hello”, [1,2,3 ] and True. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 'hello', [1, 2, 3], True]" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a_list=[1,\"hello\",[1,2,3],True]\n", | |
"a_list" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <div align=\"right\">\n", | |
"<a href=\"#q1\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n", | |
"</div>\n", | |
"<div id=\"q1\" class=\"collapse\">\n", | |
"```\n", | |
"a_list=[1, 'hello', [1,2,3 ] , True]\n", | |
"a_list\n", | |
"\n", | |
"```\n", | |
"</div>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Find the value stored at index 1 of 'a_list'." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'hello'" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a_list[1]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <div align=\"right\">\n", | |
"<a href=\"#q2\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n", | |
"</div>\n", | |
"<div id=\"q2\" class=\"collapse\">\n", | |
"```\n", | |
"a_list[1]\n", | |
"\n", | |
"\n", | |
"```\n", | |
"</div>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Retrieve the elements stored at index 1 and 2 of 'a_list'." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['hello', [1, 2, 3]]" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a_list[1:3]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <div align=\"right\">\n", | |
"<a href=\"#q3\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n", | |
"</div>\n", | |
"<div id=\"q3\" class=\"collapse\">\n", | |
"```\n", | |
"a_list[1:3]\n", | |
"\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### 4) Concatenate the following lists A=[1,'a'] abd B=[2,1,'d']:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 'a', 2, 1, 'd']" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"A=[1,'a']\n", | |
"B=[2,1,'d']\n", | |
"A+B" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <div align=\"right\">\n", | |
"<a href=\"#q4\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n", | |
"</div>\n", | |
"<div id=\"q4\" class=\"collapse\">\n", | |
"```\n", | |
"A=[1,'a'] \n", | |
"B=[2,1,'d']\n", | |
"A+B\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <a href=\"http://cocl.us/bottemNotebooksPython101Coursera\"><img src = \"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width = 750, align = \"center\"></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# About the Authors: \n", | |
"\n", | |
" [Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact \n", | |
" ]human cognition. Joseph has been working for IBM since he completed his PhD.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" <hr>\n", | |
"Copyright © 2017 [cognitiveclass.ai](cognitiveclass.ai?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
}, | |
"widgets": { | |
"state": {}, | |
"version": "1.1.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment