<jupyter_start><jupyter_text>Python - Tuples    Tuple is data structure that holds collection of different objects in it. For example, a tuple can hold numerical as well as string values in it along with the other collections like list, tuples etc. Creation Empty tuple can be created with angular brackets.<jupyter_code>tuple1 = ()
print("tuple1----> ", tuple1)<jupyter_output>tuple1---->  ()<jupyter_text>Tuple elements are enclosed within angular brackets.<jupyter_code>my_tuple = ('a', 'b', 'c', 'd', 'e')
my_tuple<jupyter_output><empty_output><jupyter_text>Tuple can hold elements belonging to the different data types.<jupyter_code>my_mixed_tuple = ('a', 1, 1.1, True)
my_mixed_tuple<jupyter_output><empty_output><jupyter_text>Type of tuple variable is tuple.<jupyter_code>print(type(my_tuple))
print(type(my_mixed_tuple))<jupyter_output><class 'tuple'>
<class 'tuple'><jupyter_text>Type of individual elements can be same or different which can be checked as follows.<jupyter_code>type(my_tuple[0])
type(my_mixed_tuple[1])<jupyter_output><empty_output><jupyter_text>Size of the tuple can be determined using the len() funtion<jupyter_code>len(my_tuple)
len(my_mixed_tuple)<jupyter_output><empty_output><jupyter_text>Indexing Tuple elements can be accessed using the bracket operator. The index value starts from 0 and goes upto len() -1 .<jupyter_code>print("First element ---> " , my_mixed_tuple[0], "      ", "type of element ----> ", type(my_mixed_tuple[0]))
print("Second element ---> " , my_mixed_tuple[1], "      ", "type of element ----> ", type(my_mixed_tuple[1]))
print("Third element ---> " , my_mixed_tuple[2], "      ", "type of element ----> ", type(my_mixed_tuple[2]))
print("Fourth element ---> " , my_mixed_tuple[3], "      ", "type of element ----> ", type(my_mixed_tuple[3]))<jupyter_output>First element --->  a        type of element ---->  <class 'str'>
Second element --->  1        type of element ---->  <class 'int'>
Third element --->  1.1        type of element ---->  <class 'float'>
Fourth element --->  True        type of element ---->  <class 'bool'><jupyter_text>-ve indexing also possible on the tuples elements.<jupyter_code>print("Last element ---> " , my_mixed_tuple[-1])
print("Second Last element ---> " , my_mixed_tuple[-2])
print("Third element from Last---> " , my_mixed_tuple[-3])
print("Fourth element from Last---> " , my_mixed_tuple[-4])<jupyter_output>Last element --->  True
Second Last element --->  1.1
Third element from Last--->  1
Fourth element from Last--->  a<jupyter_text>Slicing The part of tuples can be accessed using the slicing.<jupyter_code>my_mixed_tuple[ 0 : 2]   # extracts first two elements of tuple
my_mixed_tuple[ 1 : ]   # extracts all elements of tuple starting from second element
my_mixed_tuple[ : 3]   # extracts first three elements of tuple
my_mixed_tuple[ 0 : : 2] # extract every alternate element of tuple
my_mixed_tuple[ : : -1 ]  #access the elements of tuple in reverse manner<jupyter_output><empty_output><jupyter_text>Concatenation Two tuples can be joined using the '+' operator.<jupyter_code>tuple1 = ('1', '2', '3', '4')
tuple2 = ('5', '6')
new_tuple = tuple1 + tuple2  # concat two tuples using + 
new_tuple
tuple1 = ('1', '2', '3', '4')
new_tuple = tuple1 + ('5', '6')  # concat two tuples using + 
new_tuple<jupyter_output><empty_output><jupyter_text>Repetition Tuple elements can be repeated using "*" operator.<jupyter_code>(1, 2) * 3  # create a tuple with elements '1' and '2' repeated three times
my_tuple = ( 'a', 'b')  
new_tuple = my_tuple * 2   #create a tuple with elements 'a' and 'b' repeated two times
new_tuple<jupyter_output><empty_output><jupyter_text>Immutability Tuple elements can not be altered.<jupyter_code>my_tuple = ('a', 'b', 'c', 'd', 'e')
my_tuple[0] = 'p'    #throws error as individual tuple elements can not be altered<jupyter_output><empty_output><jupyter_text>New elements can not be added to the tuple using append().<jupyter_code>my_tuple.append('f')   #throws error as tuple can not be altered
my_tuple.remove(0)   # elements can not be removed from the tuple<jupyter_output><empty_output><jupyter_text>Tuple can be assigned to another tuple variable.<jupyter_code>my_tuple = ('a', 'b', 'c', 'd', 'e')
new_tuple = my_tuple

