You are on page 1of 15

FIITJEE Coimbatore Centre

WORKSHEET -
DICTIONARY
What will be the output of following code-
a-{1:"A",2:"B",3:"C"
for i in a

printtiend=" ")

Ans 23

What will be the output of following code-


a-: Hil' +
str(i) for i in range(5)}

Ans:
O: Hi!o', 1: H i , 2'Hi2, 3 Hi5 : H

3 What will be the output of following code-


D dict)
for i in range (3:
for j in reange(2):
D =j
print(D)

Ans:

O:,:1,2

4 What will be the output of following code-


a=i: ii for i in range(6)}

Ans:

O:O, 1:, 2 : , 3,9, : 16, 5: 25


5 What will be the output of following code
a-

-2,3,4
printt 11D
Ans:

What wil be the


output of folowring program:
dictionary {1:'1, 2:2, 3:3
del dictionary[l]
dictionaryl1] = "10
del dictionaryi2]

printlendictionary))
Ans:

Prediet the Output:


dictl =
faame": ile", 'salry: 8000}
temp dict1.pop("age)
printftemp)

Ans:

8 Whati be the output


of folouing program:
dict1 fkey1":1, "key2":2}
diet2 e y 2 2 , "ey1:1)
printdict 1 = dict2)

Ans:

9 What will be the output of followring program:


dict Virat:1,Rohit2}
dict.update(f"Rahul":2)
DICTIONARY-IRN
Page 2
printdict)
Ans:

10 What wi1l be the output of following programn:

a-dict()
a[1
Ans:

11 What wil be the output of following program:

a-
a.fromkeys([1,2,3],"check")

Ans:
"

2 ue, 3: se" 5

12 Whatwill be the output of followring program:

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:

14 What will be the


outyut of tolkowing pnoym:
test
11A', 2:", 3C
del test1]
test1 D
del teet(21

printflentest)
Ans

15 What wil be the


output f
test
11A, 2®, 3C}7 tkwing progm:
test
printlentest))
Ans

16 What w1 be the
outyut of ulwiny
proysu:
del

17 wWhat wl be the
outpat f u gug

priatiend
Ans:

18 what wil be the output of following program:

{1:5,2:3,3:4
apop(3)
printfa)

Ans:

19 What will be the output offollowing prograr


a1:5,2:3,34}
printfapopl4,9))
print

Ans

20 What will be the output of following program:


a41:A"2:T",3:"C}

asetdefanlt4, D")
printfa)

Ans:

21 What wil1 be the output of following program:

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

23 What will be the output of the program?

a
[1]=1
T1 2
1]-a[1]+1
count =0

for i in a:
count t a[]

printjcount)

Ans: 4

24 What wil be the output of following program:

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:

25 What will be the output of following program:


dict fe': 97, 'a: 96, b': 98)
for in sorted(dict):
print (dictU)

Ans:

Sood ed dict 'o95,'b:98,c':a1

91
program:
26 What will be the output of following

rec =
{Name" : "Python", "Age":"20)
r= rec.copyl)
printidr)== idfrec))

Ans Tuu

27 What will be the output of following program:


"Addr": "NJ", "Country": "USA")
rec ("Name": "Python", "Age":"20",
id1 = id(rec)

del rec
"Addr": "N, "Country": "USA"
rec (Name": "Python", "Age":"20",
id2 id(rec)
printid1 =id2)

Ans: Fose

28 What will be the output of following program:


m ydict =

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

Cl2.4): 8. (4,21): 1o,0,2) :12f

29 What will be the output of following program:


my_dict = 8

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:

30 What wil1 be the output of


arr = }
following program:
arq1] = 1

arrf1]= 2
arql]*=1
sum = 0

for k in arr:
sum + arrlk

print (sum)

DICTIONARY-IRN

Pag
Ans:

31 Whatilbe the outpat of following program:


a a1,b':2,c3}
print (DD

Ans:

32 Whatwin be the oatput of following program:


a-1,21,2,3:2}
printja[1,2

Ans

33 h a t will be the output of followring program:


33
a1:,2D, 3:'e}
for ij in aitems
printfij.end="*")

Anss a 2b # 3

34 hatill be the output of following program:


aList [1,2,3,5,6,1,5,6,1]

Dict
for i in alist:

Dictf aList.count{i)
print (Dict)

Ans:
. ' 5 2 . 6:2

35 Whatill be the output of following program:

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)

Write a Python program to get the maximum and minimum vaue in a


39

dictionary.

Ans:
eVaI (inpus (" Eder dicHona )
cT 1
moxvol = max(did. 1)

Mur va mur (dact- )


puns ("MOIMurs maxvolS)

pund Minumum 1 mAnv oJ)

40 Write a Python program to multiply all the items in a dictionary


Ans:
mudict evo (inpus (" ander a dichor
d-

foYiin my duc:
Podpaod*diT
pund (prod)

41 Write a Python program to remove duplicates from Dictionary

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 ()

puund" You endoud an npBy duchon uy

43 Write a Python program to sum all the items in a dictionary.

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

46 Python Program to Check if a Given Key Eists in a Dictionary or Hot

Ans:
vo (inpui (Enjer diUorD u )
)
F pus (" Erier R b e d
Ta Dic.

pur (" key doert ess)

47 Python Program to Generate a Dictionary that Contains ambers (between 1


and n) in the Form (zr*x).

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):

evol in pu ( tnder name. solau )

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)

p-diQ o c def ah mnopqrstovwxyz Cl23 S 6 TR


Cho ound {3
OT co in
odp- dig
cound
Cho ount ch = chor unt ch

DICTIONARY-IRN Page 15

You might also like