Python Tuple is a collection of objects separated by commas. A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable.
Example:
tup = (10, 20, 30)
print(tup)
print(type(tup))
Output
(10, 20, 30) <class 'tuple'>
Creating a Tuple in Python
They can be created using parentheses, comma-separated values or the built-in tuple() constructor. Parentheses are optional in many cases, but the comma is what makes a collection of values a tuple.
1. Creating a Tuple Using Parentheses
Simplest way to create a tuple is to place comma-separated elements inside parentheses.
Example:
fruits = ("Apple", "Banana", "Cherry")
print(fruits)
print(type(fruits))
Output
('Apple', 'Banana', 'Cherry')
<class 'tuple'>
Explanation:
- Parentheses group the elements, while commas separate them.
- type() function confirms that the variable contains a tuple.
2. Creating an Empty Tuple
An empty tuple is created using a pair of parentheses without any elements.
Example:
tup = ()
print(tup)
print(type(tup))
Output
() <class 'tuple'>
3. Creating a Single-Element Tuple
A single-element tuple requires a trailing comma. Parentheses alone do not create a tuple.
Example:
tup1 = (10,)
tup2 = (10)
print(tup1)
print(type(tup1))
print(tup2)
print(type(tup2))
Output
(10,) <class 'tuple'> 10 <class 'int'>
Explanation:
- Comma in tup1 makes it a tuple containing one element.
- Without the comma, tup2 is simply an integer enclosed in parentheses.
4. Creating a Tuple Without Parentheses
Python also allows tuple creation through comma-separated values without parentheses. This is called tuple packing.
Example:
tup = 10, 20, 30
print(tup)
print(type(tup))
Output
(10, 20, 30) <class 'tuple'>
5. Creating a Tuple Using the tuple() Constructor
This constructor converts an iterable, such as a list, string or range, into a tuple.
Example:
numbers = [10, 20, 30]
tup = tuple(numbers)
print(tup)
Output
(10, 20, 30)
Explanation:
- Each element from the iterable becomes an element in the resulting tuple.
- Passing no argument to tuple() creates an empty tuple.
Accessing Elements of a Tuple
They can be accessed using indexing. Positive indices start at 0, while negative indices start at -1 from the last element.
1. Positive Indexing
It can be accessed by placing the index number in square brackets.
Example:
fruits = ("Apple", "Banana", "Cherry")
print(fruits[0])
print(fruits[1])
print(fruits[2])
Output
Apple Banana Cherry
Explanation:
- First element is accessed using index 0.
- Accessing an index outside the tuple's range raises an IndexError.
2. Negative Indexing
It provides access to elements from the end of a tuple.
Example:
fruits = ("Apple", "Banana", "Cherry")
print(fruits[-1])
print(fruits[-2])
print(fruits[-3])
Output
Cherry Banana Apple
Explanation:
- Index -1 refers to the last element.
- Index -2 refers to the second-last element.
Immutability of Tuples
They are immutable, meaning their elements cannot be reassigned, added or removed after creation. Attempting to modify an element directly raises a TypeError.
Example:
tup = (10, [20, 30], 40)
tup[1].append(50)
print(tup)
Output
(10, [20, 30, 50], 40)
Explanation:
- Tuple's structure remains unchanged.
- Nested list can be modified because lists are mutable.
Tuple Operations in Python
Python supports several operations on tuples, including slicing, concatenation, repetition, membership testing and iteration.
1. Tuple Packing
It's the process of grouping multiple values together into a single tuple variable.
Example:
name = "Geek"
age = 20
city = "Noida"
person = name, age, city
print(person)
Output
('Geek', 20, 'Noida')
Explanation:
- Multiple values are grouped into one tuple using comma-separated assignment.
- Tuple packing is useful for collecting related values into a single object.
2. Tuple Unpacking
Its a process of extracting individual values from a tuple and assigning them to separate variables in a single statement.
Example:
person = ("Geek", 20, "Noida")
name, age, city = person
print(name)
print(age)
print(city)
Output
Geek 20 Noida
Explanation:
- Each variable receives the corresponding element from the tuple.
- Number of variables must match the number of elements unless extended unpacking is used.
Converting a Tuple to a List
Although tuples cannot be modified directly, they can be converted into lists when changes are required.
Example:
tup = (10, 20, 30)
numbers = list(tup)
numbers.append(40)
print(numbers)
Output
[10, 20, 30, 40]
Explanation:
- list() constructor creates a list containing the tuple's elements.
- Resulting list supports modifications such as adding, removing and replacing elements.