print("my_tuple---->", my_tuple)
print("new_tuple---->", new_tuple)<jupyter_output>my_tuple----> ('a', 'b', 'c', 'd', 'e')
new_tuple----> ('a', 'b', 'c', 'd', 'e')<jupyter_text>Operations<jupyter_code>my_tuple = ('a', 'b', 'c', 'c', 'e')<jupyter_output><empty_output><jupyter_text>index() can be used to get index of first instance of element value.<jupyter_code>my_tuple.index('c')  #get first index of element 'c' in tuple
my_tuple.index('b')  #get first index of element 'b' in tuple
my_tuple.index('v')  #get first index of element 'v' in tuple, as value is not present throws error<jupyter_output><empty_output><jupyter_text>count() - counts the number of instances of element value in tuple.<jupyter_code>my_tuple.count('a') # count how many times 'a' appeared in tuple
my_tuple.count('c') # count how many times 'c' appeared in tuple
my_tuple.count('m') # count how many times 'm' appeared in tuple (which is not present as element in the tuple)<jupyter_output><empty_output><jupyter_text>Iterable Tuple is iterable. One can traverse through the individual tuple elements using loops.<jupyter_code>for i in range(len(my_tuple)):   #access tuple elements with for loop
    print("(", i, ") ", my_tuple[i])<jupyter_output>( 0 )  a
( 1 )  b
( 2 )  c
( 3 )  c
( 4 )  e<jupyter_text>Sorting Tuple elements can be sorted as follows :<jupyter_code>Scores = (10, 8 , 3, 2 , 4, 0 , 0, 3)
Scores
sorted(Scores)  # sort the tuple values using the sorted
grades = ('a', 'c', 'd', 'e', 'b')
grades
sorted(grades)<jupyter_output><empty_output><jupyter_text>Deletion Tuple can be removed from the memory by using del().<jupyter_code>my_tuple     # tuple elements are printed
del(my_tuple)  # remove the tuple
my_tuple  # throws error as tuple no more available<jupyter_output><empty_output><jupyter_text>Exercise Q1. Accept 5 string values from user and store them as part of tuple. Print the elements of tuple with their indices.<jupyter_code>#Try it here

#Try it here<jupyter_output><empty_output><jupyter_text>Q2. Accept 5 string values from user and store them as part of tuple. Print the elements of tuple with their indices in reverse order.   Answer<jupyter_code>#Q1 Solution

#create empty tuple 
tuple = ()  

#Ask user five times to enter the string and concat that string to the tuple
times = 5 
for i in range(times):
    user_string = input("enter the string\n")
    tuple = tuple + (user_string, )
    
#Output the tuple
print("\nThe elements of tuple are : ")
for i in range(len(tuple)):
    print("(", i , ")", "   " , tuple[i])
    
#Q2 Solution

#create empty tuple 
tuple = ()  

#Ask user five times to enter the string and concat that string to the tuple
times = 5 
for i in range(times):
    user_string = input("enter the string\n")
    tuple = tuple + (user_string, )
    
#Output the tuple
print("\nThe elements of tuple (in reverse order) are : ")
for i in range(1, len(tuple) + 1):
    j = -1 * i 
    print("(", j , ")", "   " , tuple[j])<jupyter_output>enter the string
e
enter the string
3
enter the string
2
enter the string
1
enter the string
2

The elements of tuple (in reverse order) are : 
( -1 )     2
( -2 )     1
( -3 )     2
( -4 )     3
( -5 )     e
{'fraction_non_alphanumeric': 0.13252688172043012, 'fraction_numerical': 0.013440860215053764, 'mean_word_length': 4.577961019490255, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '526239', 'license': 'Apache-2.0', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2479, 'n_words': 821, 'path': 'Python Fundamentals for Data Science/S2/Additional Notebooks S2/5_Python - Tuples.ipynb', 'repo_name': 'ParameswaranP/BITS-DSE-2021---2023', 'size': '7440'}