TUPLES
PRACTICE QUESTIONS
1.# Find the output:
tuple1 = (15, 25, 35, 45, 55, 65) # Given tuple
list1 = list(tuple1) # Convert tuple to list
new_list = []
for i in list1:
if i % 5 == 0:
new_list.append(i)
new_tuple = tuple(new_list)
print(new_tuple)
2.#Give Output of the following code:
Tuple1 = (5,6,-1,3)
Tuple2 = (7,8,4,-9)
Tuple3=()
i=0
while i<4:
if(Tuple1[i]–Tuple2[i])%2==0:
Tuple3+=(Tuple1[i] + 2 * Tuple2[i],)
else:
Tuple3+=(2*Tuple1[i] –Tuple2[i],)
i+=1
print(Tuple3)
3.#Give Output of the following code:
t=(3.5,1.2,2.6,4.8)
L=list(t)
count=0
for x in L:
L[count]+=int(x)+count
count+=1
print(L)
4.#Give Output of the following code:
t1=(3.5,8,"computer","SCIENCE",False)
t2=()
for c in t1:
if type(c) == int:
t2+=(3*c,)
elif type(c)==float:
t2+=(int(c) **2,)
elif type(c)==str:
if c.islower():
t2+=(c.capitalize(),)
else:
t2+=(c.lower(),)
elif type(c)==bool:
if c==False:
t2+=("0",)
else:
t2+=("1",)
print(t2)
5.#Give Output of the following code:
t1=("This",[9,34,53],"of" ,"new" ,"chapter")
print(t1[1][2:4])
print(t1[:1]+t1[1:])
print(t1[1::2])
print(t1[3][0] in t1[1])
6.#Give Output of the following code:
T1=(45, 6, 86, 9)
a,b,c,d= T1
a=a*2
b=b+2
c=c-2
d=d+2
T1=T1[:1]+(a,)+T1[1:2]+(b,)+T1[2:3]+(c,)+T1[3:]+(d,)
print(T1)
7.#Give Output of the following code:
T1=(2,4,6,8,9,10,15)
i=1
while i<=5:
if (T1[i]%2==0):
T1 = T1[:i]+ T1[i+1:]
i+=1
i+=1
print(T1)
8.#Give Output of the following code:
t1=(2,45.5,"PYthOn",90,"c",False)
t2=( )
for i in t1:
if type(i)== float:
t2+=(i+2,)
elif type(i)== int:
t2+= ((i%3),)
elif type(i)== str:
if i.islower():
t2+= (chr(ord(i)+2),)
else:
t2+=(i.lower(),)
elif type(i)== bool:
if i==True:
t2+=("*",)
else:
t2+=("#",)
print(t2)
9. Rewrite the following program after removing the error. Underline each corrections made.
tuple1=tuple(1,2,3,4)
for i in range(len(tuple1)
num=int(input(“Enter the element”))
tuple1+=(num)
if tuple1[i]<10:
tuple1[i]=20
10. Given the tuple tupl = (15, 25, 35, 45, 55, 65, 75, 85, 95)
,
what will be the output of:
Comments
Post a Comment