Sunday, February 2, 2020

Tuples

Tuple

A Tuple is a simple collection of elements like lists in Python, These elements can be heterogeneous. The major difference is the mutability of these data structures. Lists are mutable, but Tuples are not mutable. It means once a tuple object is created then it can not be modified. We can declare a tuple by comma separator, like this,

t=1,1.23,'welcome',0,True,'to',-4.5

or 

t=(1,1.23,'welcome',0,True,'to',-4.5)

The brackets are not necessary, but it is good practice to enhance readability. We can access tuples by putting index value like we do in lists.

Indexing of Tuple

A member item of the tuple can be accessed by the index of that item. The index number for a tuple in Python starts with 0 in forwarding direction and -1 in the backward direction.
tuple indexing




print('===================================')
tup=(
'Jan','Feb','Mar','April','May', 'June')
print(tup[0:6]) #Forward indexing
print(tup[-7:-1]) #Backward indexing
print(tup[:-3]) #print left side from index -3
print(tup[-4:]) #print right side from index -4
print(tup[:])  #print All

Output:
===================================
Accessing the items by index
('Jan', 'Feb', 'Mar', 'April', 'May', 'June')
('Jan', 'Feb', 'Mar', 'April', 'May')
('Jan', 'Feb', 'Mar')
('Mar', 'April', 'May', 'June')

('Jan', 'Feb', 'Mar', 'April', 'May', 'June')


Reassigning a value to any index position is not possible, it means doing tup[5]='Dec' is not possible, but if required we can first change the tuple into a list than modify the value and again create the tuple from this list.

print('===================================')
tup=(
'Jan','Feb','Mar','April','May', 'June')
ls=
list(tup) # Create a new list
ls[4]='Sept'  #change item
tup=tuple(ls)  #Convert back into tuple
print(tup)
Output:
===================================
('Jan', 'Feb', 'Mar', 'April', 'Sept', 'June')


We can also apply different operation on tuples,

print('1.===================================')
print('Loop over Tuple')
tup=
1,1.23,'welcome',0,True,'to',-4.5

for item in tup:
 
print(item)

print('2.===================================')
print(type(tup))

print('3.===================================')
t1=
0,
print(type(t1))

print('4.===================================')
#we can also ceate tuple using tuple() function
t2=tuple('programming')
print(t2)

#OR we can use a range
print('5.===================================')
tr=
tuple(range(5))
print(tr)

t7=(
2,5,7)
#tup[1]=10;  NOT POSSIBLE as tuples are immutable
print('6.===================================')
# we can also use += Assignment
t7+=(9,11)
print(t7)


Try Yourself



Tuples do not support append() and extend() method. we can use += Assignment, but += operator does not change the tuple itself, but it changes the bindings of the variable.

Packing and Unpacking of Tuples


Assigning the values to a tuple like tp=(3,4,7) is also known as the Packing of the tuple. We can create a single value tuple as tp= (3,) (add a comma).
For unpacking the data from tuple we use multiple assignments. For example
a,b,c=(3,4,7)
if some value is not assigned an identifier then we can use _ (underscore) symbol for the disposal of unassigned value as a placeholder. For example
a,_,b=(3,4,5)