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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s