Skip to content

Instantly share code, notes, and snippets.

@emilfh
Created January 4, 2021 17:41
Show Gist options
  • Save emilfh/6f53246c4241d75d4f724ec0d046f56f to your computer and use it in GitHub Desktop.
Save emilfh/6f53246c4241d75d4f724ec0d046f56f to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"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>TUPLES IN PYTHON</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref0\"></a>\n",
"<center><h2>About the Dataset</h2></center>"
]
},
{
"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\">Tuples</a></li>\n",
"<li><a href=\"#ref2\">Quiz on Tuples</a></li>\n",
"\n",
"<p></p>\n",
"Estimated Time Needed: <strong>15 min</strong>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Imagine you received album recommendations from your friends and compiled all of the recomendations 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": [
"<a id=\"ref1\"></a>\n",
"<center><h2>Tuples</h2></center>\n",
"\n",
"In Python, there are different data types: string, integer and float. These data types can all be contained in a tuple as follows:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://ibm.box.com/shared/static/t2jw5ia78ulp8twr71j6q7055hykz10c.png\" width = 750, align = \"center\"></a>\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('disco', 10, 1.2)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple1=(\"disco\",10,1.2 )\n",
"tuple1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The type of variable is a **tuple**. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" type(tuple1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Each element of a tuple can be accessed via an index. The following table represents the relationship between the index and the items in the tuple. Each element can be obtained by the name of the tuple followed by a square bracket with the index number:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://ibm.box.com/shared/static/83kpang0opwen5e5gbwck6ktqw7btwoe.gif\" width = 750, align = \"center\"></a>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can print out each value in the tuple:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"disco\n",
"10\n",
"1.2\n"
]
}
],
"source": [
"print( tuple1[0])\n",
"print( tuple1[1])\n",
"print( tuple1[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can print out the **type** of each value in the tuple:\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n",
"<class 'int'>\n",
"<class 'float'>\n"
]
}
],
"source": [
"print( type(tuple1[0]))\n",
"print( type(tuple1[1]))\n",
"print( type(tuple1[2]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use negative indexing. We use the same table above with corresponding negative values:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <img src = \"https://ibm.box.com/shared/static/uwlfzo367bekwg0p5s5odxlz7vhpojyj.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can obtain the last element as follows (this time we will not use the print statement to display the values):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple1[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can display the next two elements as follows:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple1[-2]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'disco'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple1[-3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can concatenate or combine tuples by using the **+** sign:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('disco', 10, 1.2, 'hard rock', 10)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple2=tuple1+(\"hard rock\", 10)\n",
"tuple2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can slice tuples obtaining multiple values as demonstrated by the figure below:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://ibm.box.com/shared/static/s9nofy728bcnsgnx3vh159bu16w7frnc.gif\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can slice tuples, obtaining new tuples with the corresponding elements: "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('disco', 10, 1.2)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple2[0:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can obtain the last two elements of the tuple:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('hard rock', 10)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple2[3:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can obtain the length of a tuple using the length command: "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(tuple2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This figure shows the number of elements:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"<img src = \"https://ibm.box.com/shared/static/apxe8l3w42f597yjhizg305merlm4ijf.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Consider the following tuple:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"Ratings =(0,9,6,5,10,8,9,6,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can assign the tuple to a 2nd variable:\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(0, 9, 6, 5, 10, 8, 9, 6, 2)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ratings1=Ratings\n",
"Ratings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can sort the values in a tuple and save it to a new tuple: "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 5, 6, 6, 8, 9, 9, 10]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"RatingsSorted=sorted(Ratings )\n",
"RatingsSorted"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A tuple can contain another tuple as well as other more complex data types. This process is called 'nesting'. Consider the following tuple with several elements: "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"NestedT =(1, 2, (\"pop\", \"rock\") ,(3,4),(\"disco\",(1,2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each element in the tuple including other tuples can be obtained via an index as shown in the figure:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://ibm.box.com/shared/static/estqe2bczv5weocc4ag4mx9dtqy952fp.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Element 0 of Tuple: 1\n",
"Element 1 of Tuple: 2\n",
"Element 2 of Tuple: ('pop', 'rock')\n",
"Element 3 of Tuple: (3, 4)\n",
"Element 4 of Tuple: ('disco', (1, 2))\n"
]
}
],
"source": [
"print(\"Element 0 of Tuple: \", NestedT[0])\n",
"print(\"Element 1 of Tuple: \", NestedT[1])\n",
"print(\"Element 2 of Tuple: \", NestedT[2])\n",
"print(\"Element 3 of Tuple: \", NestedT[3])\n",
"print(\"Element 4 of Tuple: \", NestedT[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the second index to access other tuples as demonstrated in the figure:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://ibm.box.com/shared/static/j1orgjuasaaj3d0feymedrnoqv8trqyo.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can access the nested tuples :"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Element 2,0 of Tuple: pop\n",
"Element 2,1 of Tuple: rock\n",
"Element 3,0 of Tuple: 3\n",
"Element 3,1 of Tuple: 4\n",
"Element 4,0 of Tuple: disco\n",
"Element 4,1 of Tuple: (1, 2)\n"
]
}
],
"source": [
"print(\"Element 2,0 of Tuple: \", NestedT[2][0])\n",
"print(\"Element 2,1 of Tuple: \", NestedT[2][1])\n",
"print(\"Element 3,0 of Tuple: \", NestedT[3][0])\n",
"print(\"Element 3,1 of Tuple: \", NestedT[3][1])\n",
"print(\"Element 4,0 of Tuple: \", NestedT[4][0])\n",
"print(\"Element 4,1 of Tuple: \", NestedT[4][1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can access strings in the second nested tuples using a third index:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'r'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"NestedT[2][1][0]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'o'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" NestedT[2][1][1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can use a tree to visualise the process. Each new index corresponds to a deeper level in the tree:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <img src ='https://ibm.box.com/shared/static/vjvsygpzpwcr6czsucgno1wukyhk5vxq.gif' width = 750, align = \"center\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we can access elements nested deeper in the tree with a fourth index:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"NestedT[4][1][0]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"NestedT[4][1][1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following figure shows the relationship of the tree and the element **NestedT[4][1][1]**:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <img src ='https://ibm.box.com/shared/static/9y5s7515zwzc9v6i4f67yj3np2fv9evs.gif'width = 750, align = \"center\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a id=\"ref2\"></a>\n",
"<h2 align=center> Quiz on Tuples </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the following tuple:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('pop',\n",
" 'rock',\n",
" 'soul',\n",
" 'hard rock',\n",
" 'soft rock',\n",
" 'R&B',\n",
" 'progressive rock',\n",
" 'disco')"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"genres_tuple = (\"pop\", \"rock\", \"soul\", \"hard rock\", \"soft rock\", \\\n",
" \"R&B\", \"progressive rock\", \"disco\") \n",
"genres_tuple"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find the length of the tuple, \"genres_tuple\":"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(genres_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\">\n",
"<a href=\"#String1\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"\n",
"\n",
"<div id=\"String1\" class=\"collapse\">\n",
"\n",
"\"len(genres_tuple)\"\n",
" <a ><img src = \"https://ibm.box.com/shared/static/n4969qbta8hhsycs2dc4n8jqbf062wdw.png\" width = 1100, align = \"center\"></a>\n",
"```\n",
"\n",
"\n",
"```\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Access the element, with respect to index 3: "
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'hard rock'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"genres_tuple[3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\">\n",
"<a href=\"#2\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"\n",
"\n",
"<div id=\"2\" class=\"collapse\">\n",
"\n",
"\n",
" <a ><img src = \"https://ibm.box.com/shared/static/s6r8v2uy6wifmaqv53w6adabqci47zme.png\" width = 1100, align = \"center\"></a>\n",
"\n",
"</div>\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use slicing to obtain indexes 3, 4 and 5:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('hard rock', 'soft rock', 'R&B')"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"genres_tuple[3:6]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
" <div align=\"right\">\n",
"<a href=\"#3\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"\n",
"\n",
"<div id=\"3\" class=\"collapse\">\n",
"\n",
"\n",
" <a ><img src = \"https://ibm.box.com/shared/static/nqo84vydw6eixdex0trybuvactcw7ffi.png\" width = 1100, align = \"center\"></a>\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find the first two elements of the tuple \"genres_tuple\":"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('pop', 'rock')"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"genres_tuple[0:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div align=\"right\">\n",
"<a href=\"#q5\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"</div>\n",
"<div id=\"q5\" class=\"collapse\">\n",
"```\n",
"genres_tuple[0:2]\n",
"\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find the first index of 'disco':"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"genres_tuple.index(\"disco\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div align=\"right\">\n",
"<a href=\"#q6\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"</div>\n",
"<div id=\"q6\" class=\"collapse\">\n",
"```\n",
"genres_tuple.index(\"disco\") \n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Generate a sorted List from the Tuple C_tuple=(-5,1,-3):"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[-5, -3, 1]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C_tuple = (-5,1,-3)\n",
"C_sorted = sorted(C_tuple)\n",
"C_sorted"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div align=\"right\">\n",
"<a href=\"#q7\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"</div>\n",
"<div id=\"q7\" class=\"collapse\">\n",
"```\n",
"C_tuple = (-5,1,-3)\n",
"C_list = sorted(C_tuple)\n",
"C_list\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <hr></hr>\n",
"<div class=\"alert alert-success alertsuccess\" style=\"margin-top: 20px\">\n",
"<h4> [Tip] Saving the Notebook </h4> \n",
"\n",
"Your notebook saves automatically every two minutes. You can manually save by going to **File** > **Save and Checkpoint**. You can come back to this notebook anytime by clicking this notebook under the \"**Recent Notebooks**\" list on the right-hand side. \n",
"\n",
"\n",
"</div>\n",
"<hr></hr>\n",
"<div class=\"alert alert-success alertsuccess\" style=\"margin-top: 20px\">\n",
"<h4> [Tip] Notebook Features </h4> \n",
" \n",
"Did you know there are other **notebook options**? Click on the **>** symbol to the left of the notebook:\n",
"\n",
"<img src =https://ibm.box.com/shared/static/otu40m0kkzz5hropxah1nnzd2j01itom.png width = 35%>\n",
"\n",
"\n",
"<p></p>\n",
"\n",
"</div>\n",
"<hr></hr>"
]
},
{
"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 human cognition. Joseph has been working for IBM since he completed his PhD.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <hr>\n",
"Copyright &copy; 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