Python Tuples: Immutable Sequences
1 min readTuples are similar to lists but with an important difference: they are immutable. Once created, you cannot modify a tuple.
**Creating Tuples**
Tuples are defined using parentheses `()`:
coordinates = (10.0, 20.0)
person = (“Alice”, 30)
**Accessing Tuple Items**
You can access items in a tuple using indices:
print(coordinates[0]) # Output: 10.0
**Tuple Operations**
Tuples support operations similar to lists, like indexing and slicing, but you cannot change their contents:
sliced_tuple = person[0:1]
print(sliced_tuple) # Output: (“Alice”,)
**Concatenation and Repetition**
You can concatenate and repeat tuples:
combined = coordinates + person
repeated = person * 2
**Example**:
print(combined) # Output: (10.0, 20.0, “Alice”, 30)
print(repeated) # Output: (“Alice”, 30, “Alice”, 30)
**Conclusion**
Tuples are useful when you need a fixed set of values that should not be changed. They are often used in function return values and for immutable data structures.