0% found this document useful (0 votes)
14 views3 pages

Assignment 2 (B - 26) Colab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Assignment 2 (B - 26) Colab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/4/24, 10:35 PM ASSIGNMENT-2(B_26) - Colab

LIST ASSIGNMENT

a=[]
b=[]
for i in range(10):
a.append(i)
print('The list is:',a)
for i in reversed(a):

b.append(i)
print('The reversed list is: ',b)

The list is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


The reversed list is: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

list1 = ["a", "b", "c"]


list2 = ["1", "2", "3"]

result = []

for i in range(min(len(list1), len(list2))):


concatenated_element = list1[i] + list2[i]
result.append(concatenated_element)

print(result)

['a1', 'b2', 'c3']

l1=['a','b','c']
l2=['1','2','3']
l3=[]
for i in range(len(l1)):
l3.append(l1[i]+l2[i])
print(l3)

['a1', 'b2', 'c3']

l1=['a','b','','c']
filled_list=[s for s in l1 if s!='']
print(filled_list)

['a', 'b', 'c']

L1 = ['a', 'b', 'd']


i = 0
while i < len(L1):
if L1[i] == 'b':
L1.insert(i + 1, 'c')
i += 1
i += 1

print(L1)

['a', 'b', 'c', 'd']

nest_list=[[1,2,3],[4,5,6],[7,8]]
new_list=[9,10]
nest_list.append(new_list)
print(nest_list)

[[1, 2, 3], [4, 5, 6], [7, 8], [9, 10]]

L1=["ABC","DEF","GHI"]
for i in range(len(L1)):
if L1[i]=="DEF":
L1[i]="PQR"
print(L1)

['ABC', 'PQR', 'GHI']

https://colab.research.google.com/drive/1dx208wadlgoeV5PtFFL4RUf56r_ufONG#scrollTo=XY5nR1o_WdHQ&printMode=true 1/3
8/4/24, 10:35 PM ASSIGNMENT-2(B_26) - Colab
LI1=['a','a','a','b','b','c','d','a','f']
LI2=[]
for i in LI1:
if i!='a':
LI2.append(i)
print(LI2)

['b', 'b', 'c', 'd', 'f']

LI1=['a','a','a','b','b','c','d','a','f']
i=0
while i<len(LI1):
if LI1[i]=='a':
LI1.remove(LI1[i])
else:
i+=1

print(LI1)

['b', 'b', 'c', 'd', 'f']

LI1 = ['a', 'a', 'a', 'b', 'b', 'c', 'd', 'a', 'f']
to_remove = 'a'
LI1 = [item for item in LI1 if item != to_remove]
print(LI1)

['b', 'b', 'c', 'd', 'f']

TUPLE ASSIGNMENT

tup=(1,2,6,7,9)
tupi=[]
for items in reversed(tup):
tupi.append(items)
tupi=tuple(tupi)
print(tupi)

(9, 7, 6, 2, 1)

tupa=(10,29,30,40,20,50)
for items in tupa:
if items==20:
print(tupa.index(items))

tupe=(50,)
print(tupe)

(50,)

tup1 = (1, 2, 3)
for i in tup1:
for j in range(1,len(tup1)+1):
print(f"the item {j} is", i)

the item 1 is 1
the item 2 is 1
the item 3 is 1
the item 1 is 2
the item 2 is 2
the item 3 is 2
the item 1 is 3
the item 2 is 3
the item 3 is 3

tup1=(1,2,3)
tup2=(4,5,6)
tup1,tup2=tup2,tup1
print("Tuple 1 is: ",tup1)
print("Tuple 2 is: ",tup2)

https://colab.research.google.com/drive/1dx208wadlgoeV5PtFFL4RUf56r_ufONG#scrollTo=XY5nR1o_WdHQ&printMode=true 2/3
8/4/24, 10:35 PM ASSIGNMENT-2(B_26) - Colab

Tuple 1 is: (4, 5, 6)


Tuple 2 is: (1, 2, 3)

tu1=(1,2,3)
l1=list(tu1)
l1[1]=99
tu1=tuple(l1)
print(tu1)

(1, 99, 3)

tups=(20,30,40,50,60)
print(tups[1:4])

(30, 40, 50)

tupk=(10,20,30,40,50)
tupk=list(tupk)
tupk[1]=99
tupk=tuple(tupk)
print(tupk)

(10, 99, 30, 40, 50)

def reverse_tuple_of_tuples(tuple_of_tuples):

tuple_of_tuples = ((1, 2), (3, 4), (5, 6))


reversed_tuple = reverse_tuple_of_tuples(tuple_of_tuples)
print(reversed_tuple)

my_tuple=('x','y','z','a','b','c','x','y','z')
print(my_tuple.count('x'))

my_tup=(5,5,5,5,5,5,5)
all_same=all(item==my_tup[0] for item in my_tup)
print(all_same)

True

https://colab.research.google.com/drive/1dx208wadlgoeV5PtFFL4RUf56r_ufONG#scrollTo=XY5nR1o_WdHQ&printMode=true 3/3

You might also like