Tuple Built In Methods

 


Built in Methods for Tuple Class to perform certain operations

  • count() : return the number of elements with specified values


#program

t1 = ("Techno Xpresss",28, 80.72, "Techno Xpresss", 80.72)
print("t1 : ", t1)
count = t1.count(
"Techno Xpresss")
print("Techno Xpress presnts ", count, "times")

count = t1.count(
80)
print("80 presnts ", count, "times")

#output

t1 : ('Techno Xpresss', 28, 80.72, 'Techno Xpresss', 80.72)
Techno Xpress presnts
2 times
80 presnts 0 times


  • index() : returns index of 1st matched  item, with the passed value

#program

t1 = ("Techno Xpresss",28, 80.72, "Techno Xpresss", 80.72)
print("t1 : ", t1)
index = t1.index(
"Techno Xpresss")
print("Techno Xpresss 1st occurance is at index : ", index)

index = t1.index(
80.72)
print("80.72 1st occurance is at index : ", index)

#output

t1 : ('Techno Xpresss', 28, 80.72, 'Techno Xpresss', 80.72)
Techno Xpresss
1st occurance is at index : 0
80.72 1st occurance is at index : 2

                
index() will return ValueError if passed value is not an item in Tuple

#program

t1 = ("Techno Xpresss",28, 80.72, "Techno Xpresss", 80.72)
print("t1 : ", t1)
index = t1.index(
"Techno Xpresss")
print("Techno Xpresss 1st occurance is at index : ", index)

index = t1.index(
80)
print("80 1st occurance is at index : ", index)

#output

t1 : ('Techno Xpresss', 28, 80.72, 'Techno Xpresss', 80.72)
Techno Xpresss
1st occurance is at index : 0
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 70, in <module>
index = t1.index(80)
ValueError: tuple.index(x): x not in
tuple







Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India