Vipin | Tue, 16 Jun, 2020 | 174
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.
Tuple is immutable collection i.e values can't be changed.
Example:-
Creating a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
You can access tuple items by referring to the index number, inside square brackets:
Example:-
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
Example:-
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
Example:-
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
Note: The search will start at index 2 (included) and end at index 5 (not included).
Remember that the first item has index 0.
Specify negative indexes if you want to start the search from the end of the tuple:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1])
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
You can loop through the tuple items by using a for
loop.
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
You will learn more about for
loops in our Python For Loops Chapter.
To determine if a specified item is present in a tuple use the in
keyword:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
To determine how many items a tuple has, use the len()
method:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)
To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.
thistuple = ("apple",) #remember the commma
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
Note: You cannot remove items in a tuple.
Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely.
The del
keyword can delete the tuple completely.
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
To join two or more tuples you can use the +
operator:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
It is also possible to use the tuple() constructor to make a tuple.
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
Python has two built-in methods that you can use on tuples.
Method | Description |
---|---|
count() | Returns the number of times a specified value occurs in a tuple |
index() | Searches the tuple for a specified value and returns the position of where it was found |
Example:-
a = (1,2,3,4,5,6,4)
print(a.index(5)) # return index of 5 i.e. 4
print(a.count(4)) # 2