Tuple Function in Python programming | Hands on coding

Python contains the following tuple functions.


fruit_tuple=("Apple","Orange","Mango")
print ("tuple elements : ", fruit_tuple)
#this line will print length of tuple
print("Length of tuple is ",len(fruit_tuple))
"""
***Output***
tuple elements :  ('Apple', 'Orange', 'Mango')
Length of tuple is  3
"""

fruit_tuple=("Apple","Orange","Mango")
print("Fruits tuple :",fruit_tuple)
print ("Max elements : ", max(fruit_tuple))

animal_tuple=("Zebra","Dog","Elephant")
print("Animal tuple :",animal_tuple)
print ("Max elements : ", max(animal_tuple))

int_tuple=(45,85,36)
print("int tuple :",int_tuple)
print ("Max elements : ", max(int_tuple))

"""
***Output***
Fruits tuple : ('Apple', 'Orange', 'Mango')
Max elements :  Orange
Animal tuple : ('Zebra', 'Dog', 'Elephant')
Max elements :  Zebra
int tuple : (45, 85, 36)
Max elements :  85
"""
  • It is used to get minimum value from the tuple.
  • In case of string focus on ASCII value of first letter of tuple items.

fruit_tuple=("Apple","Orange","Mango")
print("Fruits tuple :",fruit_tuple)
print ("Min elements : ", min(fruit_tuple))

animal_tuple=("Zebra","Dog","Elephant")
print("Animal tuple :",animal_tuple)
print ("Min elements : ", min(animal_tuple))

int_tuple=(45,85,36)
print("int tuple :",int_tuple)
print ("Min elements : ", min(int_tuple))

"""
***Output***
Fruits tuple : ('Apple', 'Orange', 'Mango')
Min elements :  Apple
Animal tuple : ('Zebra', 'Dog', 'Elephant')
Min elements :  Dog
int tuple : (45, 85, 36)
Min elements :  36
"""
  • It is used to convert list into tuple.

#tuple is created using parentheses
fruit_list=["Apple","Orange","Mango"]
print("List Items:",fruit_list)
#this line convert list into tuple
fruit_tuple=tuple(fruit_list)
print("Tuple Items :",fruit_tuple)
"""
***Output***
List Items: ['Apple', 'Orange', 'Mango']
Tuple Items : ('Apple', 'Orange', 'Mango')
"""