Python Tuple Methods Explained with Examples | Beginner Guide

0
Python Tuple Methods Explained with Examples | Beginner Guide
Python Tuple Methods Explained with Examples | Beginner Guide

📑 Table of Contents 


Tuple Definition in Python

Python Tuple: Tuple is a collection of data of different data type. It is used to store tuple of values. A tuple is created by putting of comma-separated values between parentheses(). A tuple is an immutable means tuple items can not be change. Tuple can store elements of different data types. It is a collection which is ordered and not changeable tuple items. It is a allows duplicate items.

Tuple All Functions in Python

Len Function: It can be used to get the length numbers of elements in tuple.
Max Function: It can be used to get maximum value from the tuple. In case of string focus on ASCII value of First letter of tuple items.
Min Function: It can be used to get the minimum value from the tuple.
count Function: It can be used counts the number of occurrence of particular item in a tuple.
Index Function: It can be used to get the index value of particular tuple items.

How to Create Tuple in Python

str_tuple=("Hello","World","Python","Programming","jiocoding")
int_tuple=(10,30,40,80,60)
float_tuple=(22.32,51.6,12.4,93.6)
mixed_tuple=("Jiocoding",2026,40.3)
print("String Tuple Data:",str_tuple)
print("Integer Tuple Data:",int_tuple)
print("Float Tuple Data:",float_tuple)
print("Mixed Tuple Data:",mixed_tuple)
print(type(str_tuple))
print(type(int_tuple))
print(type(float_tuple))
print(type(mixed_tuple))

============OUTPUT============
String Tuple Data: ('Hello', 'World', 'Python', 'Programming', 'jiocoding')

Integer Tuple Data: (10, 30, 40, 80, 60)  
Float Tuple Data: (22.32, 51.6, 12.4, 93.6)
Mixed Tuple Data: ('Jiocoding', 2026, 40.3) 
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>


How to Access Tuple Items in Python

Value of tuple can be accessed using index number. Index number is always an integer value and starts with 0.

Access Tuple Elements Python

data=("Programming","Python","jiocoding")
print("Python Tuple Data is:",data[0])
print("Programming Language:",data[1])
print("Webiste Name is:",data[2])

============OUTPUT============
Python Tuple Data is: Programming

Programming Language: Python
Website Name is: jiocoding

Get Tuple Element by index

data=(5,10,15,20,25,30,35,40,45,50)
print("Data Access using Indexing")
print("Data is:",data[1:6])
print("Data is:",data[::2])

============OUTPUT============
Data Access using Indexing

Data is: (10, 15, 20, 25, 30)
Data is: (5, 15, 25, 35, 45)


Access Value of Tuple Using Negative Index

Access the tuple items using negative index number indexes start from the end of the tuple. Negative index always starts with [-1].

data=("Welcome","Programming","jiocoding") here index of jiocoding [-1] ,Programming [-2] and Welcome[-3].

Negative indexing in Python

data=("Welcome","Programming","jiocoding")
print("First element:",data[-1])
print("Second element:",data[-2])
print("Last element:",data[-3])
============OUTPUT============
First element: jiocoding
Second element: Programming
Last element: Welcome

Tuple Slicing in Python 

Tuple Slicing: Slicing is can be used to extract a portion of the tuple by specifying a range of index's. The basic syntax for slicing a tuple[start:stop] where start is the index of first tuple items and stop is the index of the last item in a tuple.

Tuple Slicing in Python Example

data=("Python","Programming","jiocoding")
print("First element:",data[1:3])
print("Second element:",data[::2])
print("Last element:",data[1::3])
============OUTPUT============
First element: ('Programming', 'jiocoding')

Second element: ('Python', 'jiocoding')
Last element: ('Programming')

How to Access Tuple Elements in Python

names=("Ramesh","Suresh","Mahesh","Naresh","Rajesh")
for name in names:
        print("Student Name is:",name)
============OUTPUT============
Student Name is: Ramesh

Student Name is: Suresh
Student Name is: Mahesh
Student Name is: Naresh
Student Name is: Rajesh

Update Value in Tuple Python Method 1

names=("Ramesh","Suresh","Mahesh","Naresh","Rajesh")
names[2]="Jiocoding"
print("After update List Data is")
print(names)

============OUTPUT============
names[2]="Jiocoding"
TypeError: 'tuple' object does not support item assignment

Update Value in Tuple Python Method 2

