0% found this document useful (0 votes)
54 views5 pages

Program-12:-Tuple and Its Operations

The document discusses various operations that can be performed on tuples in Python. It provides 10 examples of Python programs that demonstrate how to unpack tuples, convert tuples to strings, find repeated items in tuples, remove items from tuples, convert lists of tuples to dictionaries, replace tuple values in lists, sort tuples based on float values, convert tuple of strings to integers, remove empty tuples from lists, and count elements in a list until reaching a tuple.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views5 pages

Program-12:-Tuple and Its Operations

The document discusses various operations that can be performed on tuples in Python. It provides 10 examples of Python programs that demonstrate how to unpack tuples, convert tuples to strings, find repeated items in tuples, remove items from tuples, convert lists of tuples to dictionaries, replace tuple values in lists, sort tuples based on float values, convert tuple of strings to integers, remove empty tuples from lists, and count elements in a list until reaching a tuple.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program-12 :- Tuple and its operations

(i) Write a Python program to unpack a Tuple in a several variables.


T = (3, 6, 8)
A, B, C = T
print(A)
print(B)
print(C)
Output :
3
6
8
(ii) Write a Python program to convert a Tuple to a string.
T = (‘e’, ‘x’, ‘e’, ‘r’, ‘c’, ‘i’, ‘s’, 'e')
str =' '.join(T)
print(str)
Output :
exercise

(iii) Write a Python program to find the repeated items of a tuple.


Var = int(input())
Tup=(10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2)
A=list(Tup)
for i in range(len(A)) :
A[i]=int(A[i])
count=A.count(Var)
Print(Var,’appears’,count,’times in the tuple’)
Output :
8
8 appears 3 times in the tuple
(iv) Write a Python program to remove an item from a tuple.
a = (23,45,56,68,10,45,7,9)
print(“Original A :”,a)
b = list(a)
b.remove(56)
a = tuple(b)
print(“After Removing :”,a)

Output :
Original A : (23,45,56,68,10,45,7,9)
After Removing: (23,45,68,10,45,7,9)

(v) Write a Python program to convert a list of tuples into a


dictionary.
t = [(‘Ram’,1),(‘Shyam’,2),(‘Sohan’,3)]
x = dict(t)
print(x)

Output :
{‘Ram’:1, ‘Shyam’:2, ‘Sohan’:3}

(vi) Write a Python program to replace the last value of tuple in a list.
L = [(10,20,30),(40,50,60),(70,80,90)]
print([t[:-1]+(600,) for t in L])

Output :
[(10,20,600),(40,50,600),(70,80,600)]

(vii) Write a Python program to short a tuple by its float number.


Rate = [(“item1”,12.20),(“item2”,13.10),(“item3”,24.5)]
print(sorted(rate, key=lambda x: float(x[1]),reverse=True)
Output:
[(‘item3’,24.5),(‘item2',13.10),(‘item1’,12.20)

(viii) Write a Python program to convert a tuple of string values to a


tuple of integer values.
tuple1 = (‘1’, ‘4’, ‘3’, ‘6’, ‘7’)
x=[int(j) for j in (tuple(filter(lambda i:(i),tuple1)))]
print(tuple(x))
Output:
(1, 4, 3, 6, 7)

(ix) Write a Python program to remove an empty tuple(s) from a list


of tuples.
L = [(),(),(' ',),(‘a’,’b’),(‘d’)]
L = [t for t in L if t]
print (L)
Output :
[(‘ ' ,), (‘a’,’b’),’d’]

(x) Write a Python program to count the element in a list until an


element is a tuple.
num = [10,20,30,(10,20),40]
ctr = 0
for n in num :
if isinstance(n,tuple) :
break
ctr = ctr+1
print (ctr)
Output :
3

You might also like