- Create a tuple: Use parentheses to create a tuple, with elements separated by commas.
Example: my_tuple = (1, 2, 3, 4, 5)
- Accessing elements: Use an index to access the elements in a tuple, where the index starts at 0.
Example: print(my_tuple[0])
- Slicing: You can use the slicing operator to retrieve a subset of elements from a tuple.
Example: print(my_tuple[1:3])
- Merge tuples: You can merge two tuples using the plus operator.
Example: new_tuple = my_tuple + (6, 7, 8)
- Calculate the number of elements: You can use the len() function to determine the number of elements in a tuple.
Example: print(len(my_tuple))
- Search for elements: You can use the “in” keyword to check if a tuple contains a specific element.
Example: print(3 in my_tuple)
- Tuple unpacking: allows the elements of a tuple to be unpacked into multiple variables.
Example: a, b, c, d, e = my_tuple
- Immutability: Tuples are immutable, meaning that the elements within a tuple cannot be modified.
Example: my_tuple[0] = 10 # Will result in an error.
- Remove a tuple: you can use the del keyword to delete the entire tuple.
Example: del my_tuple
✖