Python Tuple Methods
Now, we are going to have a look at some of the methods, which help us do some different things in relation to a tuple. Basically, there is not so much that we can do directly with a tuple, because it is unchangeable. So, there is nothing like append, remove, pop, etc. but we have some different methods, like the index, and the count method, and we are going to explore them.
Python Tuple Methods
Here is the table, with the names of the methods, with some descriptions and examples.
In the table, for an explanation, the example tuple considered is as given below –
mytuple = (1,2,2,2,2,3,4,5,6,6,6)
Method | Description | Example |
count() | This method returns the number of times the specified element has occurred in the tuple. Gives error if the specified element is not present in the tuple. | c = mytuple.count(2) print(c) output → 4 |
index() | This method returns the index of first occurrence of the element in the tuple (if it is present multiple times) | c = mytuple.index(2) print(c) output → 1 |
Now, Let’s have a look at programs, which demonstrate us about using these methods in our programs.