Courses
Free Courses Interview Questions Tutorials Community
Home /
Tutorial /
Tuple in Python
Tuple in Python
By Manoj 8.4 K Views 17 min read Updated on March 9, 2022
In this module of the Python tutorial, we will learn in detail about the tuples in Python. We will further learn the advantages of
Python tuples over the list data type. This module also highlights topics such as how to create and access tuples in Python, and
toward the end of this module we will also learn about various operations in Python tuple data type.
Become a Certified Professional
Python Tutorial
Constant, Global & Static
Variables
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python While Loop
For Loop in Python
Python Functions -
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Python Arrays
Python Classes and
Objects
Python Modules
Python Dates
Python JSON
Python RegEx
PIP Python
Python File Handling -
How to Create, Open,
Read & Write
Exception Handling in
Python
Enumerate Function in
Python
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.
Following is the list of all topics that we will cover in this module.
Advantages of Tuples in Python over Lists
Creating a Tuple in Python
Tuple length in Python
Accessing Python Tuple Elements
Indexing of Tuples in Python
Reveres Indexing of Tuples in Python
Slicing Operator of Tuples in Python
Performing Operations in Tuples in Python
Modifying Elements in a Python Tuple
Deleting Python Tuple Elements
Difference between list and tuple in python
Python List of Tuples
List to Tuple in Python
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)
Tuple length in Python
To evaluate the length of a tuple of the number of items it has, you can use the len() function
tuple1 = (“python”,“java”,“c”)
print(len(tuple1))
The output will be 3
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[0])
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.
The 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)
Difference between list and tuple in python
The table below explains the differences between lists and tuples in python.
Lists Tuples
Lists are mutable. Tuples are immutable.
Iterations are time-consuming. Iterations are comparatively faster.
To perform operations like insert,
Tuples are better to access elements
delete etc., lists are better.
Lists have many built-in methods Tuples have fewer built-in methods
Lists consume more memory. Tuples consume less memory than lists.
If you want to know why Python is the most preferred language for data science, you can go through this Python Data Scien
ce tutorial.
Python List of Tuples
Here is the python code for creating a list of tuples in python.
list1 = [1,3,8,9]
res = [(val, pow(val,3)) for val in list1]
print(res)
The output will be:
[(1, 1), (2, 8), (5, 125), (6, 216)]
List to Tuple in Python
To typecast to tuple, you can simply use tuple(list_name)
def convert(a):
return tuple(a)
a = [5,10,15,20,25]
print(convert(a))
The output will be: (5,10,15,20,25)
Further, check out our offers for Python training courses prepared by industry experts.
With this, we come to the 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 Courses and also refer to the trending Python interview
questions prepared by the industry experts.
Previous Next
Course Schedule
Name Date
2022-05-07 2022-05-08
Python Course View Details
(Sat-Sun) Weekend batch
2022-05-14 2022-05-15
Python Course View Details
(Sat-Sun) Weekend batch
2022-05-21 2022-05-22
Python Course View Details
(Sat-Sun) Weekend batch
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name * Email *
Post Comment
Browse Categories