Python Tuple
Tuples in Python
A tuple is an immutable sequence type in Python, meaning once it’s created, its elements cannot be changed. Tuples are commonly used to group related data and ensure that it remains unchanged throughout the program.
Creating a Tuple
Tuples are defined by placing values inside parentheses ()
separated by commas.
person = ("Alice", 30, "Engineer")
print(person)
Output:
('Alice', 30, 'Engineer')
You can also create a tuple without parentheses:
coordinates = 10.0, 20.0
print(coordinates)
Output:
(10.0, 20.0)
Single-Element Tuples
To define a tuple with only one element, you must include a trailing comma:
single = (42,)
print(type(single))
Output:
<class 'tuple'>
Without the comma, Python will not create a tuple:
not_a_tuple = (42)
print(type(not_a_tuple))
Output:
<class 'int'>
Accessing Elements
You can access tuple elements using their index, just like with lists.
fruits = ("apple", "banana", "cherry")
print(fruits[1])
Output:
banana
Tuples support both positive indexing (from the start) and negative indexing (from the end).
print(fruits[-1])
Output:
cherry
Tuple Unpacking
Tuple unpacking allows assigning elements of a tuple to variables in a single line.
name, age, job = ("Alice", 30, "Engineer")
print(name)
print(age)
print(job)
Output:
Alice
30
Engineer
You can also use the *
operator to capture excess elements:
first, *middle, last = (1, 2, 3, 4, 5)
print(first)
print(middle)
print(last)
Output:
1
[2, 3, 4]
5
Tuples Are Immutable
Tuples cannot be modified after creation. Trying to change an element results in an error:
numbers = (1, 2, 3)
# numbers[0] = 99 # This will raise an error
Output (if executed):
TypeError: 'tuple' object does not support item assignment
However, if a tuple contains mutable elements (like a list), the contents of those elements can still be changed:
mixed = (1, [2, 3], 4)
mixed[1].append(5)
print(mixed)
Output:
(1, [2, 3, 5], 4)
Tuple Methods
Tuples support a limited number of methods:
count(x)
: returns the number of timesx
appearsindex(x)
: returns the index of the first occurrence ofx
colors = ("red", "blue", "red", "green")
print(colors.count("red"))
print(colors.index("green"))
Output:
2
3
When to Use Tuples
Use a tuple when:
- The data should not change.
- You want to ensure data integrity.
- You need to use a sequence as a key in a dictionary (tuples are hashable).
Example: using a tuple as a key in a dictionary:
locations = {
(48.8566, 2.3522): "Paris",
(51.5074, -0.1278): "London"
}
print(locations[(48.8566, 2.3522)])
Output:
Paris
Summary
- Tuples are ordered and immutable sequences.
- Defined with parentheses
()
or just commas. - Support indexing and unpacking.
- Cannot be modified (though mutable elements inside can be changed).
- Limited methods:
count()
andindex()
. - Useful for fixed collections and dictionary keys.
Tuples are a simple yet powerful way to group related, unchangeable data together in Python.
Continue Learning
Python Variables
Popular
### Understanding Variables and Literals in Python Variables and literals are the foundation of any
Personalized Recommendations
Log in to get more relevant recommendations based on your reading history.