Python Tuples
Tuples are one of the core data structures in Python.
They are used to store multiple items in a single variable, similar to lists, but with one key difference: tuples are immutable.
This makes tuples faster, safer, and ideal for fixed data.
What Is a Tuple?
A tuple is a collection that:
- Is ordered
- Is immutable (cannot be changed)
- Allows duplicate values
- Can store different data types
python
my_tuple = (10, 20, 30)
print(my_tuple)
python
mixed = (1, "Python", 3.14, True)
print(mixed)
Tuple vs List (Core Difference)
| Feature | List | Tuple |
|---|---|---|
| Syntax | [] | () |
| Mutable | Yes | No |
| Performance | Slower | Faster |
| Use case | Dynamic data | Fixed data |
Creating Tuples
Using Parentheses
python
numbers = (1, 2, 3)
print(numbers)
Tuple with One Item (Important)
A single-item tuple must have a comma.
python
single = (10,)
print(type(single))
Without comma:
python
single = (10)
print(type(single)) # int
Using tuple() Constructor
python
data = tuple((1, 2, 3))
print(data)
python
letters = tuple("Python")
print(letters)
Access Tuple Items (Indexing)
Tuples use zero-based indexing, same as lists.
python
colors = ("red", "green", "blue")
print(colors[0])
python
print(colors[-1])
Access Tuple Items (Slicing)
python
numbers = (0, 1, 2, 3, 4, 5)
print(numbers[1:4])
python
print(numbers[:3])
python
print(numbers[::-1])
Tuple Is Immutable (Key Concept)
Once created, tuple items cannot be changed.
python
numbers = (1, 2, 3)
# numbers[0] = 10 # TypeError
Loop Through a Tuple
Using for Loop
python
fruits = ("apple", "banana", "cherry")
for fruit in fruits:
print(fruit)
Using Index
python
for i in range(len(fruits)):
print(fruits[i])
Check If Item Exists
python
fruits = ("apple", "banana", "cherry")
print("apple" in fruits)
python
print("mango" not in fruits)
Tuple Length
python
numbers = (1, 2, 3, 4)
print(len(numbers))
Allow Duplicate Values
python
values = (1, 2, 2, 3)
print(values)
Nested Tuples
Tuples can contain other tuples.
python
nested = ((1, 2), (3, 4))
print(nested)
python
print(nested[1][0])
Tuple Packing and Unpacking
Packing
python
data = 10, 20, 30
print(data)
Unpacking
python
a, b, c = data
print(a)
print(b)
print(c)
Extended Unpacking
python
numbers = (1, 2, 3, 4, 5)
a, *b, c = numbers
print(a)
print(b)
print(c)
Change Tuple Values (Indirect Way)
Since tuples are immutable, convert to list → modify → convert back.
python
colors = ("red", "green", "blue")
temp = list(colors)
temp[1] = "yellow"
colors = tuple(temp)
print(colors)
Add Items to a Tuple (Indirect Way)
python
numbers = (1, 2, 3)
numbers = numbers + (4,)
print(numbers)
Remove Items from a Tuple (Indirect Way)
python
numbers = (1, 2, 3, 4)
temp = list(numbers)
temp.remove(2)
numbers = tuple(temp)
print(numbers)
Delete a Tuple Completely
python
numbers = (1, 2, 3)
del numbers
Tuple Methods
Tuples have only two built-in methods.
| Method | Description |
|---|---|
count() | Counts occurrences of a value |
index() | Returns index of a value |
count()
python
values = (1, 2, 2, 3)
print(values.count(2))
index()
python
values = (10, 20, 30)
print(values.index(20))
Tuple with Functions (Common Use)
Functions often return tuples.
python
def calculate(a, b):
return a + b, a - b
result = calculate(10, 5)
print(result)
python
sum_val, diff_val = calculate(10, 5)
print(sum_val, diff_val)
When to Use Tuples
Use tuples when:
- Data should not change
- You want faster access
- Data represents a fixed structure
- Returning multiple values from functions
Summary
- Tuples are ordered and immutable
- Defined using
() - Faster and safer than lists
- Support indexing and slicing
- Can store mixed data types
- Only two built-in methods
- Ideal for fixed and read-only data
Tuples are essential for writing clean, efficient, and bug-free Python code.