A tuple is a collection of items that are stored in a variable. These items are ordered and unchangeable. Tuples are represented with round brackets. Tuple and List are the same in Python except for one point. We can the elements in the list after creation but in a tuple, we cannot. An example of creating a tuple is given below,
mytuple1 = ("apple", "banana", "cherry") mytuple2 = (10, 20, 30) print(mytuple1) print(mytuple2)
Tuple items or elements are separated by commas. Tuple can hold all types of data such as integer, float, and string. It is also possible to store several types of data in a variable. We can create an empty tuple as well. For a single item, we have to put the comma after that. Otherwise, Python will not be considered as a tuple. Examples are given below,
tuple1=() #it is an empty tuple tuple2=(10,"Python",18.5) # several types of data in a variable tuple1 = (100,); # assigning value to the empty tuple print(type(tuple1)) #<class ‘tuple’> print(tuple1) print(tuple2)
There are several benefits of applying a tuple over a list. Those are given below
By the use of index numbers, we can access tuple in Python. The index number starts from zero. We have to use square brackets for indexing. Negative indexing also supports Python. It means starting from the end.
-1 refers to the last element. Examples are given below,
mytuple = (20, 30, 40, 60, 80) print(mytuple) #30 print(mytuple) #80 print(mytuple[-1]) #80 print(mytuple[-3]) #40
We can specify a range of indexes and show outputs. For that, we have to give the initial & end indexes and separated by a colon. Here, the output will be treated as a new tuple. We have to keep in mind that the start index i.e.
1 is included and the last index i.e.
4 is excluded. Negative range indexing also supports here, but it will return the items from index
-5 (included) to index
-2 (excluded). An example is given below,
mytuple = (20, 30, 40, 60, 80, 90, 100) print(mytuple[1:4]) #(30, 40, 60) print(mytuple[-5:-2]) #(40, 60, 80)
We cannot change the elements of tuple after creation. It is the biggest drawback of a tuple. We already said that Tuples are unchangeable or immutable. If we want to change or update, then we have to convert the tuple into a list, then do necessary operations and convert the list back into a tuple. An example is given below
a = (10, 30, 40) b = list(a) b = 20 a = tuple(b) print(a) #(10, 20, 30)
for loop, we can loop through the tuple items. We can print all the items as well. An example is given below
a = (10, 30, 40) for i in a: print(i)
In this case, we have to write '
if statement' and use the membership operator ‘
in’. An example is given below
a = (10, 20, 30, 40) if 20 in a: print("Yes")
We can measure the length of a tuple in Python. We have to use the
len() function to compute that. An example is given below
a = (10, 20, 30, 40) print(len(a)) #4
We cannot add or delete any items into an existing tuple. A tuple update is not possible because it is unchangeable. If it is necessary, then we have to convert the tuple into a list, then add items and convert the list back into a tuple. To delete the entire tuple, we have to use the ‘
a1 = (10, 20, 30, 40) del a1 print(a) # Error: a1 is not defined. Because the tuple is no longer exists