Python Foundation
Tuples in Python
What is a Tuple?
A tuple is an ordered and immutable collection of items in Python.
Once a tuple is created, you cannot change, add, or remove its items.
Python
point = (10, 20)
colors = ("red", "green", "blue")
mixed = ("Kundan", 25, 5.9)
print(point)
print(type(point))
Output
(10, 20) <class 'tuple'>
Key Characteristics
- Ordered → Items have a fixed order
- Immutable → Cannot be changed after creation
- Allows duplicate values
- Can store different data types
Creating Tuples
Using Parentheses
Python
t1 = (10, 20, 30)
t2 = ("apple", "banana", "mango")
t3 = () # Empty tuple
print(t1)
print(t2)
Without Parentheses (Tuple Packing)
Python
t4 = 10, 20, 30
print(t4)
print(type(t4))
Output
(10, 20, 30) <class 'tuple'>
Single Item Tuple
Python
single = (5,) # Comma is important
print(type(single))
not_tuple = (5) # This is just an integer
print(type(not_tuple))
Output
<class 'tuple'> <class 'int'>
Indexing and Slicing
Tuples support indexing and slicing just like lists and strings.
Python
data = ("apple", "banana", "mango", "orange", "grape")
print(data[0])
print(data[-1])
print(data[1:4])
print(data[:3])
print(data[::-1])
Output
apple
grape
('banana', 'mango', 'orange')
('apple', 'banana', 'mango')
('grape', 'orange', 'mango', 'banana', 'apple')
Tuples are Immutable
Once a tuple is created, you cannot change its values.
Python
colors = ("red", "green", "blue")
# colors[1] = "yellow" → TypeError
# colors.append("black") → AttributeError
If you try to modify a tuple, Python will raise an error.
Workaround: Convert to List
Python
colors = ("red", "green", "blue")
temp = list(colors)
temp[1] = "yellow"
colors = tuple(temp)
print(colors)
Output
('red', 'yellow', 'blue')
Tuple Methods
Tuples have only two built-in methods.
Python
numbers = (1, 2, 3, 2, 4, 2, 5)
print(numbers.count(2)) # Count occurrences of 2
print(numbers.index(4)) # Find index of 4
print(len(numbers)) # Length of tuple
Output
3 4 7
count() and index() are the only two methods available for tuples.
Tuple Packing and Unpacking
Packing
Python
person = "Kundan", 25, "Hyderabad"
print(person)
Output
('Kundan', 25, 'Hyderabad')
Unpacking
Python
name, age, city = person
print(name)
print(age)
print(city)
Output
Kundan 25 Hyderabad
Using * for remaining values
Python
numbers = (10, 20, 30, 40, 50)
a, b, *rest = numbers
print(a)
print(b)
print(rest)
Output
10 20 [30, 40, 50]
When to Use Tuples?
Use tuples when you want to store data that should not change.
Good Use Cases
- Coordinates →
(x, y) - RGB colors →
(255, 0, 0) - Database records
- Function returning multiple values
- Dictionary keys (because tuples are immutable)
Python
# Function returning multiple values
def get_student():
name = "Kundan"
age = 25
city = "Hyderabad"
return name, age, city
result = get_student()
print(result)
print(type(result))
Output
('Kundan', 25, 'Hyderabad')
<class 'tuple'>
Tuple vs List
| Feature | List | Tuple |
|---|---|---|
| Mutable | Yes | No |
| Syntax | [ ] |
( ) |
| Methods | Many (append, remove...) | Only count() and index() |
| Speed | Slower | Faster |
| Memory | More | Less |
| Use case | Data that changes | Fixed data |
Summary – Tuples in Python
- Tuple is an ordered and immutable collection.
- Created using parentheses
(). - Supports indexing and slicing.
- Only two methods:
count()andindex(). - Useful for fixed data and returning multiple values from functions.
- Faster and uses less memory than lists.
Next Topic → Sets in Python
Tuples in Python
Learn ordered, immutable collections and when to use them instead of lists.
Select any topic from the left sidebar to begin.
Start Learning →
