<jupyter_start><jupyter_text>Python tuple - A tuple is a collection of Python objects separated by commas. - In Python tuples are written with round brackets or parentheses ().  - Tuples are ordered, it means that the items have a defined order, and that order will not change.- In someways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists which are mutable.<jupyter_code># Empty tuple
t1 = ()
t1<jupyter_output><empty_output><jupyter_text>If you have a `tuple` with one element, you must include a comma, (even though there is only one value.)<jupyter_code>t2 = (4,)
print(type(t2))
# This is not a tuple:
t3 = (4)
print(type(t3))
t3 = (0, 'one', 'two', 3, 4, 5.5)
print(type(t3))
print('Length =', len(t3))
[type(c) for c in t3]
print(t3[2])
# This action is not valid for tuples. Tuples are unmutable. They cannot change.
t3[2] = 2           # This will raise an error<jupyter_output><empty_output><jupyter_text>Once  a tuple is created, you cannot add items to it.<jupyter_code>t3.append(6)        # This will raise an error<jupyter_output><empty_output><jupyter_text>But... you can convert it into a list, add your item(s), and convert it back into a tuple.<jupyter_code>l3 = list(t3)
print(l3)
print(type(l3))
# Add the new value:
l3.append(6)
print(l3)
# And convert L3 into a tuple
t3 = tuple(l3)
print(t3)
print(type(t3))<jupyter_output>(0, 'one', 'two', 3, 4, 5.5, 6)
<class 'tuple'><jupyter_text>Python methods for working with tuples<jupyter_code># len: lenght
len(t3)
# concatenation
t4 = t2 + t3
t4
# membership
3 in t3
10 in t3
# count: returns the number of times a specific value occurs in a tuple
t4.count(4)
# index(): Searches the tuple for a specified value and returns the position of where it was found
t4.index(3)
# index: searches the tuple for a specified value and returns the first position of where it was found
t4.index('one')
# Tuples may be nested
t5 = ('123', 'hello', t4)
t5
len(t5)
type(t5[0])
type(t5[2])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11078431372549019, 'fraction_numerical': 0.03333333333333333, 'mean_word_length': 4.831428571428572, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '376587', 'license': 'MIT', 'n_tokens_mistral': 745, 'n_tokens_neox': 718, 'n_words': 281, 'path': '01-Basic Python/04-Tuples.ipynb', 'repo_name': 'DavidIzadaR/Introduction-to-Data-Science', 'size': '2040'}