Skip to content

Tuples in Python

wilsonshamim edited this page Jun 11, 2018 · 1 revision

A tuple is a sequence of immutable Python objects.
tup1 = (‘physics’, ‘chemistry’, 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = “a”, “b”, “c”, “d”;

an empty tuple
t = ()

To write a tuple containing a single value you have to include a comma, even though there is only one value −
tup1 = (50,);

accesing the value in tuple tup[index]

we cant update tuples as they are immutable.
deleting the tuple
del tup

we cant delete an element from tuple as they are immutable.

Built-in Tuple Functions
Python includes the following tuple functions −

Sr.No. Function with Description
1 cmp(tuple1, tuple2)
Compares elements of both tuples.

2 len(tuple)
Gives the total length of the tuple.

3 max(tuple)
Returns item from the tuple with max value.

4 min(tuple)
Returns item from the tuple with min value.

5 tuple(seq)
Converts a list into tuple.

Clone this wiki locally