asset-v1_NASSCOM+FOUNDAI100+2019+type@asset+block@PY0101EN-2-1-Tuples.ipynb

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n", " <a href=\"https://cocl.us/PYTHON101EN_portal_add_top\">\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": [ "<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>Tuples in Python</h1>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<p><strong>Welcome!</strong> This notebook will teach you about the tuples in the Python Programming Language. By the end of this lab, you'll know the basics tuple operations in Python, including indexing, slicing and sorting.</p> " ] }, { "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=\"#tuple\">Tuples</a>\n", " <ul>\n", " <li><a href=\"index\">Indexing</a></li>\n", " <li><a href=\"slice\">Slicing</a></li>\n", " <li><a href=\"sort\">Sorting</a></li>\n", " </ul>\n", " </li>\n", " <li>\n", " <a href=\"#escape\">Quiz on Tuples</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:75%\">\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=\"tuple\">Tuples</h2>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, there are different data types: string, integer and float. These data types can all be contained in a tuple as follows:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesType.png\" width=\"750\" align=\"center\" />" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let us create your first tuple with string, integer and float." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('disco', 10, 1.2)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create your first tuple\n", "\n", "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Print the type of the tuple you created\n", "\n", "type(tuple1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h3 id=\"index\">Indexing</h3>" ] }, { "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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesIndex.gif\" width=\"750\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can print out each value in the tuple:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "disco\n", "10\n", "1.2\n" ] } ], "source": [ "# Print the variable on each index\n", "\n", "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<class 'str'>\n", "<class 'int'>\n", "<class 'float'>\n" ] } ], "source": [ "# Print the type of value on each index\n", "\n", "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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesNeg.png\" width=\"750\" align=\"center\">" ] }, { "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1.2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use negative index to get the value of the last element\n", "\n", "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use negative index to get the value of the second last element\n", "\n", "tuple1[-2]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'disco'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use negative index to get the value of the third last element\n", "\n", "tuple1[-3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h3 id=\"concate\">Concatenate Tuples</h3>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can concatenate or combine tuples by using the **+** sign:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('disco', 10, 1.2, 'hard rock', 10)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Concatenate two tuples\n", "\n", "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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesSlice.gif\" width=\"750\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h3 id=\"slice\">Slicing</h3>" ] }, { "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('disco', 10, 1.2)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Slice from index 0 to index 2\n", "\n", "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('hard rock', 10)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Slice from index 3 to index 4\n", "\n", "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, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the length of tuple\n", "\n", "len(tuple2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This figure shows the number of elements:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesElement.png\" width=\"750\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h3 id=\"sort\">Sorting</h3>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Consider the following tuple:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# A sample tuple\n", "\n", "Ratings = (0, 9, 6, 5, 10, 8, 9, 6, 2)" ] }, { "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": 14, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[0, 2, 5, 6, 6, 8, 9, 9, 10]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sort the tuple\n", "\n", "RatingsSorted = sorted(Ratings)\n", "RatingsSorted" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h3 id=\"nest\">Nested Tuple</h3>" ] }, { "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": 15, "metadata": {}, "outputs": [], "source": [ "# Create a nest tuple\n", "\n", "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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesNestOne.png\" width=\"750\" align=\"center\">" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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 on each index\n", "\n", "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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesNestTwo.png\" width=\"750\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " We can access the nested tuples :" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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 on each index, including nest indexes\n", "\n", "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": 18, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'r'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Print the first element in the second nested tuples\n", "\n", "NestedT[2][1][0]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Print the second element in the second nested tuples\n", "\n", "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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesNestThree.gif\" width=\"750\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, we can access elements nested deeper in the tree with a fourth index:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Print the first element in the second nested tuples\n", "\n", "NestedT[4][1][0]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Print the second element in the second nested tuples\n", "\n", "NestedT[4][1][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following figure shows the relationship of the tree and the element <code>NestedT[4][1][1]</code>:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesNestFour.gif\" width=\"750\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h2 id=\"quiz\">Quiz on Tuples</h2>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the following tuple:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sample tuple\n", "\n", "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, <code>genres_tuple</code>:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "len(genres_tuple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/TuplesQuiz.png\" width=\"1100\" align=\"center\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "<!-- Your answer is below:\n", "len(genres_tuple)\n", "-->" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Access the element, with respect to index 3: " ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'hard rock'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "genres_tuple[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "<!-- Your answer is below:\n", "genres_tuple[3]\n", "-->" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use slicing to obtain indexes 3, 4 and 5:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('hard rock', 'soft rock', 'R&B')" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "genres_tuple[3:6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "<!-- Your answer is below:\n", "genres_tuple[3:6]\n", "-->" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the first two elements of the tuple <code>genres_tuple</code>:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('pop', 'rock')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "genres_tuple[0:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "<!-- Your answer is below:\n", "genres_tuple[0:2]\n", "-->" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the first index of <code>\"disco\"</code>:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "genres_tuple.index(\"disco\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "<!-- Your answer is below:\n", "genres_tuple.index(\"disco\")\n", "-->" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate a sorted List from the Tuple <code>C_tuple=(-5, 1, -3)</code>:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'sorted'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-35-8af2af9bbc58>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Write your code below and press Shift+Enter to execute\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mC_tuple\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mC_tuple_sorted\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mC_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'sorted'" ] } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "C_tuple=(-5, 1, -3)\n", "C_tuple_sorted=C_tuple.sorted()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "<!-- Your answer is below:\n", "C_tuple = (-5, 1, -3)\n", "C_list = sorted(C_tuple)\n", "C_list\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/PYTHON101EN_portal_add_bottom\"><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.11" } }, "nbformat": 4, "nbformat_minor": 4 }
Created on Skills Network Labs

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.