Python Tuple

1. Creating a Tuple

empty_tuple = ()
single_element = (10,)   # Comma required for single-element tuple
numbers = (1, 2, 3, 4)
mixed = (10, "Python", True)

print(empty_tuple)
print(single_element)
print(numbers)
print(mixed)

Tuples are ordered, immutable collections used to store fixed data.


2. Accessing Tuple Elements

colors = ("red", "green", "blue")

print(colors[0])    # First element
print(colors[-1])   # Last element

Like lists, tuples support indexing.


3. Slicing Tuples

Tuple slicing returns a new tuple.


4. Tuple Immutability

Tuple elements cannot be modified after creation.


5. Tuple Packing and Unpacking

Convenient for multiple assignments.


6. Nested Tuples

Tuples can contain other tuples.


7. Tuple Length and Membership

Supports standard sequence operations.


8. Iterating Through a Tuple

Tuples are iterable like lists.


9. Converting Between Tuples and Lists

Used when mutability is required temporarily.


10. Tuple as a Return Value

Functions can return multiple values as tuples.


Last updated