Choter -64
Dictionary nd set
Deionary in Kiad.
tö store data values in key:volue
DLeionaries aPe used to
þaLrs, changeable and don't ollon
They are ee unordered,
key 99 : Value
dupicata. keys.
dict =$
c name": Petiune "
ce Year":1979.02
«Marks'": [98, 97, 82, 69, 25,
Code:
info = 6name": Bet+une
99
Marks": (98, 9%, 69.25),
12.95 : True,
þrint (info)
utþut
name': 'BelHune' 9
' Marks': (98,9, 69.25), 12 95: Truef
dict ["name"J, dict Year',díct ["Marks"J
We Can CSSign a nen Vale to any of Lie keys
defined as
dict name' = Betiune_ College"
Ne can odd nen key, un the dictiohary ps
dict ["suwname" Bídan Sarani
null dict = {{
Nested Diotonaries
student
name': "pe thune",
chem": 98,
"phy" ;9*,
math': 95
studentmarks' at"
Diehonary Metliods
Dict. keys () # perns all keys
[Link]() # peuns all values
Dict. items () # returns all (key, value) pairs oas tuples
Dict. lget (key") # ret+urns tie key acording to value
[Link] (nendict)# inserts the ahecLfied itemz to Ihe
#dicionary
Code:
þrint (student, keys ())
print (list ([Link] (0)) #to
# ypecasting the dich keyo
ist
þrint (Pon (studenb) #Total 0. of keys
þrint ([Link] C))
print (Pist (sudent. valest))
Qutþut
e','marks' )
dict keys (C'hame',"
[name',mapks'
dict_values (Betiune', chem: 98, phy':7,matt': 95
['Bethune', chem: 98, phy'i9,'matii96
Code:
siden rint ludent. items ())
Dutbut "fhy':9,
dictitems (T('ome','Bethune), ((marks',chem'98,
b1ath:95)1)
Codei
þairs= lst (skudent. items())
þeint (pairsD) namet) #error
+þrint (student
print (sbudentget ("name")),
nen dict = ;ekolkata"
[Link] [Link]) print (studenb)
Qutput
('Name', Beliune)
None
name':'Bethune', 'mapks': chem': 8,'phy'9H, Sralh :954
cit': 'koLkata'
Set inEtion
Set is a ollechon oE unopdered tems.
Fach element in [Ye set_must be Lumique and nutable.
Mums =51,2,3,4f
set2-i,2,2,2f# eheaed elemets stoped only once, S0
it resolyed To f1,2f
mull set = set() # empt set syntax.
di ={ # enpty dictionary
We can store Boolean, int, float,string and tuples in a cet
tiey are
unchangae but cannot store Dists and dictonar
Set Metiods.
set,add (el) #adds an element
eeb,remove(el) # Pemoves the element
[Link]) # embties. the set
value
set.þob()# removes a random
Code code
S= set C) # enpty set [Link] (3)
[Link] (1) þrütS. þop(O)
S. odd(25)
S. add(3) þrint(6)
S. add ("Hello)
print (S)
print (len)
oukbu oubþut
1,25,
4
3,elo' j2.5, 'Heluo'
Metiods
[Link] (set2) # Corbines boli set values and rtrns nen
[Link](set2) t combLnescomTIO Vaues and peirns
new
odes
S1= 12,3,4.5, "Helo"
S2=3,4.5, "Belhune" 5.2
a þrint ([Link](s2).
print (S1. intepsection(s2))
gutut
i2,3,4:5, Hello','Bethune', s:2
3,4:5
ÎradiceQuestions
1Store
table'prlloning
: akile d meaningsina ytron dictinary:
ahiede of-purnitture ist of fats and fgures
cat: a. Smallanial
codes
Dict =
Cat: a small animal'"
table": a piece of furni Tire stof facts ond
ot print (Dit)
2. You ape
gwen a list of subjecl for studerits. Assume one
classroom L Pequiredfor 1 subiect. Hon many classpoo ms
are needed fop all subjects.
Fytion,
C.
java, Ctt, Fytion, jovasPipt, java, Fytion ,jaYa,Ctt,
3. WAP to enler marks of 5 subjedts from Ihe Lser and
store thHem in a lictionary. Start witi
dlictiomary. Start
a
witlh an embty dicton
ary md add one by [Link] subject as keyand
mapks as Vaue.
code:
I= Hbat (input ( Enter phy. marks:)
[Link](Physics":x)
X=poat (input (Enter chem marks:)
Marks. updole (*Chemistry";x)
N= float (input ( Enler matis marks:))
Marks. uþdae ({4 Maths"; x?)
floot (inpt (Entar Eng mapks:"))
Marks. updota (1"English": )
Hoat (input (Ente Bio mar ko))
Mar ks. updsla ( Biology)
þrint (Marks)