Introduction to Tuple Data Type in python:
In this article we are going learn python Tuple data type in detail. Topics which we are going to cover are, What is tuple data type, How to create tuple? How to use it and what are the methods available with tuple date type. Also the basic difference between python list and tuple.
Out of many available python data types, Tuple, String and List data type have one common characteristic.
- All of these three data types belongs to Sequence Data type. Hence they all behave like an ordered sequence of data.
- Also they support operations like sequence Indexing, Slicing etc.
Difference between List and Tuple:
Both the List and Tuple data types are used to store the data in list format. So it might confuse us where to use list and where to use tuple. There is slight difference between them which we need to understand. So the basic difference between the list and tuple is that, List is mutable while the Tuple is immutable.
Individual data item of List or Tuple is called as an element. So in case of Tuple it is not possible to change the tuple element once it is assigned. Which is not the case with the list data type, For the list data type we can edit (Add/Edit/Remove) the list elements after assigning as well.
So the basic definition of tuple is, “It is an ordered sequence of immutable data”. It is basically a list of items/elements which is immutable (Non modifiable) in nature.
How to create a Tuple:
In python Tuple can be created by placing its elements in parenthesis and each element is separated by comma. Tuple may have any number of elements. Also Tuple elements can be of any data type. Like we can have a tuple of numbers, strings or mixed data type.
number_tuple = (10, 20, 30, 40) string_tuple = ('One', 'Two', 'Three') mixed_data_tuple = (8, 'John', 2.8, 65)
So the basic difference between list and tuple is that,
- Tuple is immutable while the list is mutable.
- Tuple is created by placing its elements inside parenthesis “( )” while list elements are placed inside square bracket “[ ]”. You may check the article “List Data type in python” for more information about python list data type.
Accessing the Tuple items/elements:
To perform any operation on tuple we need to have access over each tuple element. To get the access of each tuple element we need to perform tuple indexing. So let’s see first, What is tuple indexing?
Tuple indexing means, For each tuple element we will assign an unique number (Index number). Which enables us to access any tuple element. To access tuple element we just need to specify its index number in specific format.
Positive Tuple indexing:
There are two ways of indexing a tuple in python. First approach is to start the indexing from first element till the last element. This way of indexing a tuple is known as positive tuple indexing. In positive tuple indexing all the elements are assigned with positive index number.
Which means the first element of tuple will always have zero index number. Suppose we are having a tuple with 5 elements in it. Then the first tuple element will have zero index number and the last element will have index number “4”.
Negative Tuple indexing:
On the other side the second approach of tuple indexing is to start the indexing from the last tuple element and proceed towards the first element i.e in reverse direction. This way of indexing is know as negative tuple indexing.
In negative tuple indexing last element of tuple is assigned with “-1” index number. Suppose we are having a tuple of 5 elements in it. Then the last tuple element will have “-1” index number and the first element will have index number “-5”.
Printing the single tuple element:
For printing the single tuple element see the example below.
tuple_variable = ('Red', 8.5, 'Green', 'Blue', 2, 60) print(tuple_variable) #Result Green
To print the single tuple element we have used indexing operator “[ ]” along with index number of element we want to print.
This is how we can print single tuple element. However sometimes we may come across the scenario wherein we may need to print multiple tuple elements. Let’s see tuple slicing for accessing multiple tuple elements.
For accessing multiple tuple elements we have to specify the range of tuple elements separated by colon “:”. It will slice the tuple and will provide us the access of elements in that range. This way of accessing tuple elements is known as the tuple slicing.
tuple_variable = ('Red', 8.5, 'Green', 'Blue', 2, 60) print(tuple_variable[2:5]) #Result ('Green', 'Blue', 2)
In above example we have sliced the tuple from index number 2 to 5. So the format for tuple slicing is as below;
Operations possible on python tuple:
As we already know that python tuple is immutable. Hence once the tuple assigned it can’t be changed afterwards. Due to that, there are very less operations possible with python tuple. However there are still few operation which can be performed on python tuple data type. Let’s see them one by one.
- Delete python tuple:
Since tuple is immutable, It is not possible to delete any of the tuple element. However it is possible to delete the entire tuple itself. We can delete entire tuple using “del” keyword.
tuple_variable = ('Red', 8.5, 'Green', 'Blue', 2, 60) del tuple_variable #Delete the tuple print(tuple_variable)
Even If we execute this program it will show us an error. Since on the second line we have deleted the tuple “tuple_variable” and at the last line we are trying to print the deleted tuple. Therefore it is showing an error.
- Other available methods for python tuple:
Addition of new element or removal of existing tuple element is not possible. However there are following methods available with python tuple.
|Method Available||Result Obtained|
|count(x)||Return the number 'x' elements present in tuple|
|index(x)||Return index of first found 'x' element|
tuple_variable = ('Red', 8.5, 'Green', 'Blue', 2, 60) count_Result = tuple_variable.count('Red') print(count_Result) #Result 1 #Since only one element with value 'Red' is present in tuple. index_result = tuple_variable.index('Blue') print(index_result) #Result 3 #Index number of 'Blue' will be printed.
Also there are few other methods available with python tuple:
|Tuple Functions||Result Obtained|
|len()||Return number of elements present in tuple.|
|max()||Return the largest tuple element.|
|min()||Return the smallest tuple element.|
|tuple()||Convert an iterable (list, string, set, dictionary) to a tuple.|
tuple_variable = (2, 8, 12, 20, 45, 60) print(len(tuple_variable)) print(max(tuple_variable)) print(min(tuple_variable)) #Result 6 #Number of elements in tuple 60 #Maximum number between tuple elements 2 #Minimum number between tuple elements