Tuples and Related Operations
A tuple is an ordered collection of items, similar to a list, except that once a tuple is created its contents cannot be changed. This makes tuples useful for data that should stay fixed.
Creating a Tuple
coordinates = (28.6, 77.2)
colors = ("red", "green", "blue")
Indexing a Tuple
print(coordinates[0])
print(colors[-1])
Tuple Packing and Unpacking
point = (10, 20)
x, y = point
print(x, y)
Common Tuple Methods
numbers = (1, 2, 2, 3, 4, 2)
print(numbers.count(2))
print(numbers.index(3))
Because tuples are immutable, they're often used to represent fixed records, like a single row of coordinates, and can safely be used as dictionary keys, unlike lists.
Coming Up
The next topic covers strings in depth, including the many built-in operations Python provides for searching, formatting, and transforming text data.