[go: up one dir, main page]

0% found this document useful (0 votes)
24 views23 pages

Worksheet - Dictionary

Uploaded by

duvvieekshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views23 pages

Worksheet - Dictionary

Uploaded by

duvvieekshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

WORKSHEET –

DICTIONARY

1 What will be the output of following code-


a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")

Ans:

2 What will be the output of following code-


a={i: 'Hi!' + str(i) for i in
range(5)} a

Ans:

3 What will be the output of following code-


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

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:

6 What will be the output of following


program: dictionary = {1:'1', 2:'2', 3:'3'}
del dictionary[1]
dictionary[1] =
'10' del
dictionary[2]
print(len(dictionary
))

Ans:
7 Predict the Output:

dict1 = {"name": "Mike", "salary":


8000} temp = dict1.pop("age")
print(temp)

Ans:

8 What will be the output of following


program: dict1 = {"key1":1, "key2":2}
dict2 = {"key2":2, "key1":1}
print(dict1 == dict2)

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:

10 What will be the output of following


program: a=dict()
a[1]

Ans:

11 What will be the output of following


program: a={}
a.fromkeys([1,2,3],"check")

Ans:

12 What will be the output of following


program: a={}
a['a']=1
a['b']=[2,3,4
]
print(a)

Ans:

13 What will be the output of following


program: a = {}
a[1] = 1
a['1'] = 2
a[1.0]=4
count =

5|Page
0 for i in
a:

6|Page
count +=
a[i]
print(count)

Ans:

14 What will be the output of following


program: test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test
)

Ans:

15 What will be the output of following


program: test = {1:'A', 2:'B', 3:'C'}
test = {}
print(len(test
))

Ans:

16 What will be the output of following


program: a={1:"A",2:"B",3:"C"}
del a

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:

18 What will be the output of following


program: a={1:5,2:3,3:4}
a.pop(3)
print(a)

Ans:

19 What will be the output of following


program: a={1:5,2:3,3:4}
print(a.pop(4,9))
print()

Ans:

20 What will be the output of following


program: a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D")
print(a)

Ans:

21 What will be the output of following


program: a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

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:

23 What will be the output of the


program? a = {}
a[1] = 1
a['1'] = 2
a[1]=a[1]+1
count =
0 for i in
a:
count +=
a[i]
print(count)

Ans:

24 What will be the output of following


program: box = {}
jars = {}
crates =
{}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] =

10 | P a g
e
box crates['jars'] =
jars
print(len([crates]))

11 | P a g
e
Ans:

25 What will be the output of following


program: dict = {'c': 97, 'a': 96, 'b': 98}
for _ in
sorted(dict):
print (dict[_])

Ans:

26 What will be the output of following


program: rec = {"Name" : "Python",
"Age":"20"}
r = rec.copy()
print(id(r) ==
id(rec))

Ans:

27 What will be the output of following program:


rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country"
: "USA"} id1 = id(rec)
del rec
rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country"
: "USA"} id2 = id(rec)
print(id1 == id2)

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:

29 What will be the output of following


program: my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum +=
my_dict[k]
print (sum)

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:

31 What will be the output of following


program: a = {'a':1,'b':2,'c':3}
print (a['a','b'])

Ans:

32 What will be the output of following


program: a = {(1,2):1,(2,3):2}
print(a[1,2])

Ans:

33 What will be the output of following


program: a={ 1:'a', 2:'b', 3:'c'}
for i,j in
a.items():
print(i,j,end="#"
)

Ans:

34 What will be the output of following


program: aList = [1,2,3,5,6,1,5,6,1]
fDict = {}
for i in aList:
fDict[i] = aList.count(i)
print (fDict)

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:

36 What will be the output of following program:


dict = {'Name': 'Rohit', 'Age':
30} print (dict.items())

Ans:

37 Write a Python program to iterate over dictionaries using for loops


Ans:

38 Write a Python script to merge two Python dictionaries

Ans:

18 | P a g
e
39 Write a Python program to get the maximum and minimum value in a
dictionary.

Ans:

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


Ans:

41 Write a Python program to remove duplicates from Dictionary

Ans:

19 | P a g
e
42 Write a Python program to check a dictionary is empty or not.

Ans:

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

Ans:

44 Write a Python script to concatenate following dictionaries to create a


new one
.
dic1 = {1:10,2:20}
dic2 = {3:30,4:40}
dic3 = {5:50,6:60}
Ans:

20 | P a g
e
45 Python Program to Add a Key-Value Pair to the Dictionary

Ans:

46 Python Program to Check if a Given Key Exists in a Dictionary or Not

Ans:

47 Python Program to Generate a Dictionary that Contains Numbers


(between 1 and n) in the Form (x,x*x).

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

You might also like