bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Tuple Datatype in Python

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.

Advantages of Tuple over Lists

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:

  • Iteration in tuple is faster as compared to Lists as a result of the tuples being immutable.
  • Tuples are generally used for different types of datatypes whereas Lists are used for similar datatypes.
  • Whenever you need to make sure that the data remain unchanged and write protected, tuple is the best option.

Creating a Tuple

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:

tup1 = (‘Intellipaat’, ‘ Python’, ‘tutorial’)
tup2 = 1,2,3,4
Print (tup1)
print (tup2)

Output:

(‘Intellipaat’, ‘ Python’, ‘tutorial’)
(1,2,3,4)

Accessing the tuple elements

We can use three different ways of accessing elements in Tuple, that is, Indexing, reverse indexing and slice operator.

Indexing

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:

tup1 = (‘Intellipaat’, ‘Python’, ‘tutorial’)
print (tup1[0])

Output:

Intellipaat

Reverse Indexing

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.

tup1= (‘Intellipaat’, ‘Python’, ‘tutorial’)
print (tup1[-1])

Output:

tutorial

Slicing Operator

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.

tup3 = (1,2,3,4,5,6)
tup3[1:]
tup3[2:4]

Output:

(2, 3, 4, 5, 6)
(3, 4)

Performing Operations in tuple

Following is the list of some of the most frequently used operations in tuple along with their descriptions and examples.

Deleting Tuple Elements

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:

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 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 the end of this module, the next module will highlight Python Sets in detail. See you there!

Further, you can check our offers for Python course and training and also refer to the trending Python inteview questions asked by the industry experts.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python Tuple"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Tuple.