Hello Python programmers, In this tuple tutorial, we are going to learn all about Python tuple. 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?
- Create Python Tuple
- Access Python tuple item using index number
- Access Python tuple item using negative index number
- Access Python tuple item with range indexes
- Change tuple value
- Tuple Length
- Loop through a Tuple
- Delete tuple
- Join two Tuple
- Tuple Constructor
- Python Tuple methods
- Python tuple to list conversion
- Conclusion
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 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 create 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])
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])
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 not include.
If you want to search items from end of the tuple then you can use 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 tuple then you can change using 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 tuple item, you will get an error because 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))
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 tuple using + 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 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
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'))
Output will be:- 2
Python tuple index() method
Tuple index() method is used to fetch the index number of item in the tuple.
x = ('P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g')
print(x.index('m'))
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 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 program from scratch to advanced.
For more information:- Click Here
Very nice article, keep continuing.