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.

print(typle(myTuple))

Create Python Tuple

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

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

Output

('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')
print(product[2])

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')
print(product[-1])

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')
print(product[2:5])

Output

('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')
print(product[-4:-1])

Output

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

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'
print(product)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", 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')
print(len(product))

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:
	print(product)

Output

Phone
Laptop
Desktop
Earphone
Headphone
Charger

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
print(products)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 3, in <module>
    print(products)
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
print(result)

Tuple Constructor

You can create a tuple using a tuple constructor.

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

Output

(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')
print(x.count('m'))

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')
print(x.index('m'))

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)
print(result)

Output

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

Conclusion

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