Professional Documents
Culture Documents
WORKSHEET -
DICTIONARY
What will be the output of following code-
a-{1:"A",2:"B",3:"C"
for i in a
printtiend=" ")
Ans 23
Ans:
O: Hi!o', 1: H i , 2'Hi2, 3 Hi5 : H
Ans:
O:,:1,2
Ans:
-2,3,4
printt 11D
Ans:
printlendictionary))
Ans:
Ans:
Ans:
a-dict()
a[1
Ans:
a-
a.fromkeys([1,2,3],"check")
Ans:
"
2 ue, 3: se" 5
a-
D1-(2,3,41
printfa)
Ans:
2.3,413
13 Whatil be the output offollowing program:
a[1 1
11=2
1.04
count =O
for 1 in a
Page 3
DICTIONARY-IRN
count + a
printeount)
Ans:
printflentest)
Ans
16 What w1 be the
outyut of ulwiny
proysu:
del
17 wWhat wl be the
outpat f u gug
priatiend
Ans:
{1:5,2:3,3:4
apop(3)
printfa)
Ans:
Ans
asetdefanlt4, D")
printfa)
Ans:
a1:"A"2:T",3:"C"}
printa.setdefault(3)D
Ans:
Page 5
DICTIONARY-IRN
22 What will be the output of following program:
a-1:"A",2:"B",3:"C°"}
ba.copyl0
b21-"D
printfa)
Ans: 1:"A" 2 D", 3:"c"S
a
[1]=1
T1 2
1]-a[1]+1
count =0
for i in a:
count t a[]
printjcount)
Ans: 4
box 8
Jars 8 box bisct 1, co 3
crates0
boxjblscuft]= 1
C O e 1 box: box, ju a
boafcake1 3
jarejam]=4
erates|box]=box
erates[jars]-jars
printflenlicrate ]))
DICTIONARY-IRN Page 6
Ans:
Ans:
91
program:
26 What will be the output of following
rec =
{Name" : "Python", "Age":"20)
r= rec.copyl)
printidr)== idfrec))
Ans Tuu
del rec
"Addr": "N, "Country": "USA"
rec (Name": "Python", "Age":"20",
id2 id(rec)
printid1 =id2)
Ans: Fose
8
my dictl1,2,4)]
=
my dict[l4,2,1)] 10
Page 7
DICTIONARY-IRN
my_dict[(1,2)] =12
sum = 0
for k in my_dict:
sum * my_dictk]
print (sum)
printimy_dict)
Ans: 30
my dict[1] 1
my dict[1] = 2
my dict[1.0 = 4
Sum = 0
for k in
my_dict:
sum t= my dictk
print (sum)
Ans:
arrf1]= 2
arql]*=1
sum = 0
for k in arr:
sum + arrlk
print (sum)
DICTIONARY-IRN
Pag
Ans:
Ans:
Ans
Anss a 2b # 3
Dict
for i in alist:
Dictf aList.count{i)
print (Dict)
Ans:
. ' 5 2 . 6:2
Page 9
DICTIONARY-IRN
plant=
plant[1]-Rohit'
plant2]-Sharma'
plant name']=Ishant'
plant/4-8harma'
print (plantj2|)
print (plant/name])
priat(plant p
print(plant)
Ans
plard- 1: Roh. 2: Sharmo, nou : 1shont, 4 SrarmO
Shoama
shand
Rohat
36
1:'Ruht, 2: 'Shoma', 'norru; '1short, u
Shayma S
What will be the
output of following
program:E
dict {Name': 'Rohie, 'Age': 30}
print (dict.items0)
Ans:
sodas 'Rohs+
Aae 30
37 Write a
Python program to iterate over
dictionaries using for
Ans: loops
a o : h, 'b: hulo,' welom 3
PO volus
d.Hems():
purs R voluus)
38
Write a Python seript to merge two
Python dictionaries
Ans:
DICTIONARY-IRN
Page 10
did- 1:'o', 2: b3
d 2 {2:c', 4:'d'S
dc 3 di - 2. opy)
dad3. updole (did)
punt ( dact3)
dictionary.
Ans:
eVaI (inpus (" Eder dicHona )
cT 1
moxvol = max(did. 1)
foYiin my duc:
Podpaod*diT
pund (prod)
Ans:
DICTIONARY-IRN Page 11
MAdid evol (inpud ("Enler dacr onu
temp L1
NeS dud )
fov R vo i d i t .tdens ) :
vol not in em p
1enmp. op perd (vo)
42 Write a Python program to check a dictionary is empty or not.
Ans:
evol io pud (" Enter ddiono )
dct dict ()
Ans:
dic evo (inpua Ender duction Qu))
u r (" Sum is sum(dic. volu (O))
44 Write a
Python script to concatenate following dictionaries to create a new one
dic1 {1:10,2:20
dic2 (3:30,4:40
die3 (5:50,6:60
Ans:
DICTIONARY-IRN
Page 12
45 Python Program to Add a Key-Value Pair to the Dictionary
Ans:
PSsGe er ev
. u p d o l e (Fa vs
Ans:
vo (inpui (Enjer diUorD u )
)
F pus (" Erier R b e d
Ta Dic.
Ans:
Page 13
DICTIONARY-IRN
Key as Pirst
Charaeter and Va
a Dictionaryith
48
48 Python Program to Create
Character
as Worda Starting with that
ErteT un
Ans: r u _ s t r i r a - Tpud
)
plu *Tirorras - 3urG. sp
plut
du.
Tra
Opperd C)
.apperd (i
V nu d . es
49
Write aprogram in python using dictionary to print the name and salary of
employee.
Ans: -
r-
(pu ("ow rcru epo yees
C r orae (o):
Cuchd
7 (M)
DICTIONARY-IRN
Page 14
Writea program to count the frequency of each character in string using
50
dictionary.
Ans:
("Enier O serder a :
sen =
in pud
Ser e r . \o a)
DICTIONARY-IRN Page 15