Tuples in python

>>> tup = (11,22,33)
>>> list1 = [11,22,33]
>>> type(tup)
tuple
>>> type(list1)
list
>>> len(tup)
3

Various object types can also be included in the tuple

>>> tup2 = ('Python', 22)

Indexing

>>> tup2[0]
'Python'
>>> tup2[-1]
22

Slicing

>>> tup[0:2]
(11, 22)

Built-in methods

1) Count method

It counts the occurrences of the element of the tuple.

>>> tup3 = (1,1,2,3,3)
>>> tup3.count(1)
2

2) Index method

It returns the index position of the element. In case the element is repeated then the first occurrence index position is returned.

>>> tup3.index(1)
0
>>> tup3.index(2)
2

Immutability

>>> tup
(11, 22, 33)
>>> list1
[11, 22, 33]
>>> list1[0] = 'Python'>>> tup[0] = 'Python'
--------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-19-49ad9da61fd6> in <module>
----> 1 tup[0] = 'Python'
TypeError: 'tuple' object does not support item assignment

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jayashree domala

Jayashree domala

157 Followers

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.