Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 13 November 2016

Python Tuple Methods

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.

python-tuple-methods

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 list of 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 - count() and index(). Both the methods are very simple, let's study them one-by-one.

1. count() Method :

This methods accepts a mandatory argument, say item, and returns the number for which the item item appears in the tuple on which this method is called. The syntax to use this method is - tuple_name.count(item).

Example:

>>> 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

2. index() Method :

Like previous method, this method also accepts a mandatory argument, say item, and returns the index of the item item in the tuple. If item is occurs in the tuple multiple times, index of the first occurrence is returned. The syntax to use this method is - tuple_name.index(item).

>>> 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.

This article is originally published at www.codeninja.in - Introduction to Python Tuple Methods


0 comments:

Post a Comment