| id | python-tuple | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| title | Tuple in Python | |||||||||
| sidebar_label | Tuples in Python | |||||||||
| sidebar_position | 9 | |||||||||
| tags |
|
A Tuple is an immutable, ordered collection of elements.
Unlike lists, tuples cannot be changed after creation, which makes them useful for storing fixed collections of data.
Tuples are created using parentheses () or simply commas:
# Empty Tuple
empty_tuple = ()
# Tuple with multiple items
fruits = ("apple", "banana", "cherry")
# Tuple without parentheses (comma-separated)
numbers = 1, 2, 3
# Single-element Tuple (Note the comma!)
single = ("hello",)Important: Without the comma, Python does not recognize it as a tuple:
not_a_tuple = ("hello") # This is a string, NOT a tupleUse indexing just like lists:
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # cherryTuples support slicing operations:
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4]) # (20, 30, 40)
print(numbers[:3]) # (10, 20, 30)
print(numbers[2:]) # (30, 40, 50)Tuples cannot be modified after creation:
fruits = ("apple", "banana", "cherry")
# This will raise an error:
fruits[1] = "mango"Output:
TypeError: 'tuple' object does not support item assignment
This property makes tuples safe for constant data, like coordinates, fixed configurations, etc.
Tuples have only two built-in methods:
| Method | Description |
|---|---|
count(x) |
Counts how many times x occurs in the tuple |
index(x) |
Returns the index of the first occurrence of x |
numbers = (1, 2, 3, 2, 2, 4)
print(numbers.count(2)) # 3
print(numbers.index(3)) # 2Packing: Combining values into a tuple:
data = "John", 25, "Engineer"
print(data) # ('John', 25, 'Engineer')Unpacking: Assigning tuple elements to variables:
name, age, profession = data
print(name) # John
print(age) # 25
print(profession) # EngineerTuples can contain other tuples or collections:
nested = (
(1, 2, 3),
("a", "b", "c"),
(True, False)
)
print(nested[1]) # ('a', 'b', 'c')
print(nested[1][2]) # 'c'| Feature | Tuple | List |
|---|---|---|
| Syntax | (1, 2, 3) |
[1, 2, 3] |
| Mutability | Immutable (cannot change) | Mutable (can change) |
| Methods | count(), index() only | Many built-in methods |
| Use Case | Fixed data, safe storage | Dynamic data, frequent changes |
| Performance | Slightly faster | Slightly slower |
Use tuples when:
- Data should not change.
- You need hashable objects (e.g., as dictionary keys).
- You want to protect data integrity.
Examples:
- Geographic coordinates:
(latitude, longitude) - RGB color codes:
(255, 255, 255) - Database records
Note: Python does NOT have tuple comprehensions. However, you can use a generator expression in parentheses:
gen = (x*x for x in range(5))
print(gen) # <generator object ...>To create a tuple from it, use tuple():
squares = tuple(x*x for x in range(5))
print(squares) # (0, 1, 4, 9, 16)Tuples are a fundamental data type in Python, providing a simple, efficient, and immutable way to store ordered data. Understanding when to choose a tuple over a list is essential for writing clear and robust code.