Menu Close

Python Tuple Tutorial

Python tuple tutorial

Hello Python programmers, In this tuple tutorial, we are going to learn all about Python tuples. In this guide, we will see what is Python tuple, How to create and use them, and also we will see some important functions of the tuple.

What is Python Tuple?

A tuple in python is a collection of elements that is unchangeable or immutable and order data type, python tuple is written within the round bracket, and items in the Python tuple are separated by commas.

Python tuple allows duplicate elements that means you can insert duplicate elements in the tuple. You can access Python tuple items using an index number.

Here we have a Python tuple named myTuple.

myTuple = (1, 2, 3, 4, 5, 6, 7, 7)

If you want to check the data type of python tuple then you need to use type() built-in function.


Create Python Tuple

Tuple in python creates using round bracket ( () ).

product = ('Phone', 'Laptop', 'Desktop', 'Earphone', 'Headphone', 'Charger')


('Phone', 'Laptop', 'Desktop', 'Earphone', 'Headphone', 'Charger')

Access Python tuple item using index number

If you want to access the tuple element then you can use the index number of the particular item. The index number starts from 0 which means the index number of the first element is 0.

product = ('Phone','Laptop','Desktop','Earphone','Headphone','Charger')

The output will be:- Desktop

Access Python tuple item using negative index number

You can use a negative index number to access the tuple items. negative indexing start from -1 that means index number of charger id -1.

product = ('Phone', 'Laptop', 'Desktop', 'Earphone', 'Headphone', 'Charger')

The output will be:- Charger

Access Python tuple item with range indexes

Tuple provides a facility to access tuple items within a specific range.
In range, the first number is the start index and the last number refers to the end index number. This process is also called slicing.

product = ('Phone', 'Laptop', 'Desktop', 'Earphone', 'Headphone', 'Charger')


('Desktop', 'Earphone', 'Headphone')

Note:- In the result set end index number item is not included.

If you want to search items from the end of the tuple then you can use a range of negative indexes.

product = ('Phone','Laptop','Desktop','Earphone','Headphone','Charger')



Change tuple value

Sometimes you need to change the value of the tuple then you can change using the tuple element index number.

product = ('Phone','Laptop','Desktop','Earphone','Headphone','Charger')
product[3] = 'Speakers'


Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\", line 2, in <module>
    product[3] = 'Speakers'
TypeError: 'tuple' object does not support item assignment

If you will try to change a tuple item, you will get an error because the tuple is unchangeable.

Tuple Length

If you want to calculate the length of Python tuple length then you can use len() built-in function.

product = ('Phone', 'Laptop', 'Desktop', 'Earphone', 'Headphone', 'Charger')

The output will be:- 6

Loop through a Tuple

You can also use for loop to iterate the tuple items.

products = ('Phone','Laptop','Desktop','Earphone','Headphone','Charger')
for product in products:



Delete tuple

You can not remove items from a tuple because the tuple is unchangeable but you can delete the tuple completely using a del keyword.

products = ('Phone','Laptop','Desktop','Earphone','Headphone','Charger')
del products


Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\", line 3, in <module>
NameError: name 'products' is not defined

Join two Tuple

You can join two tuples using the + operator.

a = (1,2,4,5,6)
b = (7,8,9,10)

result = a + b

Tuple Constructor

You can create a tuple using a tuple constructor.

tuple1 = tuple((1, 2, 3, 4, 5, 6, 7, 8, 9))


(1, 2, 3, 4, 5, 6, 7, 8, 9)
<class 'tuple'>

Python Tuple methods

Tuples in python provide only two methods which are count() and index().

Python tuple count() method

The tuple count() method is used to count the numbers of times, specific items present in the tuple.

x = ('P',  'r',  'o',  'g',  'r',  'a',  'm',  'm',  'i',  'n',  'g')

The output will be:- 2

Python tuple index() method

The tuple index() method is used to fetch the index number of items in the tuple.

x = ('P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g')

The output will be:- 6

Note:- If the tuple contains a duplicate item, Then the tuple index method returns the index number of the first matching item

Python tuple to list conversion

To convert python tuple to list, you have to use the list() function in python.

x = ('P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g')
result = list(x)


['P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']


In this article, you have learned all about Python tuple using various examples. tuple in python is a collection that is ordered and unchangeable or immutable.

Once you have created the tuple in python, then you can not change the value of a specific index in the tuple or you can’t add the item in the tuple.

I hope this will help you, if you like this article, please share it with your friends who want to learn Python programs from scratch to advanced.

For more information:- Click Here

Python Keywords Tutorial
Python f-string Tutorial

Related Posts