A Tuple is like a List which is a member of data types called sequences.
Tuples and Lists are almost similar to each other, the only difference between them is that unlike Lists,
Tuples are immutable, i.e., elements in a Tuple cannot be changed.
Tuples have some advantages over Lists even though they are very similar to each other.
Few advantages of Tuples over Lists are given below: –
- As Tuples are immutable, they are more secure than Lists. This prevents accidental modification of Tuple members.
- The iterator reads a Tuple faster than a List because of their immutable property, this saves some time in execution process.
- Dictionary is another Python data type and it requires one of its component values to be immutable and Tuple can be used for this. On the other hand, a List is mutable and thus cannot be used.
Creating A Tuple
Tuples are created by placing all the elements in enclosed small brackets () separated by commas.
A Tuple can contain multiple other Tuples and Lists inside it, thus creating a nested system.
Similarly, a Tuple can also be created by enclosing all the items in a constructor called the Tuple constructor.
Accessing Items in a Tuple
Each element in a Tuple is associated with its own index number and can be accessed by using that number.
The number is put inside closed square brackets [ ] with the Tuple’s name.
Nested elements can be accessed using the same indexing method.
Elements in a string, i.e., sub-strings can also be accessed by this method. Let see how
The number which comes in the first square brackets represents the element of the main Tuple and the second brackets’ index number indicates to the sub-members of the nested members.
Python interpreter will show an error if there are no nested members present and still this syntax is used.
Operation on Tuples
You can also use Tuples as operands to different operators like the addition (+) and the multiplication (*) operators.
Concatenation can be performed on two tuples by using the addition operator. Also, with the help of a multiplication operator, you can make a Tuple to repeat itself just like Lists.
Similarly, to make a Tuple repeat itself, we have to do this:
If you check the data type of a Tuple by using the type() method, Python will obviously will identify as a Tuple but when you store only a single element in it, the output will be the data type of that element.
Python identified it as an integer because Python just ignored the small brackets because they are also used to define some operator’s priority in mathematical expressions.
This also applies for all the data types. To make Python “believe” that the value stored in the variable is actually a Tuple, the integer (or any other data type) should be followed by a Comma.
Looping Through a Tuple
The for loop can be used to loop through a Tuple like this: –
Elements in a Tuple can be accessed by their unique index number. This is called Indexing.
The element of the first element is zero which is similar to Lists and Strings. Let’s take an example
If you want to access some element on an index number that does not exist, Python interpreter will show an error.
Similar to Lists, the elements in the nested data types can also be accessed in Tuples.
Also, if there are no elements in a nested member, the code won’t run.
Indexing can also be done using negative numbers.
This works in a whole opposite manner, and instead of starting with 0, the index number starts with the negative of the last positive index number and the index of the last element becomes -1.
Have an example to understand this much better: –
This is very useful when you have a very long Tuple and you don’t know the size of it. With the help of negative indexing you can access some last elements in the Tuple.
Slicing is used to access a range of elements in a sequence, in this case the sequence is a Tuple.
This method can also be used to print the whole Tuple in a reverse order and also to print some parts of it in the reverse order.
Updating and Deleting a Tuple
Tuples are immutable which means that they cannot be changed.
Updating them simply by using the assignment operator like this will result in an error: –
However, Tuples can be converted into lists then can be updated or changed.
After the updating is done, you can convert the List back into a Tuple.
Also, you cannot delete elements from a Tuple. However, the whole Tuple can be deleted by using the del function.
index() – Searches the specific element in the Tuple and returns it index number.
count() – It returns the times an element occurs in the Tuple.