Python Tuple : Tips and tricks

In python the ‘tuple’ data type can hold multiple elements of different types.Unlike int,float or string data type whose single object can hold only one data of it’s kind a single tuple object can hold many values of int ,float or string type,thus making tuple one of the most versatile data type in Python.Note tuples are immutable.

The values assigned to tuple object is separated by comas.The values assigned to tuple may be within a bracket or without it.

>>> a=9 #An int object 
>>> c="String" #a string object
>>> tuple=(1,2,3) #three integers assigned to tuple object
>>> tuple_3=1,"String", 45.67 #Without bracket but still a tuple object
>>> tuple
(1,2,3)
>>> tuple_3
(1,'String' , 45.67)

If a single value is assigned to tuple,the value must be followed by a coma.

>>> tp=23 #An int object
>>> tpi=(23) #An int object
>>> tpt=23, #A tuple object
>>> tp1=(23,) #a tuple object
>>> tp1
(23,)
>>> tp2="C++", #A tuple object
>>> tp2
('C++',)

Without the coma after the value it is treated as an int type object.The coma tells Python to treat it as a tuple object.


Assigning multiple elements to multiple objects of different types

We can also assign multiple values to various object of different types in a single line. According to the sequence the elements are assigned accordingly to the object.

>>> i,s,f,tp=34 , 'text' , 3.1415 , ('7/28/2018' , 657)
>>> i #an int object
34
>>> tp #a tuple object
('7/28/2018' , 657)
>>> i , s , f
(34 , 'text' , 3.1415)

With tuple reverse assignment of tuple to objects is also possible.Consider the code below.

>>> tp=('New' , 'Text')
>>> s1 , s2 =tp  #reverse assignment
>>> s1
'New'
>>> s2
'Text'

While reverse assigning the number of objects on the left hand side must be same as the number of values hold by the tuple.Note such reverse assignment is also known as sequence unpacking.

Nesting a tuple

With tuple we can also assigned a tuple to another tuple object this is also know as nesting a tuple.

>>> tp=('7/28/2018' , 657 , 89.465)
>>> All_tp=tp , ('Happy' , 'New' , 'Year')
>>> All_tp
(('7/28/2018', 657, 89.465), ('Happy', 'New', 'Year'))

The ‘tp’ tuple is the first value and (‘Happy’ , ‘New’ , ‘Year’) is the second value of the ‘All_tp’ tuple.

 


Accessing the elements of tuple

A multiple values may be assigned to single tuple object and to access each if the value we can use the index 0,1,2,3 and so on,The index is written inside a subscript which is a square bracket([]).

>>> tp=('7/28/2018' , 657 , 89.465)
>>> tp[0] #access the first element
'7/28/2018'
>>> tp[1]
657
>>> tp[2]
89.465

We can also assign a single tuple object to multiple object.In this case each values of the tuple object is assigned to the object in sequence.

>>> tp=('7/28/2018' , 657 , 89.465)
>>> st , ii , ff =tp
>>> st
'7/28/2018'
>>> ii
657
>>> ff
89.465

Every thing is simple!!


Use of Tuple

One specific use of tuple is to swap two values of an object.A normal way of swapping two values of an object is to follow a multiple assignment and re-assignment,consider the code below.

>>> a=89
>>> b=45
>>> c=a
>>> a=b
>>> b=c
>>> a
45
>>> b
89

The values are now swaped.Consider the code given below using a tuple.

>>> a=89
>>> b=45
>>> a , b =b , a #Swapping the values
>>> a
45
>>> b
89

The values are swapped in a single line.



Leave a Reply

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