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:
The dataset can be seen below:
<table font-size:xx-small style="width:25%">
In [1]:
tuple1=("disco",10,1.2 )
tuple1
Out[1]:
The type of variable is a tuple.
In [2]:
type(tuple1)
Out[2]:
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:
In [3]:
print( tuple1[0])
print( tuple1[1])
print( tuple1[2])
We can print out the type of each value in the tuple:
In [4]:
print( type(tuple1[0]))
print( type(tuple1[1]))
print( type(tuple1[2]))
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):
In [5]:
tuple1[-1]
Out[5]:
We can display the next two elements as follows:
In [6]:
tuple1[-2]
Out[6]:
In [7]:
tuple1[-3]
Out[7]:
We can concatenate or combine tuples by using the + sign:
In [8]:
tuple2=tuple1+("hard rock", 10)
tuple2
Out[8]:
We can slice tuples obtaining multiple values as demonstrated by the figure below:
We can slice tuples, obtaining new tuples with the corresponding elements:
In [9]:
tuple2[0:3]
Out[9]:
We can obtain the last two elements of the tuple:
In [10]:
tuple2[3:5]
Out[10]:
We can obtain the length of a tuple using the length command:
In [11]:
len(tuple2)
Out[11]:
This figure shows the number of elements:
Consider the following tuple:
In [12]:
Ratings =(0,9,6,5,10,8,9,6,2)
We can assign the tuple to a 2nd variable:
In [13]:
Ratings1=Ratings
Ratings
Out[13]:
We can sort the values in a tuple and save it to a new tuple:
In [14]:
RatingsSorted=sorted(Ratings )
RatingsSorted
Out[14]:
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:
In [15]:
NestedT =(1, 2, ("pop", "rock") ,(3,4),("disco",(1,2)))
Each element in the tuple including other tuples can be obtained via an index as shown in the figure:
In [16]:
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])
We can use the second index to access other tuples as demonstrated in the figure:
We can access the nested tuples :
In [17]:
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])
We can access strings in the second nested tuples using a third index:
In [18]:
NestedT[2][1][0]
Out[18]:
In [19]:
NestedT[2][1][1]
Out[19]:
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:
In [20]:
NestedT[4][1][0]
Out[20]:
In [21]:
NestedT[4][1][1]
Out[21]:
The following figure shows the relationship of the tree and the element NestedT[4][1][1]:
Consider the following tuple:
In [22]:
genres_tuple = ("pop", "rock", "soul", "hard rock", "soft rock", \
"R&B", "progressive rock", "disco")
genres_tuple
Out[22]:
In [23]:
len(genres_tuple)
Out[23]:
In [24]:
genres_tuple[3]
Out[24]:
In [28]:
genres_tuple[3:6]
Out[28]:
In [29]:
genres_tuple[:2]
Out[29]:
genres_tuple[0:2]
In [31]:
genres_tuple.index("disco")
Out[31]:
genres_tuple.index("disco")
In [36]:
C_tuple=sorted((-5, 1, -3))
C_tuple
Out[36]:
C_tuple = (-5,1,-3)
C_list = sorted(C_tuple)
C_list
Joseph Santarcangelo 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.
In [ ]: