[metaslider id="213"]

In Python programming, a tuple is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is added on the other side in a list, elements can be changed.

Tuples are quite similar to lists, but there are some advantages of using tuples.

  • Tuple are immutable, iterating through tuple is faster than with list.
  • Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
  • If you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected.

For creating a tuple add data by comma separation and user round brackets as parentheses

tup1 = ('a', 'b', 2, 5);
tup2 = (1, 2, 3, 4, 5 );

If there is only one values in tuple you have to include a comma after value

tup1 = ("Uzair",);

For accessing and  updating  a value use indices and change any index value of a tuple.  Tuple indices start at 0.

For deleting a tuple use del statement


Tuple in Python

Post navigation

Leave a Reply

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