Tuples in Python

Tuples in Python

We should check the methods the tuple data include:

>>> r = (1,2,3,4,5,6)
>>> r
(1, 2, 3, 4, 5, 6)
>>> dir(r)
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘_
_eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getnewargs
__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’
, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__
rmul__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘count’, ‘i
ndex’]
>>>

Not many. They are like in the list data, but without those that can change something. We should remember that tuples are immutable.

The following accessor methods provide information about a tuple.

tuple.count(value) → integer

>>> t = (1,2,3,4,5,6)
>>> u = (“Bolek”, “Lolek”, “Tolek”, “Molek”)

>>> t.count(3)
1
>>> u.count(“Tolek”)
1
>>>

Return number of occurrences of value in list.

tuple.index(value) → integer

>>> t.index(3)
2
>>> u.index(“Tolek”)
2
>>>

Return index of first occurrence of value in list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s