A tuple in Python is an ordered collection of multiple values stored in a single variable. Tuples are similar to lists, but they are immutable, which means their elements cannot be changed after the tuple is created.
Tuples are written using parentheses ().
mytuple = ("apple", "banana", "cherry")
print(mytuple)Output:
('apple', 'banana', 'cherry')Creating Tuples
A tuple can contain values of different data types.
mytuple = ("Python", 10, 20.5, True)
print(mytuple)You can also create an empty tuple:
mytuple = ()To create a tuple with only one element, a comma is required:
mytuple = ("apple",)
print(type(mytuple))Without the comma, Python treats it as a normal value:
mytuple = ("apple")
print(type(mytuple))You can also create a tuple using the tuple() constructor:
mytuple = tuple(("apple", "banana", "cherry"))
print(mytuple)Accessing Elements
You can access tuple elements using their index number. Python uses zero-based indexing, so the first element has index 0.
fruits = ("apple", "banana", "cherry")
print(fruits[0])
print(fruits[1])Output:
apple
bananaYou can also use negative indexing to access elements from the end.
print(fruits[-1])Output:
cherryIndexing and Slicing
Indexing is used to access a specific element, while slicing is used to access a range of elements.
numbers = (10, 20, 30, 40, 50)
print(numbers[2])Output:
30To slice a tuple, use the following syntax:
tuple[start:end]For example:
print(numbers[1:4])Output:
(20, 30, 40)The start index is included, but the end index is not included.
You can also use a step:
print(numbers[0:5:2])Output:
(10, 30, 50)To reverse a tuple, use:
print(numbers[::-1])Output:
(50, 40, 30, 20, 10)Tuple Packing
Tuple packing means putting multiple values into a single tuple.
student = "Rahul", 21, "Delhi"
print(student)Output:
('Rahul', 21, 'Delhi')Python automatically packs the values into a tuple.
Parentheses can also be used:
student = ("Rahul", 21, "Delhi")Tuple Unpacking
Tuple unpacking allows you to assign the values of a tuple to separate variables.
student = ("Rahul", 21, "Delhi")
name, age, city = student
print(name)
print(age)
print(city)Output:
Rahul
21
DelhiThe number of variables should normally match the number of elements in the tuple.
You can also use * to collect multiple elements:
numbers = (10, 20, 30, 40, 50)
a, *b = numbers
print(a)
print(b)Output:
10
[20, 30, 40, 50]Updating and Deleting Tuple
Tuples are immutable, so you cannot directly change an element after creating a tuple.
numbers = (10, 20, 30)
numbers[1] = 50This produces a TypeError.
However, you can create a new tuple and assign it to the same variable:
numbers = (10, 20, 30)
numbers = (10, 50, 30)
print(numbers)Output:
(10, 50, 30)You cannot delete individual elements from a tuple:
numbers = (10, 20, 30)
del numbers[1]This produces an error.
However, you can delete the complete tuple using del:
numbers = (10, 20, 30)
del numbersTuple Methods
Python provides two built-in methods for tuples:
- count()
- index()
count() Method
The count() method returns the number of times a value appears in a tuple.
numbers = (10, 20, 10, 30, 10)
print(numbers.count(10))Output:
3index() Method
The index() method returns the index of the first occurrence of a specified value.
fruits = ("apple", "banana", "cherry")
print(fruits.index("banana"))Output:
1Nested Tuples
A nested tuple is a tuple that contains one or more tuples as its elements.
numbers = ((10, 20), (30, 40), (50, 60))
print(numbers)Output:
((10, 20), (30, 40), (50, 60))You can access elements of a nested tuple using multiple indexes:
print(numbers[1])
print(numbers[1][0])Output:
(30, 40)
30Here, numbers[1] accesses the second inner tuple, while numbers[1][0] accesses its first element.