Worksheet - Dictionary
Worksheet - Dictionary
DICTIONARY
Ans:
Ans:
Ans:
1|Page
4 What will be the output of following code-
a={i: i*i for i in
range(6)} a
Ans:
2|Page
5 What will be the output of following code-
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
Ans:
Ans:
7 Predict the Output:
Ans:
Ans:
3|Page
9 What will be the output of following
program: dict={"Virat":1,"Rohit":2}
dict.update({"Rahul":2})
4|Page
print(dict)
Ans:
Ans:
Ans:
Ans:
5|Page
0 for i in
a:
6|Page
count +=
a[i]
print(count)
Ans:
Ans:
Ans:
Ans:
7|Page
17 What will be the output of following
program: a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")
8|Page
Ans:
Ans:
Ans:
Ans:
Ans:
9|Page
22 What will be the output of following
program: a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)
Ans:
Ans:
10 | P a g
e
box crates['jars'] =
jars
print(len([crates]))
11 | P a g
e
Ans:
Ans:
Ans:
Ans:
12 | P a g
e
28 What will be the output of following
program: my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
13 | P a g
e
my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum +=
my_dict[k]
print (sum)
print(my_dict
)
Ans:
Ans:
14 | P a g
e
30 What will be the output of following
program: arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum +=
arr[k]
print (sum)
15 | P a g
e
Ans:
Ans:
Ans:
Ans:
Ans:
16 | P a g
e
35 What will be the output of following program:
17 | P a g
e
plant={}
plant[1]='Rohit'
plant[2]='Sharma'
plant['name']='Isha
nt'
plant[4]='Sharma'
print (plant[2])
print
(plant['name'])
print (plant[1])
print (plant)
Ans:
Ans:
Ans:
18 | P a g
e
39 Write a Python program to get the maximum and minimum value in a
dictionary.
Ans:
Ans:
19 | P a g
e
42 Write a Python program to check a dictionary is empty or not.
Ans:
Ans:
20 | P a g
e
45 Python Program to Add a Key-Value Pair to the Dictionary
Ans:
Ans:
Ans:
21 | P a g
e
48 Python Program to Create a Dictionary with Key as First Character and
Value as Words Starting with that Character.
Ans:
49 Write a program in python using dictionary to print the name and salary
of employee.
Ans:
22 | P a g
e
for key,val in details.items():
print(key, "=>", val)
50 Write a program to count the frequency of each character in string using
dictionary.
Ans:
23 | P a g
e