Python Tuple Methods - Hello readers! This is the 13th article of our tutorial series on Python - Python on Terminal, and we are going to learn about Tuple methods in this one. In the last article on Python Tuple, we have learned what tuples are, what properties they have and what operations we can perform on them. This article is all about the methods related to Python tuples, how they are used and their examples.
As usual, we try to know what all possible methods we can call on a tuple. For that, we create a tuple and provide it as an argument to Python built-in
dir()function. For those, who do not know about
dir()function - it accepts an argument (a Python object) and returns a
listof all the methods associated with that Python object. Here, we will provide a Python tuple, so we will get a list of all the methods associated with any Python tuple. Let's check this now.
>>> myTuple = (1, 4, 9, 16, 25) >>> dir(myTuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
We can see some methods with names in the form
__methodName__, which are called as Magic methods. These methods are user by Python internally. For example, when we use Python built-in
len()function to get length of a tuple, Python internally uses
__len__magic method. For now, we keep them aside and focus on rest of the methods. And, there are only two of them -
index(). Both the methods are very simple, let's study them one-by-one.
count() Method :
This methods accepts a mandatory argument, say
item, and returns the number for which the item
itemappears in the tuple on which this method is called. The syntax to use this method is -
>>> myTuple = (1, 4, 9, 'One', 4, 'Nine', 1, 'Four', 'Nine', 1) >>> myTuple.count('Nine') 2 >>> myTuple.count(1) 3 >>> myTuple.count(4) 2 >>> myTuple.count('One') 1
index() Method :
Like previous method, this method also accepts a mandatory argument, say
item, and returns the index of the item
itemin the tuple. If
itemis occurs in the tuple multiple times, index of the first occurrence is returned. The syntax to use this method is -
>>> myTuple = (1, 4, 9, 'One', 4, 'Nine', 1, 'Four', 'Nine', 1) >>> myTuple (1, 4, 9, 'One', 4, 'Nine', 1, 'Four', 'Nine', 1) >>> myTuple.index('Four') 7 >>> myTuple.index('One') 3 # Index of the repeated element >>> myTuple.index(1) 0 >>> myTuple.index(4) 1
Thus, we have come to an end of this article. There wasn't much to learn about Python tuple methods as there are only two, but it was essential to know about them. Anyways, please share your views and opinions in the comment section below and please stay tuned for some more articles on Python. Thank you for stopping by.