05 Tuples
05 Tuples
1) Python Tuple:
i) Python Tuple
ii) Access Tuple
iii) Update Tuple
iv) Unpack Tuple
v) Loop Tuple
vi) Join Tuple
vii) Tuple Methods
Tuple
Tuple is one of 4 built-in data types in Python used to store collections of data, the other
3 are List, Set, and Dictionary, all with different qualities and usage.
Tuples can store heterogeneous data types (e.g., integers, strings, other tuples).
Once created, elements cannot be added, removed, or
modified (immutability).
Tuples are hashable (if all elements are hashable) and can be used as keys in
dictionaries.
Example:
Theory:
Similar to lists, tuples use zero-based indexing and slicing.
Examples:
Theory:
Tuples are immutable, so you cannot modify elements directly. To "update" a tuple:
Examples:
iv) Unpack Tuple
Theory:
Unpacking assigns tuple elements to variables in a single line.
Examples:
Theory:
Use loops to iterate over tuple elements:
Examples:
Theory:
Combine tuples using:
+ operator: Concatenation.
* operator: Repetition.
zip(): Merge tuples element-wise.
Examples:
Theory:
Tuples have only two built-in methods:
Examples:
Key Takeaways:
Use Cases: Ideal for fixed data (e.g., coordinates, database records).