A Tuple is an immutable collection of ordered items.
Items in the tuple can be of any type, including mixed types within the same tuple: strings, numbers, dictionaries, lists, sets, objects, tuples, ...
Tuples allow duplicates.
Items of tuples can be accessed by position (0-based indexing).
Creating basic lists:
Use parentheses (
()) to create a tuple.
Use commas to separate items in the tuple.
Tuples with one item:
Empty tuples:
Nested tuples:
Immutability:
If you try to modify a tuple you get an error:
Tuples packing and unpacking:
Indexing:
Accessing elements using square brackets with zero-based indexing:
Slicing:
Extract parts of a tuple:
Tuple built-in methods:
-
Common operations:
-
Finding elements:
-
Creating tuples:
-
Converting tuples: