Tuple Function in Python

  • Python contains the following tuple functions.
  • It is used to get the numbers of elements in tuple.

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
"""
  • It is used to get maximum 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 ("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')
"""

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. Thanks.

Post a Comment

0 Comments