Tuple Concept in Python (Similar to List)

It is like a list with all comma separated elements enclosed in a pair of parenthesis.

It is an immutable list. A Tuple can not be changed in any way once it is created.

>>> t = (“Hello”, 10, “world”, 20) >>> t (‘Hello’, 10, ‘world’, 20)

Like a list, all its elements are ordered. First element starts at index 0.

>>> t[0]

‘Hello’

 

Negative indices count from the end of the tuple, just like a list

>>> t[-1]

20

Slicing also works for tuple same as it works for list.

>>> t[1:3]

(10, ‘world’) # creates a new tuple

 

Unlike list, they have no in-built methods. So you cannot add, remove, index elements in a tuple.

 

Thus, they are truly immutable in a way that lets you write create write-protect data.

Leave a Reply

Your email address will not be published. Required fields are marked *