In Python, a tuple is similar to a list, but it is immutable, meaning that its elements cannot be changed after it is created. Tuples are defined using parentheses, with elements separated by commas.
Here is how you can initialize a tuple:
myTuple = (10, 20, 30, 40, 50) # A tuple of integers
anotherTuple = ('apple', 'banana', 'cherry') # A tuple of strings
- Immutable: Once a tuple is created, you cannot add, remove, or modify its elements.
- Ordered: Like lists, tuples maintain the order of elements.
- Indexable and Slicable: You can access elements by their index and slice tuples.
- Allow Duplicate Elements: Tuples can contain duplicate elements.
Since tuples are ordered and indexable, you can access elements using their index. The first element has an index of 0.
firstElement = myTuple[0] # Accessing the first element
lastElement = myTuple[-1] # Accessing the last element
Tuples have fewer methods compared to lists due to their immutability. The most commonly used methods are:
myTuple.count(20) # Returns the number of times 20 appears in the tuple
myTuple.index(40) # Returns the index of the first occurrence of 40
You can unpack a tuple into several variables, which is a convenient way to extract its values.
a, b, c = anotherTuple
print(b) # 'banana'
- Performance: Tuples can be slightly faster than lists for certain operations due to their immutability.
- Use Cases: Tuples are commonly used for data that should not change, like coordinates or dates.