Tag Archives: tuples in python

Python- The Tuples

Since list used the square brackets [], tuple had to do with the paranthese ()

The next big difference between list and tuple is that tuples are immutable. That means the value of the tuple cannot be changed.

We can change the value of list, but we will get an error ‘object does not support item assignment’ when we try to do the same for a tuple.

 

 

We cannot delete individual elements as we did in lists.

But, we can delete the entire tuple using the method ‘del’

Some of the others methods in tuple are : min,max,len

 

tuple = (‘0′,’1′,’2’)
print(‘Print the tuple :’,tuple)
del tuple
print(‘Tuple has gone empty :’,tuple)

tup2 = (‘0′,’1′,’2’)
tuple = tup2
print(‘Print the tuple :’,tuple)
print(‘Length of the tuple is now ‘,len(tuple))

tuple = tuple + tup2
print(‘Print the tuple :’,tuple)
print(‘Length of the tuple is now ‘,len(tuple))

tup3 = (‘-1′,’7’)
tuple = tuple + tup3

print(‘Maximum value in the tuple is ‘,max(tuple))
print(‘Minimum value in the tuple is ‘,min(tuple))

 

The output of the above program

 

Print the tuple : (‘0’, ‘1’, ‘2’)
Tuple has gone empty : <class ‘tuple’>
Print the tuple : (‘0’, ‘1’, ‘2’)
Length of the tuple is now 3
Print the tuple : (‘0’, ‘1’, ‘2’, ‘0’, ‘1’, ‘2’)
Length of the tuple is now 6
Maximum value in the tuple is 7
Minimum value in the tuple is -1