Python Programming
About Lesson

Length of the tuple can get using len() function.

Tuple is immutable
Once a tuple is created, you cannot change its values. Tuples are unchangeable or immutable.

Example:
tup1 = (12, 34.56);
tup2 = (‘abc’, ‘xyz’);

# Following action is not valid for tuples
# tup1[0] = 100;

# So let’s create a new tuple as follows
tup3 = tup1 + tup2;
print (tup3);

Convert Tuple as List
You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Example:
x = (“apple”, “banana”, “cherry”)
#x[1]=”kiwi” this is not possible
y = list(x) #converting tuple into a list
y[1] = “kiwi”
x = tuple(y) #converting a list into a tuple
print(x)

Delete Tuple Elements
Removing individual tuple elements is not possible. But we can delete entire tuple with ‘del’ command.

Example:
tup = (‘physics’, ‘chemistry’, 1997, 2000);
print (tup);
del tup; #but del tup[0] is not possible
print (“After deleting tup : “);
print (tup);

Concatenation and extending tuples
tup1=(1, 2, 3) + (4, 5, 6) #returns (1, 2, 3, 4, 5, 6)
tup2=(‘Hi!’,) * 4 #returns (‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’)
print(tup1)
print(tup2)

You cannot copy content of this page