Methods of Tuple in Python
Hello guys Welcome to CodeWithDevops, Hoping you all doing well.. So, today we are going through Methods in Tuple, We have seen What is tuple and much more... But now we are seeing methods so let's start.
Tuple is Immutable data structure in Python so we can't do much more with it.
Immutable means we can't change if we declared before.
1. Count Method
Count method is same as List we seen before i.e. we can count elements that how many times they are in that tuple.
Syntax for count method in Tuple is :
file_name.count(element)
Eg:-
dummyTuple = (1, 2, 3, 4, 5, 4, 4, 22.2, True, "GeekInsider")
print(dummyTuple.count(4))
Output :
3
2. Index Method
This method is use for finding index of element in tuple.
Syntax for index method in tuple is :
file_name.index(element)
Eg:-
dummyTuple = (1, 2, 3, 4, 5, 4, 4, 22.2, True, "GeekInsider")
value = dummyTuple.index(22.2)
print(value)
Output:
7