Python Tuples
What is a Tuple in Python?
A tuple is one of Python’s built-in data types used to store a collection of items. Tuples are:
- Ordered – elements have a fixed position.
- Immutable – once defined, values cannot be changed.
- Allow duplicates – elements can repeat.
- Use parentheses
()
or thetuple()
constructor. - A single-element tuple requires a trailing comma.
Tuples are written with round brackets ()
and items are separated by commas.
How to Create a Tuple
Example 1: Basic Tuple
my_tuple = (1, 2, 3)
print(my_tuple)
Output:
(1, 2, 3)
Example 2: Tuple with Different Data Types
mixed_tuple = (101, "Alice", 3.14, True)
print(mixed_tuple)
Output:
(101, 'Alice', 3.14, True)
Example 3: Tuple Without Parentheses (Tuple Packing)
packed_tuple = 5, 10, 15
print(packed_tuple)
Output:
(5, 10, 15)
Example 4: Single Element Tuple
single_element = ("Python",) # Note the comma
print(type(single_element))
Output:
<class 'tuple'>
Accessing Tuple Elements
Example 1: Access Using Index
fruits = ("apple", "banana", "cherry")
print(fruits[0])
Output
apple
Example 2: Negative Indexing
fruits = ("apple", "banana", "cherry")
print(fruits[-1])
Output:
cherry
Slicing Tuples
You can use slicing to retrieve a subset of the tuple.
Example 1:
colors = ("red", "green", "blue", "yellow")
print(colors[1:3])
Output:
('green', 'blue')
Immutability of Tuples
You cannot change the values of a tuple after creation.
Example 1:
numbers = (1, 2, 3)
numbers[0] = 100 # Trying to modify
Output:
TypeError: 'tuple' object does not support item assignment
Adding Items to a Tuple
You can’t modify a tuple directly, but you can create a new tuple by concatenation.
Example 1:
t1 = (1, 2)
t2 = (3,)
new_tuple = t1 + t2
print(new_tuple)
Output:
(1, 2, 3)
Removing Items from a Tuple
You cannot remove individual items from a tuple, but you can delete the whole tuple.
Example 1:
t = (10, 20, 30)
del t
# print(t) would now raise an error
Looping Through a Tuple
Example 1:
colors = ("red", "green", "blue")
for color in colors:
print(color)
Output:
red
green
blue
Tuple Methods
Tuples only have two built-in methods: count()
and index()
.
Example 1:
nums = (1, 2, 3, 2, 2, 4)
print(nums.count(2)) # How many times 2 appears
print(nums.index(3)) # Index of first occurrence of 3
Output:
3
2
Tuple Unpacking
Tuple unpacking lets you assign values to multiple variables at once.
Example 1:
person = ("John", 25, "Engineer")
name, age, job = person
print(name)
print(age)
print(job)
Output:
John
25
Engineer
Tuple vs List Comparison
Feature | Tuple | List |
---|---|---|
Syntax | (1, 2, 3) | [1, 2, 3] |
Mutability | Immutable | Mutable |
Methods | Limited (count , index ) | Many (append, pop, etc.) |
Performance | Faster | Slightly slower |
Use Case | Fixed data | Dynamic or changing data |
Practice Exercises
- Create a tuple of 5 elements and print it.
- Access the 3rd item using indexing.
- Use slicing to get the last 3 elements.
- Count how many times
5
appears in(1, 5, 3, 5, 5)
. - Try to change the first element and observe the error.
Conclusion
- Tuples are ordered and immutable collections in Python.
- Defined using round brackets
()
. - Useful when working with fixed sets of data.
- Provide better performance than lists for read-only operations.