solution for test yourself-2




Hello folks...!!!

Try all these in your jupyter notebook.


Q1:

s1="I am learning python"
li=s1.split()
print(li)
li[3]=li[3].capitalize()
s1=' '.join(li)

print(s1)


Q2:

s2="There was a lazy fox in the forest.The fox along with its family lived near the river.The lazy fox always slept and never brought food.So its family suffered from hunger."
s2=s2.replace("."," ",4)
print(s2)
dict1={}
li1=s2.split(' ')
print("\n\n",li1)
dict1['fox']=li1.count('fox')
dict1['lazy']=li1.count('lazy')
dict1['family']=li1.count('family')
dict1['river']=li1.count('river')

print("\n\n",dict1)


Q3:

s3="Twinkle Twinkle little star"
li1,li2=[],[]
li1=s3.split()
print(li1)
li2.append(li1[0][::-1])
li2.append(li1[1][::-1])
li2.append(li1[2][::-1])
li2.append(li1[3][::-1])
print("\n\n",li2)
s3=' '.join(li2)

print("\n\nEncrypted string :",s3)


Q4:

li_keys=['a','b','c','d']
li_values=['apple','ball','cat','data']

dict1={}
dict1[li_keys[0]]=li_values[0]
dict1[li_keys[1]]=li_values[1]
dict1[li_keys[2]]=li_values[2]
dict1[li_keys[3]]=li_values[3]


print(dict1)


Q5: Try yourself for the s2 & s3.

s1="He is MLA"
s2="University is in 2nd street"
s3="I saw bird"

li1=s1.split(" ")
print(li1)
li1.insert(2,'an')
print(li1)
s1=' '.join(li1)

print(s1)

NOTE : The method insert(index,element) was not explained before. It is used to insert an element in a list at specified index.


Q6:

li=['22','35','10','15','9']
li1=list(map(int,li))
tot=sum(li1)
aveg=tot/len(li)

print("\n",tot,"\n",aveg)

Q7:

s1="The cattle is grazing"
s2="The field looks like a green blanket over the earth"

li1=s1.split(' ')
print(li1)
li1=li1+(s2.split(' '))
li1.sort()
print(li1)

s3=' '.join(li1)

print(s3)

Q8: Error correction

#code snippet1
a=dict([('a','apple'),('b','ball'),('c','cat')])
print(a)

#code snippet2
dict1={'a':1,'b':2,'c':3,'d':4}

dict1.pop('e',"value not found")

Q9:

dict1={}
dict1['a']={'antonym':'opposite','adhere':'stick to','anguish':'suffering','accustomed':'usual'}
dict1['q']={'quaint':'old-fashioned','quiver':'tremble','quoth':'said','quasi':'partly'}

print(dict1)

Q10:

maths={'a','b','c','d'}
phy={'b','e','z','q'}
cs={'a','f','g','d','k','r'}
che={'l','m','k','d'}

tot=maths.union(phy,cs,che)
print(tot)

print("Total number of students appeared for the admission :",len(tot))
print("The students who applied for both maths and physics :",maths.intersection(phy))

print("Number of students who applied for computer science but not for maths :",len(cs-maths))

Comments