<jupyter_start><jupyter_text>TupleTuple is ordered, **inmmutable**, and allows duplicated elements  Tuple Definition  Tuple Definition<jupyter_code># define tuple through parenthesis
mytuple = ("Max", "28", "Boston")
print(f"this is mytuple {mytuple}")

# need comma to define a single element tuple
tuple1 = ("Max")
print(f"the type of tuple1 is {type(tuple1)}")
tuple2 = ("Max",)
print(f"the type of tuple2 is {type(tuple2)}")

# tuple can also be created using the tuple function
tuple3 = tuple(["Max", 28, "Boston"])
print(f"this is tuple3 {tuple3}")<jupyter_output>this is mytuple ('Max', '28', 'Boston')
the type of tuple1 is <class 'str'>
the type of tuple2 is <class 'tuple'>
this is tuple3 ('Max', 28, 'Boston')<jupyter_text>Tuple UnpackingUnpacking tuples means assigning individual elements of a tuple to multiple variables<jupyter_code># exact match
tuple1 = ("Max", 28, "Boston")
name, age, city = tuple1
print(f"this is the assignment to name: {name}")
print(f"this is the assignment to age: {age}")
print(f"this is the assignment to city: {city}")

# multiple matches
tuple2 = (0, 1, 2, 3, 4)
i1, *i2, i3 = tuple2
print(f"this is the assignment to i1: {i1}")
# anything in between i1 and i3
print(f"this is the assignment to i2: {i2}")
print(f"this is the assignment to i3: {i3}")<jupyter_output>this is the assignment to name: Max
this is the assignment to age: 28
this is the assignment to city: Boston
this is the assignment to i1: 0
this is the assignment to i2: [1, 2, 3]
this is the assignment to i3: 4<jupyter_text>Tuple Content Extraction  Tuple Extraction<jupyter_code>tuple1 = tuple(["Max", 28, "Boston"])
element1 = tuple1[1]
print(f"element1 is {element1} with type {type(element1)}")

element2 = tuple1[2]
print(f"element2 is {element2} with type {type(element2)}")

element3 = tuple1[-2]
print(f"element3 is {element3} with type {type(element3)}")

element4 = tuple1[::-1]
print(f"element4 is reversed due to negative step index {element4}")<jupyter_output>element1 is 28 with type <class 'int'>
element2 is Boston with type <class 'str'>
element3 is 28 with type <class 'int'>
element4 is reversed due to negative step index ('Boston', 28, 'Max')<jupyter_text>Tuple Slicing<jupyter_code>a = (1, 2, 3, 4, 5, 6)
print(f"this is a: {a}")

# selection with stepping
b = a[2:5:2]
print(f"this is b: {b}")

# negative stepping (reverse)
c = a[::-1]
print(f"this is c: {c}",)<jupyter_output>this is a: (1, 2, 3, 4, 5, 6)
this is b: (3, 5)
this is c: (6, 5, 4, 3, 2, 1)<jupyter_text>Searching<jupyter_code># counting number of occurrences of each element
tuple1 = ("a", "p", "p", "l", "e")
a = tuple1.count("a")
print(f"the number of a's is {a}")
p = tuple1.count("p")
print(f"the number of p's is {p}")
o = tuple1.count("o")
print(f"the number of o's is {o}")

# find the index of the first occurrence
a_index = tuple1.index("a")
print(f"the index of first a is {a_index}")
p_index = tuple1.index("p")
print(f"the index of first p is {p_index}")<jupyter_output>the number of a's is 1
the number of p's is 2
the number of o's is 0
the index of first a is 0
the index of first p is 1<jupyter_text>List vs. Tuple  Memory ComparisonBoth list and tuple are able to store elements, there are some key differences:* Tuple are immutable, meaning we cannot change the elements inside either by adding or dropping* Tuple takes up less memory space than list, even if they are storing the same elements<jupyter_code>import sys
my_list = [0, 1, 2, "hello", True]
my_tuple = (0, 1, 2, "hello", True)

print(f"the memory size of the list is {sys.getsizeof(my_list)} bytes")
print(f"the memory size of the tuple is {sys.getsizeof(my_tuple)} bytes")<jupyter_output>the memory size of the list is 96 bytes
the memory size of the tuple is 80 bytes<jupyter_text>List Tuple Conversion<jupyter_code># tuple convert to list
tuple1 = ("a", "p", "p", "l", "e")
print(f"the type of tuple1 is {type(tuple1)}")
list1 = list(tuple1)
print(f"the type of list1 is {type(list1)}")

# list convert to tuple
list2 = ["a", "p", "p", "l", "e"]
print(f"the type of list2 is {type(list2)}")
tuple2 = tuple(list2)
print(f"the type of tuple2 is {type(tuple2)}")<jupyter_output>the type of tuple1 is <class 'tuple'>
the type of list1 is <class 'list'>
the type of list2 is <class 'list'>
the type of tuple2 is <class 'tuple'>
{'fraction_non_alphanumeric': 0.13556174558960074, 'fraction_numerical': 0.035051067780872795, 'mean_word_length': 5.460269865067466, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '340002', 'license': 'MIT', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1532, 'n_words': 606, 'path': 'Python Fundamentals - Tuple.ipynb', 'repo_name': 'ChenshuLiu/Python-Fundamentals', 'size': '4308'}