names=("Ramesh","Suresh","Mahesh","Naresh","Rajesh")
print("Before Tuple Data")
print(names)
data=list(names)
data[2]="Jiocoding"
names=tuple(data)
print("After Tuple Data")
print(names)

============OUTPUT============
Before Tuple Data
('Ramesh', 'Suresh', 'Mahesh', 'Naresh', 'Rajesh')
After Tuple Data
('Ramesh', 'Suresh', 'Jiocoding', 'Naresh', 'Rajesh')

Tuple Len Function in Python

Len Function: It can be used to get the length total numbers of elements in tuple. Length function is a built in or user define that counts many items are present in our tuple data.

Example of len Function in Python

data=("Ramesh","Suresh","Mahesh","Naresh","Rajesh")
print("Length of Tuple Data:",len(data))
============OUTPUT============
Length of Tuple Data:5


Tuple Max Function in Python

Max Function: It can be used to get maximum value from the tuple. In case of string focus on ASCII value of First letter of tuple items. The max function returns the largest value given out tuple data.

Example of max function in Python

data=(12,35,78,98,77,99,55,77,89,47,87)
print("Maximum Value of Tuple Data:",max(data))
============OUTPUT============
Maximum Value of Tuple Data:99


Tuple Min Function in Python

Min Function: It can be used to get the minimum value from the tuple. Minimum function are returns the smallest value from a out tuple data.

Example of min function in Python

data=(12,35,78,98,77,99,55,77,89,47,87)
print("Minimum Value of Tuple Data:",min(data))
============OUTPUT============
Minimum Value of Tuple Data:12


Tuple Delete Method in Python

Del Method: We can not delete item from the tuple because it is not changeable. But Del keyword is also used to delete all the items of tuple and delete complete tuple object.

Delete Complete Data using del Keyword Python

data=("Python","Language","India","Bharat")
print("Before Delete List Data")
print(data)
del data
print("After Delete List Item Data")
print(data)
============OUTPUT============
Before Delete List Data
('Python', 'Language', 'India', 'Bharat')
After Delete List Data
NameError: name 'data' is not defined


Tuple Clear Function in Python

Clear Function: Clear() function is used to clear or empty the tuple items. It can be used to remove all elements from a tuple.

Example of empty Tuple in Python

data=["Python","Language","India","Bharat"]
print("Before Clear List Items")
print(data)
data.clear()
print("After Clear List Items")
print(data)
============OUTPUT============
Before Clear List Items
['Python', 'Language', 'India', 'Bharat']
After Clear List Items
[]


Join Tuple Elements in Python: Join Two tuple Join two tuples using + symbol. We can join two tuple using plus(+) operator.

Join Two List Python

data1=("Python","Language","India","Bharat")
data2=("Welcome","Jiocoding","ListData")
data3=data1+data2

print(data3)
============OUTPUT============
('Python', 'Language', 'India', 'Bharat', 'Welcome', 'Jiocoding', 'ListData')


Convert Tuple to List Python

It is used to convert sequence types (tuple) into list.

Convert Tuple to List Python

tdata=("Jiocoding","Python","Language","India","Bharat")
print("Tuple Data Items")
print(tdata)
print(type(tdata))
ldata=list(tdata)
print("List Data Items")
print(ldata)
print(type(ldata))
============OUTPUT============
Tuple Data Items
('Jiocoding', 'Python', 'Language', 'India', 'Bharat')
<class 'tuple'>
['Jiocoding', 'Python', 'Language', 'India', 'Bharat']
<class 'list'>


Count Function: This method counts the number of occurrence of particular item in a tuple.

Count Tuple Items in Python

data=(10,20,40,10,16,40,50,60,40,16,30,16,10,50)
print(data)
print("Total Counts:",data.count(40))
============OUTPUT============
[10, 20, 40, 10, 16, 40, 50, 60, 40, 16, 30, 16, 10, 50]
Total Counts: 3


Index Function: It returns the lowest index of given element.

Example of Index Function in Python

data=(10,20,40,10,16,40,50,60,40,16,30,16,10,50)
print("Find Index Value of 50:",data.index(50))
============OUTPUT============
Find Index Value of 50: 6


Conclusion of Tuple Data Type in Python

Conclusion: This is a all about python tuple data types. I can very function explains with simple example of tuple data. If your any query for in this article than you can contact me by email i can provide best solutions.

Post a Comment

0 Comments
Post a Comment (0)
To Top