Table of Contents
- What is a Tuple?
- Declaring a Tuple
- Accessing elements of a Tuple
- Updating elements of a Tuple
- Deleting a Tuple
- Slicing a Tuple
- Frequently used Tuple methods
What is a Tuple?
Tuple is a sequence of elements. A tuple is immutable, unlike a list in Python. By immutable, we mean that it is not possible to update a tuple after declaring one. The elements inside a tuple can be any Python object such a string, integer etc. Tuples are surrounded by parenthesis.
Declaring a Tuple
Empty Tuple is declared by empty parenthesis.
It is important to note that declaring a tuple consisting a single element requires a comma after the first element.
If we miss out on that comma,
t3 would become an integer because 1 itself is an integer.
Do keep this in mind. In future, a lot of people will judge your basic python knowledge on the above knowledge.
Accessing elements of a Tuple
We use square brackets along with the index which we want to access.
If you try accessing some index that doesn’t exist, Python raises appropriate error. We can also access elements using negative indexing.
Index table indicating positive and negative index values.
Updating elements of a Tuple
We saw in the definition of a tuple that tuples are immutable. It is not possible to update them once declared. but what happens if we try to do so?
If we try to update the element by using index, it raises an error saying that ‘tuple’ object does not support item assignment.
However, if you add mutable elements such as a list inside a tuple, we can mutate them. For example:
A tuple can also be nested, i.e. just like a list inside a tuple, we can have tuples inside a tuple.
However, now that tuples are not mutable, we cannot update the tuple inside a tuple either.
If we really want to update a tuple, we can use that tuple to create another tuple.
Deleting a Tuple
Since tuples are immutable, it is not possible to delete individual elements of a tuple, however, we can delete the whole tuple itself.
Slicing a Tuple
Tuple slicing is similar to string slicing. Using slicing we can extract out elements of any tuple. We have to provide the starting index and the ending index i.e.
It starts from
start and ends right before
end. It won’t print the element at the index
end. As in the example above, we have
n at the 4th index of tuple
t, however, it printed only till
start is out of the range of the tuple, then it prints empty tuple.
If you don’t provide
start, then it will print from
end is not provided, it prints till the last element of the tuple.
And if we don’t provide either of them? You guessed it right! It prints the whole tuple as is.
Frequently used Tuple methods
Depending on the elements inside a tuple, there are certain useful methods we can use with a tuple.
+ to create a new tuple using existing tuples. This is called concatenation.
Note: This is a part of what I learned in an online Open Course Ware offered by MIT on edX. Its for my personal reference & also for those who would like to revisit the course.