A tuple is an ordered, immutable (unchangeable) collection in Python. Tuples are similar to lists, but unlike lists, their elements cannot be modified after creation. Tuples allow duplicate values and can store different data types.
1. Creating Tuples
Tuples are defined using parentheses ( )
, with elements separated by commas.
Example: Creating Tuples
# Empty tuple
empty_tuple = ()
# Tuple with numbers
numbers = (1, 2, 3, 4, 5)
# Tuple with strings
fruits = ("apple", "banana", "cherry")
# Tuple with mixed data types
mixed = (1, "hello", 3.14, True)
# Tuple with nested tuple
nested_tuple = ((1, 2), (3, 4))
print(numbers)
print(fruits)
print(mixed)
print(nested_tuple)
Output:
(1, 2, 3, 4, 5)
('apple', 'banana', 'cherry')
(1, 'hello', 3.14, True)
((1, 2), (3, 4))
2. Single-Element Tuples
To define a tuple with one element, add a comma after the value.
Incorrect (Not a Tuple)
single = (5) # This is just an integer
print(type(single)) # Output: <class 'int'>
Correct (Tuple)
single = (5,) # A comma makes it a tuple
print(type(single)) # Output: <class 'tuple'>
3. Accessing Tuple Elements
(a) Indexing (Zero-based Indexing)
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # First element
print(fruits[1]) # Second element
print(fruits[-1]) # Last element
Output:
apple
banana
cherry
(b) Slicing
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4]) # Elements from index 1 to 3
print(numbers[:3]) # First 3 elements
print(numbers[2:]) # Elements from index 2 to end
print(numbers[-3:]) # Last 3 elements
Output:
(20, 30, 40)
(10, 20, 30)
(30, 40, 50)
(30, 40, 50)
4. Tuple Immutability
Tuples cannot be modified after creation. The following actions will cause an error:
Example: Trying to Modify a Tuple
numbers = (1, 2, 3, 4)
numbers[1] = 10 # This will cause an error
Error:
TypeError: 'tuple' object does not support item assignment
5. Looping Through a Tuple
(a) Using for
Loop
fruits = ("apple", "banana", "cherry")
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
(b) Using enumerate()
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple
1 banana
2 cherry
6. Tuple Methods
Method | Description |
---|---|
count(x) | Returns the number of times x appears in the tuple |
index(x) | Returns the first index of x |
Example: Using Tuple Methods
numbers = (1, 2, 3, 2, 2, 4, 5)
print(numbers.count(2)) # Count occurrences of 2
print(numbers.index(3)) # Find index of first occurrence of 3
Output:
3
2
7. Checking if an Item Exists
fruits = ("apple", "banana", "cherry")
if "banana" in fruits:
print("Banana is in the tuple!")
Output:
Banana is in the tuple!
8. Tuple Packing and Unpacking
(a) Tuple Packing (Assigning Multiple Values to a Tuple)
person = ("Alice", 25, "Engineer")
print(person)
Output:
('Alice', 25, 'Engineer')
(b) Tuple Unpacking (Extracting Values from a Tuple)
name, age, job = person
print(name)
print(age)
print(job)
Output:
Alice
25
Engineer
9. Joining Tuples
Tuples can be combined using the +
operator.
Example: Concatenating Tuples
a = (1, 2, 3)
b = (4, 5, 6)
c = a + b
print(c)
Output:
(1, 2, 3, 4, 5, 6)
10. Converting Between Tuples and Lists
Since tuples are immutable, you can convert them into lists to modify them.
Example: Convert Tuple to List
numbers = (1, 2, 3, 4)
numbers_list = list(numbers) # Convert tuple to list
numbers_list.append(5) # Modify list
numbers = tuple(numbers_list) # Convert back to tuple
print(numbers)
Output:
(1, 2, 3, 4, 5)
11. Comparing Tuples vs Lists
Feature | Tuples | Lists |
---|---|---|
Mutable? | No | Yes |
Ordered? | Yes | Yes |
Duplicates Allowed? | Yes | Yes |
Performance | Faster | Slower |
Use Case | Fixed data | Dynamic data |
- Use tuples when the data should remain constant.
- Use lists when you need to modify data.
12. Deleting a Tuple
You cannot delete individual elements, but you can delete the entire tuple.
Example: Deleting a Tuple
numbers = (1, 2, 3)
del numbers # Deletes the tuple completely