Question -
Answer -
Updating Tuples :
Tuples are immutable which means you cannot update them or change values of tuple elements. But we are able to take portions of an existing tuples to create a new tuples as follows. Following is a simple example :
#!/user/bin/python
tup1 =(12,34.56);
tup2 = (‘abc’, ‘xyz’);
#Following action is not valid for tuples
#tup1[0] = 100;
#So lets create a new tuple as follows :
tup3 = tup1 + tup2;
print tup3;
When the above code is executed, it produces the following result :
(12,34.56, ‘abc’, ‘xyz’)
Delete Tuple Elements :
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.
To explicity remove an entire tuple, just use the del statement. Following is a simple example :
#!/user/bin/python
tup = (‘physics’, ‘chemistry’, 1997,2000);
print tup;
del tup;
print “After deleting tup:”
print tup;
This will produce following result. Note an exception raised, there is because after del tup tuple does not exist any more :
(‘physics’, ‘chemistry’, 1997,2000)
After deleting tup :
Traceback (most recent call last) :
Fill “test.py”, line 9, in < modulo>
print tup;
Name Error : name ‘tup’ is not defined