What is Tuple in Python
Tuple data type in Python is a collection of various immutable Python objects separated by commas. Tuples are much similar to Python Lists, but they are syntactically different, i.e., in lists we use square brackets while in tuples we use parentheses. In this module, we will learn all about the tuple data type in order to get started with it.
Watch this video on Python:
Following is the list of all topics that we will cover in this module.
So, without further delay, let’s get started.
Advantages of Tuples in Python over Lists
The main difference between Python tuples and lists is that the elements of a tuple cannot be changed once they are assigned; whereas, the elements of a list can be changed.
As tuples and lists are quite similar to each other, they are often used in similar kinds of situations. Although, a tuple in Python has a bunch of advantages over lists. Following are some of the main advantages:
- Iteration in a tuple is faster as compared to lists since tuples in Python are immutable.
- Tuples are generally used for different Python Data Types; whereas, lists are used for similar data types.
- Whenever, we need to make sure that the data remains unchanged and write protected, Python tuple is the best option.
Go through this Python Course in London to get a clear understanding of Python!
Creating a Tuple in Python
A Python tuple is created using parentheses around the elements in the tuple. Although using parentheses is only optional, it is considered a good practice to use them.
Elements in the tuple can be of different data types or of the same data type. A tuple in Python can have any number of elements.
Following is the code block that shows how to create a tuple:
tup1 = (‘Intellipaat’, ‘ Python’, ‘tutorial’) tup2 = 1,2,3,4 Print (tup1) print (tup2) Output: (‘Intellipaat’, ‘ Python’, ‘tutorial’) (1,2,3,4)
Accessing Python Tuple Elements
We can use three different ways of accessing elements in a tuple, that is, Indexing, reverse indexing, and using the slice operator.
Indexing of Tuples in Python
To access an element of a tuple, we simply use the index of that element. We use square brackets around that index number as shown in the example below:
tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’) print (tup1) Output: Intellipaat
Learn more about Python from this Python Training in New York to get ahead in your career!
Reverse Indexing of Tuples in Python
Much similar to regular indexing, here, we use the index inside the square brackets to access the elements, with only one difference, that is, we use the index in a reverse manner. Meaning, the indexing of the elements would start from the last element. Here, 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.
tup1= (‘Intellipaat’, ‘Python’, ‘tutorial’) print (tup1[-1]) Output: tutorial
Slicing Operator of Tuples in Python
Using the slicing operator to access elements is nothing new, as we have seen this in previous modules as well. As the name suggests, we will slice, that is, extract some elements from the tuple and display them. To do this, we use a colon between the index from where we want to start slicing and the index till where we want to perform it.
Following code block is an example to show how to access elements using the slicing operator.
tup3 = (1,2,3,4,5,6) tup3[1:] tup3[2:4] Output: (2, 3, 4, 5, 6) (3, 4)
Interested in learning Python? Check out the Python Training in Sydney!
Performing Operations in Tuples in Python
Following is the list of some of the most frequently used operations in a Python tuple along with their descriptions and examples.
Deleting Python Tuple Elements
Since a tuple in Python is an immutable data type in Python, deleting particular elements in a tuple is not possible. But, the whole tuple can be deleted using the del keyword as shown in the following example:
tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’) print (tup1) del tup1 print (tup1) Output: (‘Intellipaat’, ‘Python’, ‘tutorial’) Traceback (most recent call last): File “”, line 1, in NameError: name ‘tup1’ is not defined
Modifying Elements in a Python Tuple
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:
tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’) tup2 = (1,2,3) tup3 = tup1 + tup2 print (tup3) Output: (‘Intellipaat’, ‘Python’, ‘tutorial’,1,2,3)
With this, we come to an end of this module of Python Tutorial. Now, if you want to know why Python is the most preferred language for data science, you can go through this Python for Data Science blog.
Further, check out our offers for Python training Course and also refer to the trending Python interview questions prepared by the industry experts.