Tuple datatype in Python is collection of various immutable Python objects separated by commas. Tuples are much similar to Lists in Python but they are syntactically different, meaning that, in lists we use square brackets while in Tuple we use parenthesis. In this module, we will learn all about Tuple datatype in order to get started with it, following is the list of all the topics that we will cover, in case you want to jump to a specific one.
So, without any further delay, let’s get started.
The main difference between Tuple ad Lists is that the elements of tuple cannot be changed once they are assigned, whereas, the elements of the list can be changed.
As Tuple and lists are quite similar to each other, that are often used in similar kinds of situations. Although, Tuple datatype in Python has a bunch of advantages over Lists. Following are some of the main advantages:
A tuple is created using parenthesis around the elements in the tuple. Although parenthesis is only optional but it is considered good practice to use them.
The elements in the tuple can be of different datatype or same datatype as well. A tuple can have any number of elements.
Following is the code block to show how to create a tuple:
We can use three different ways of accessing elements in Tuple, that is, Indexing, reverse indexing and slice operator.
To access an element of Tuple, we simply use the index of that element. We use square brackets around that index number as shown in the example below:
Much similar to regular indexing, we use the index inside of the square brackets to access the elements, the only difference is that, we use index in reverse manner. Meaning, the indexing of the element would start from the last element. And we use indexes as -1,-2, -3 and so on, where -1 represents the last element.
Following code block is an example to access elements using Reverse Indexing.
Using slicing operator to access elements is nothing new in Tuples, we have seen this in previous modules as well. As the name suggests, we will slice, that is, extract some elements from tuple and display them. To do this, we use colon between the index from where we want to start slicing and to the index till where we want to perform slicing.
Following code block is an example to show how to access elements using slicing operator.
Following is the list of some of the most frequently used operations in tuple along with their descriptions and examples.
Since Tuple is an immutable Datatype of Python, deleting particular elements in Tuple is not possible. But the whole Tuple can be deleted using the del keyword as shown in the following example:
Again, since tuple is immutable, it is impossible to change or modify the value of a particular element. Although we can take some portion of an existing tuple and create a new tuple using the concatenating operator, as shown in the example below:
With this we come to the end of this module, the next module will highlight Python Sets in detail. See you there!Previous Next
Download Interview Questions asked by top MNCs in 2019?