{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Tuples in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Welcome! 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.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", " \n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Table of Contents

\n", "
\n", " \n", "

\n", " Estimated time needed: 15 min\n", "

\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

About the Dataset

" ] }, { "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", "
\n", "
\n", "\n", "The dataset can be seen below:\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ArtistAlbumReleasedLengthGenreMusic recording sales (millions)Claimed sales (millions)ReleasedSoundtrackRating (friends)
Michael JacksonThriller198200:42:19Pop, rock, R&B466530-Nov-8210.0
AC/DCBack in Black198000:42:11Hard rock26.15025-Jul-808.5
Pink FloydThe Dark Side of the Moon197300:42:49Progressive rock24.24501-Mar-739.5
Whitney HoustonThe Bodyguard199200:57:44Soundtrack/R&B, soul, pop26.15025-Jul-80Y7.0
Meat LoafBat Out of Hell197700:46:33Hard rock, progressive rock20.64321-Oct-777.0
EaglesTheir Greatest Hits (1971-1975)197600:43:08Rock, soft rock, folk rock32.24217-Feb-769.5
Bee GeesSaturday Night Fever19771:15:54Disco20.64015-Nov-77Y9.0
Fleetwood MacRumours197700:40:01Soft rock27.94004-Feb-779.5
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Tuples

" ] }, { "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let us create your first tuple with string, integer and float." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Print the type of the tuple you created\n", "\n", "type(tuple1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Indexing

" ] }, { "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can print out each value in the tuple:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": [ "" ] }, { "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Use negative index to get the value of the second last element\n", "\n", "tuple1[-2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Use negative index to get the value of the third last element\n", "\n", "tuple1[-3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Concatenate Tuples

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can concatenate or combine tuples by using the **+** sign:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Slicing

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can slice tuples, obtaining new tuples with the corresponding elements: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Sorting

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Consider the following tuple:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Sort the tuple\n", "\n", "RatingsSorted = sorted(Ratings)\n", "RatingsSorted" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Nested Tuple

" ] }, { "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": null, "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "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": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " We can access the nested tuples :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Print the first element in the second nested tuples\n", "\n", "NestedT[2][1][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, we can access elements nested deeper in the tree with a fourth index:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Print the first element in the second nested tuples\n", "\n", "NestedT[4][1][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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 NestedT[4][1][1]:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Quiz on Tuples

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the following tuple:" ] }, { "cell_type": "code", "execution_count": 2, "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": 2, "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, genres_tuple:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 3, "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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Access the element, with respect to index 3: " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'hard rock'" ] }, "execution_count": 7, "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", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use slicing to obtain indexes 3, 4 and 5:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('hard rock', 'soft rock', 'R&B')" ] }, "execution_count": 8, "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", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the first two elements of the tuple genres_tuple:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "('pop', 'rock')" ] }, "execution_count": 9, "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", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the first index of \"disco\":" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 10, "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", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate a sorted List from the Tuple C_tuple=(-5, 1, -3):" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "[-5, -3, 1]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "C_tuple=(-5, 1, -3)\n", "c_list = sorted(C_tuple)\n", "c_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The last exercise!

\n", "

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 this article to learn how to share your work.\n", "


" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Get IBM Watson Studio free of charge!

\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

About the Authors:

\n", "

Joseph Santarcangelo 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.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other contributors: Mavis Zhou" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Copyright © 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the MIT License.

" ] } ], "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 }