<jupyter_start><jupyter_text>Tuple A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets. e.g - Create a Tuple:<jupyter_code>thistuple = ("apple", "banana", "cherry")
print(thistuple)<jupyter_output>('apple', 'banana', 'cherry')<jupyter_text>Access Tuple Items You can access tuple items by referring to the index number, inside square brackets: e.g - Print the second item in the tuple:<jupyter_code>thistuple = ("apple", "banana", "cherry")
print(thistuple[1])<jupyter_output>banana<jupyter_text>Negative Indexing Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc. e.g - Print the last item of the tuple:<jupyter_code>thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])<jupyter_output>cherry<jupyter_text>Range of Indexes You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new tuple with the specified items. e.g - Return the third, fourth, and fifth item:e.g -<jupyter_code>thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])<jupyter_output>('cherry', 'orange', 'kiwi')<jupyter_text>*Note: The search will start at index 2 (included) and end at index 5 (not included). *Remember that the first item has index 0.<jupyter_code># Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the tuple:
e.g - This example returns the items from index -4 (included) to index -1 (excluded)<jupyter_output><empty_output><jupyter_text>thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")print(thistuple[-4:-1])<jupyter_code># Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
e.g - Convert the tuple into a list to be able to change it:<jupyter_output><empty_output><jupyter_text>x = ("apple", "banana", "cherry")y = list(x)y[1] = "kiwi"x = tuple(y)print(x)<jupyter_code># Loop Through a Tuple
You can loop through the tuple items by using a for loop.
e.g - Iterate through the items and print the values:<jupyter_output><empty_output><jupyter_text>thistuple = ("apple", "banana", "cherry")for x in thistuple:  print(x)<jupyter_code># Check if Item Exists
To determine if a specified item is present in a tuple use the in keyword:
e.g - Check if "apple" is present in the tuple:<jupyter_output><empty_output><jupyter_text>thistuple = ("apple", "banana", "cherry")if "apple" in thistuple:  print("Yes, 'apple' is in the fruits tuple")<jupyter_code># Tuple Length
To determine how many items a tuple has, use the len() method:
e.g - Print the number of items in the tuple:<jupyter_output><empty_output><jupyter_text>thistuple = ("apple", "banana", "cherry")print(len(thistuple))<jupyter_code># Add Items
Once a tuple is created, you cannot add items to it. Tuples are unchangeable.<jupyter_output><empty_output><jupyter_text>e.g - You cannot add items to a tuple:<jupyter_code>thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)<jupyter_output><empty_output><jupyter_text>Create Tuple With One Item To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple. e,g - One item tuple, remember the commma:<jupyter_code>thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))<jupyter_output><class 'tuple'>
<class 'str'><jupyter_text>Remove Items<jupyter_code>*Note: You cannot remove items in a tuple.<jupyter_output><empty_output><jupyter_text>Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely: The del keyword can delete the tuple completely:e.g -<jupyter_code>thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists<jupyter_output><empty_output><jupyter_text>Join Two Tuples<jupyter_code>tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)<jupyter_output>('a', 'b', 'c', 1, 2, 3)<jupyter_text>The tuple() Constructor It is also possible to use the tuple() constructor to make a tuple.<jupyter_code>thistuple = tuple(("apple", "banana", "cherry")) 
# note the double round-brackets
print(thistuple)<jupyter_output>('apple', 'banana', 'cherry')<jupyter_text>Tuple Methods Python has two built-in methods that you can use on tuples.<jupyter_code>count()	     Returns the number of times a specified value occurs in a tuple
index()	     Searches the tuple for a specified value and returns the position              of where it was found<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11228212403729226, 'fraction_numerical': 0.005472233481961897, 'mean_word_length': 5.98019801980198, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '502821', 'license': 'Apache-2.0', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1614, 'n_words': 596, 'path': 'Python Tuples.ipynb', 'repo_name': 'Vinaykumargond/Python-Tutorial', 'size': '4934'}