<jupyter_start><jupyter_text>TUPLES IN PYTHON About the Dataset  Table of ContentsAbout the DatasetTuplesQuiz on TuplesEstimated Time Needed: 15 min Imagine you received album recommendations from your friends and compiled all of the recomendations into a table, with specific information about each album.The table has one row for each movie and several columns:- **artist** - Name of the artist- **album** - Name of the album- **released_year** - Year the album was released- **length_min_sec** - Length of the album (hours,minutes,seconds)- **genre** - Genre of the album- **music_recording_sales_millions** - Music recording sales (millions in USD) on [SONG://DATABASE](http://www.song-database.com/)- **claimed_sales_millions** - Album's claimed sales (millions in USD) on [SONG://DATABASE](http://www.song-database.com/)- **date_released** - Date on which the album was released- **soundtrack** - Indicates if the album is the movie soundtrack (Y) or (N)- **rating_of_friends** - Indicates the rating from your friends from 1 to 10The dataset can be seen below:      Artist    Album     Released    Length    Genre     Music recording sales (millions)    Claimed sales (millions)    Released    Soundtrack    Rating (friends)        <NAME>    Thriller     1982    00:42:19    Pop, rock, R&B    46    65    30-Nov-82        10.0        AC/DC    Back in Black     1980    00:42:11    Hard rock    26.1    50    25-Jul-80        8.5          Pink Floyd    The Dark Side of the Moon     1973    00:42:49    Progressive rock    24.2    45    01-Mar-73        9.5          Whitney Houston    The Bodyguard     1992    00:57:44    Soundtrack/R&B, soul, pop    26.1    50    25-Jul-80    Y    7.0          Meat Loaf    Bat Out of Hell     1977    00:46:33    Hard rock, progressive rock    20.6    43    21-Oct-77        7.0          Eagles    Their Greatest Hits (1971-1975)     1976    00:43:08    Rock, soft rock, folk rock    32.2    42    17-Feb-76        9.5          Bee Gees    Saturday Night Fever     1977    1:15:54    Disco    20.6    40    15-Nov-77    Y    9.0          Fleetwood Mac    Rumours     1977    00:40:01    Soft rock    27.9    40    04-Feb-77        9.5    TuplesIn Python, there are different data types: string, integer and float. These data types can all be contained in a tuple as follows:<jupyter_code>tuple1=("disco",10,1.2 )
tuple1<jupyter_output><empty_output><jupyter_text>The type of variable is a **tuple**.<jupyter_code>type(tuple1)<jupyter_output><empty_output><jupyter_text>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:  We can print out each value in the tuple:<jupyter_code>print( tuple1[0])
print( tuple1[1])
print( tuple1[2])<jupyter_output><empty_output><jupyter_text>We can print out the **type** of each value in the tuple:<jupyter_code>print( type(tuple1[0]))
print( type(tuple1[1]))
print( type(tuple1[2]))<jupyter_output><empty_output><jupyter_text>We can also use negative indexing. We use the same table above with corresponding negative values:   We can obtain the last element as follows (this time we will not use the print statement to display the values):<jupyter_code>tuple1[-1]<jupyter_output><empty_output><jupyter_text>We can display the next two elements as follows:<jupyter_code>tuple1[-2]
tuple1[-3]<jupyter_output><empty_output><jupyter_text>We can concatenate or combine tuples by using the **+** sign:<jupyter_code>tuple2=tuple1+("hard rock", 10)
tuple2<jupyter_output><empty_output><jupyter_text>We can slice tuples obtaining multiple values as demonstrated by the figure below:  We can slice tuples, obtaining new tuples with the corresponding elements:<jupyter_code>tuple2[0:3]<jupyter_output><empty_output><jupyter_text>We can obtain the last two elements of the tuple:<jupyter_code>tuple2[3:5]<jupyter_output><empty_output><jupyter_text>We can obtain the length of a tuple using the length command:<jupyter_code>len(tuple2)<jupyter_output><empty_output><jupyter_text>This figure shows the number of elements:   Consider the following tuple:<jupyter_code>Ratings  =(0,9,6,5,10,8,9,6,2)<jupyter_output><empty_output><jupyter_text>We can assign the tuple to a 2nd variable:<jupyter_code>Ratings1=Ratings
Ratings<jupyter_output><empty_output><jupyter_text>We can sort the values in a tuple and save it to a new tuple:<jupyter_code>RatingsSorted=sorted(Ratings )
RatingsSorted<jupyter_output><empty_output><jupyter_text>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:<jupyter_code>NestedT =(1, 2, ("pop", "rock") ,(3,4),("disco",(1,2)))<jupyter_output><empty_output><jupyter_text>Each element in the tuple including other tuples can be obtained via an index as shown in the figure:<jupyter_code>print("Element 0 of Tuple: ",   NestedT[0])
print("Element 1 of Tuple: ",  NestedT[1])
print("Element 2 of Tuple: ",  NestedT[2])
print("Element 3 of Tuple: ", NestedT[3])
print("Element 4 of Tuple: ", NestedT[4])<jupyter_output><empty_output><jupyter_text>We can use the second index to access other tuples as demonstrated in the figure:   We can access the nested tuples :<jupyter_code>print("Element 2,0 of Tuple: ",   NestedT[2][0])
print("Element 2,1 of Tuple: ",   NestedT[2][1])
print("Element 3,0 of Tuple: ",   NestedT[3][0])
print("Element 3,1 of Tuple: ",   NestedT[3][1])
print("Element 4,0 of Tuple: ",   NestedT[4][0])
print("Element 4,1 of Tuple: ",   NestedT[4][1])<jupyter_output><empty_output><jupyter_text>We can access strings in the second nested tuples using a third index:<jupyter_code>NestedT[2][1][0]
 NestedT[2][1][1]<jupyter_output><empty_output><jupyter_text>We can use a tree to visualise the process. Each new index corresponds to a deeper level in the tree:   Similarly, we can access elements nested deeper in the tree with a fourth index:<jupyter_code>NestedT[4][1][0]
NestedT[4][1][1]<jupyter_output><empty_output><jupyter_text>The following figure shows the relationship of the tree and the element **NestedT[4][1][1]**:     Quiz on Tuples  Consider the following tuple:<jupyter_code>genres_tuple = ("pop", "rock", "soul", "hard rock", "soft rock", \
                "R&B", "progressive rock", "disco") 
genres_tuple<jupyter_output><empty_output><jupyter_text>Find the length of the tuple, "genres_tuple": Click here for the solution"len(genres_tuple)" ``````  Access the element, with respect to index 3:<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10197960407918416, 'fraction_numerical': 0.04274145170965807, 'mean_word_length': 4.49341021416804, 'pattern_counts': {'":': 1, '<': 82, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '62732', 'license': 'MIT', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2156, 'n_words': 715, 'path': 'Jupyter notebook/IBM/python for data science/python tuples.ipynb', 'repo_name': 'qwertypsv/Documents', 'size': '6677'}