Tuple

Tuple

immutable, tuple can not be modified. Modify a tuple variable result in a new tuple.

Tuple usually is in parentheses (), for example: (1,2,3,4,5) or (‘a’,’b’,’c’) or (1,2,3,’a’,’b’,’10’)

However, parentheses is not indicative to tuple, comma is. Tuple must have comma.

Like list, tuple is a sequence, iterable

t=(‘a’,’b’,’c’)

t[0]=“a”

t[1]=“b”

t[2]=“c”

It is zero based indexing, index starts from zero

Tuple can be iterated through:

for i in t:
    print(i)

Convert to Tuple, use function tuple()

For example:

tuple("12345")
('1', '2', '3', '4', '5')

tuple([1,2,3,4,5])
(1, 2, 3, 4, 5)

tuple(12345)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-cc97692a48d4> in <module>
----> 1 tuple(12345)

TypeError: 'int' object is not iterable

Tuple is an object, meaning it has methods and attributes that can be invoked

To see all methods, type

help(<tuple variable>)

In Jupyter notebook, to see list of methods or attributes

Press shift key after enter <tuple variable>.

Last updated