Created
February 7, 2020 13:15
-
-
Save AbheeHub/8169dc50c4c204e4cd546c75b5a3a169 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
This file contains 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=\"https://cognitiveclass.ai/\">\n", | |
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n", | |
"</a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h1>Lists in Python</h1>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<p><strong>Welcome!</strong> This notebook will teach you about the lists in the Python Programming Language. By the end of this lab, you'll know the basics list operations in Python, including indexing, list operations and copy/clone list.</p> " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n", | |
" <a href=\"https://cocl.us/topNotebooksPython101Coursera\">\n", | |
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/TopAd.png\" width=\"750\" align=\"center\">\n", | |
" </a>\n", | |
"</div>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>Table of Contents</h2>\n", | |
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n", | |
" <ul>\n", | |
" <li>\n", | |
" <a href=\"#dataset\">About the Dataset</a>\n", | |
" </li>\n", | |
" <li>\n", | |
" <a href=\"#list\">Lists</a>\n", | |
" <ul>\n", | |
" <li><a href=\"index\">Indexing</a></li>\n", | |
" <li><a href=\"content\">List Content</a></li>\n", | |
" <li><a href=\"op\">List Operations</a></li>\n", | |
" <li><a href=\"co\">Copy and Clone List</a></li>\n", | |
" </ul>\n", | |
" </li>\n", | |
" <li>\n", | |
" <a href=\"#quiz\">Quiz on Lists</a>\n", | |
" </li>\n", | |
" </ul>\n", | |
" <p>\n", | |
" Estimated time needed: <strong>15 min</strong>\n", | |
" </p>\n", | |
"</div>\n", | |
"\n", | |
"<hr>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"#dataset\">About the Dataset</h2>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Imagine you received album recommendations from your friends and compiled all of the recommandations into a table, with specific information about each album.\n", | |
"\n", | |
"The table has one row for each movie 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": [ | |
"<h2 id=\"list\">Lists</h2>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3 id=\"index\">Indexing</h3>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"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 <b>index</b>. An index is used to access and refer to items within a list." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/ListsIndex.png\" width=\"1000\" />" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" To create a list, type the list within square brackets <b>[ ]</b>, with your content inside the parenthesis and separated by commas. Let’s try it!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.1, 1982]" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Create a list\n", | |
"\n", | |
"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": [ | |
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/ListsNeg.png\" width=\"1000\" />" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": 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 elements on each index\n", | |
"\n", | |
"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": [ | |
"<h3 id=\"content\">List Content</h3>" | |
] | |
}, | |
{ | |
"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": null, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# Sample List\n", | |
"\n", | |
"[\"Michael Jackson\", 10.1, 1982, [1, 2], (\"A\", 1)]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3 id=\"op\">List Operations</h3>" | |
] | |
}, | |
{ | |
"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": 3, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.1, 1982, 'MJ', 1]" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Sample List\n", | |
"\n", | |
"L = [\"Michael Jackson\", 10.1,1982,\"MJ\",1]\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/ListsSlice.png\" width=\"1000\">" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['MJ', 1]" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# List slicing\n", | |
"\n", | |
"L[3:5]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can use the method <code>extend</code> to add new elements to the list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, 'pop', 10]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Use extend to add elements to list\n", | |
"\n", | |
"L = [ \"Michael Jackson\", 10.2]\n", | |
"L.extend(['pop', 10])\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Another similar method is <code>append</code>. If we apply <code>append</code> instead of <code>extend</code>, we add one element to the list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, ['pop', 10]]" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Use append to add elements to list\n", | |
"\n", | |
"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 <code>extend</code> we add two new elements to the list. The list <code>L</code> is then modified by adding two new elements:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, 'pop', 10]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Use extend to add elements to list\n", | |
"\n", | |
"L = [ \"Michael Jackson\", 10.2]\n", | |
"L.extend(['pop', 10])\n", | |
"L" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"If we append the list <code>['a','b']</code> we have one new element consisting of a nested list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Michael Jackson', 10.2, 'pop', 10, ['a', 'b']]" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Use append to add elements to list\n", | |
"\n", | |
"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": 9, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before change: ['disco', 10, 1.2]\n", | |
"After change: ['hard rock', 10, 1.2]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Change the element based on the index\n", | |
"\n", | |
"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 <code>del</code> command:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Before change: ['hard rock', 10, 1.2]\n", | |
"After change: [10, 1.2]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Delete the element based on the index\n", | |
"\n", | |
"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 <code>split</code>. For example, the method <code>split</code> translates every group of characters separated by a space into an element in a list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['hard', 'rock']" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Split the string, default is by space\n", | |
"\n", | |
"'hard rock'.split()" | |
] | |
}, | |
{ | |
"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": 12, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['A', 'B', 'C', 'D']" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Split the string by comma\n", | |
"\n", | |
"'A,B,C,D'.split(',')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3 id=\"co\">Copy and Clone List</h3>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"When we set one variable <b>B</b> equal to <b>A</b>; both <b>A</b> and <b>B</b> are referencing the same list in memory:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A: ['hard rock', 10, 1.2]\n", | |
"B: ['hard rock', 10, 1.2]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Copy (copy by reference) the list A\n", | |
"\n", | |
"A = [\"hard rock\", 10, 1.2]\n", | |
"B = A\n", | |
"print('A:', A)\n", | |
"print('B:', B)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/ListsRef.png\" width=\"1000\" align=\"center\">" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Initially, the value of the first element in <b>B</b> is set as hard rock. If we change the first element in <b>A</b> to <b>banana</b>, we get an unexpected side effect. As <b>A</b> and <b>B</b> are referencing the same list, if we change list <b>A</b>, then list <b>B</b> also changes. If we check the first element of <b>B</b> we get banana instead of hard rock:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"B[0]: hard rock\n", | |
"B[0]: banana\n" | |
] | |
} | |
], | |
"source": [ | |
"# Examine the copy by reference\n", | |
"\n", | |
"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": [ | |
"<img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/ListsRefGif.gif\" width=\"1000\" />" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can clone list **A** by using the following syntax:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['banana', 10, 1.2]" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Clone (clone by value) the list A\n", | |
"\n", | |
"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": [ | |
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/ListsVal.gif\" width=\"1000\" />" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now if you change <b>A</b>, <b>B</b> will not change: " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"B[0]: banana\n", | |
"B[0]: banana\n" | |
] | |
} | |
], | |
"source": [ | |
"print('B[0]:', B[0])\n", | |
"A[0] = \"hard rock\"\n", | |
"print('B[0]:', B[0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2 id=\"quiz\">Quiz on List</h2>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Create a list <code>a_list</code>, with the following elements <code>1</code>, <code>hello</code>, <code>[1,2,3]</code> and <code>True</code>. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 'hello', [1, 2, 3], True]" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a_list = [1, 'hello', [1, 2, 3] , True]\n", | |
"a_list" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- Your answer is below:\n", | |
"a_list = [1, 'hello', [1, 2, 3] , True]\n", | |
"a_list\n", | |
"-->" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Find the value stored at index 1 of <code>a_list</code>." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'hello'" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a_list[1]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- Your answer is below:\n", | |
"a_list[1]\n", | |
"-->" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Retrieve the elements stored at index 1, 2 and 3 of <code>a_list</code>." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"collapsed": false, | |
"jupyter": { | |
"outputs_hidden": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"A=((1),[2,3],[4])\n", | |
"A[2][0]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- Your answer is below:\n", | |
"a_list[1:4]\n", | |
"-->" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Concatenate the following lists <code>A = [1, 'a']</code> and <code>B = [2, 1, 'd']</code>:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 'a', 2, 1, 'd']" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"A = [1, 'a']\n", | |
"B = [2, 1, 'd']\n", | |
"A+B" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Double-click <b>here</b> for the solution.\n", | |
"\n", | |
"<!-- Your answer is below:\n", | |
"A = [1, 'a'] \n", | |
"B = [2, 1, 'd']\n", | |
"A + B\n", | |
"-->" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<hr>\n", | |
"<h2>The last exercise!</h2>\n", | |
"<p>Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow <a href=\"https://cognitiveclass.ai/blog/data-scientists-stand-out-by-sharing-your-notebooks/\" target=\"_blank\">this article</a> to learn how to share your work.\n", | |
"<hr>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n", | |
"<h2>Get IBM Watson Studio free of charge!</h2>\n", | |
" <p><a href=\"https://cocl.us/bottemNotebooksPython101Coursera\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n", | |
"</div>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3>About the Authors:</h3> \n", | |
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Other contributors: <a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<hr>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<p>Copyright © 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python", | |
"language": "python", | |
"name": "conda-env-python-py" | |
}, | |
"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.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment