0% found this document useful (0 votes)
2 views13 pages

Dictionary in Python

Uploaded by

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

Dictionary in Python

Uploaded by

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

ADHARSH VIDHYALAYA PUBLIC SCHOOL

Sub-Topic : DICTIONARY MANIPULATION


Name of Student :
________________________________________
1. What will be the output of the following Python code snippet?
d = {"john":40, "peter":45}

"john" in d

2. What will be the output of the following Python code snippet?


d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

d1 == d2

3. What will be the output of the following Python code snippet?


d = {"john":40, "peter":45} d["john"]

4. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?
a) d.delete(“john”:40)

b) d.delete(“john”)

c) del d[“john”]

d) del d(“john”:40)
5. What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}

print(list(d.keys()))

6. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the
expression d[“susan”]?

a) Since “susan” is not a value in the set, Python raises a KeyError exception

b) It is executed fine and no exception is raised, and it returns None

c) Since “susan” is not a key in the set, Python raises a KeyError exception

d) Since “susan” is not a key in the set, Python raises a syntax error
7. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys

b) The keys of a dictionary can be accessed using values

c) Dictionaries aren’t ordered

d) Dictionaries are mutable


8. Which of the following is not a declaration of the dictionary?

a) {1: ‘A’, 2: ‘B’}

b) dict([[1,”A”],[2,”B”]])

c) {1,”A”,2”B”}

d) { }

9. What will be the output of the following Python code snippet?

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

for i,j in a.items():

print(i,j,end=" ")

a) 1A2B3C

b) 1 2 3

c) A B C

d) 1:”A” 2:”B” 3:”C”

10. What will be the output of the following Python code snippet?

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

print(a.get(1,4))

a) 1

b) A

c) 4

d) Invalid syntax for get method


11. What will be the output of the following Python code snippet?

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

print(a.get(5,4))

a) Error, invalid syntax

b) A

c) 5

d) 4
12. What will be the output of the following Python code?

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

b={4:"D",5:"E"}

a.update(b)

print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}

b) Method update() doesn’t exist for dictionaries

c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}

d) {4: ‘D’, 5: ‘E’}

13. What will be the output of the following Python code?

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

b=a.copy()

b[2]="D"

print(a)

a)Error, copy() method doesn’t exist for dictionaries


b) {1: ‘A’, 2: ‘B’, 3: ‘C’}

c) {1: ‘A’, 2: ‘D’, 3: ‘C’}

d) “None” is printed

14. What will be the output of the following Python code?

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

a.clear()

print(a)

a) None

b) { None:None, None:None, None:None}

c) {1:None, 2:None, 3:None}


d) { }

15. What will be the output of the following Python code?

a={1:5,2:3,3:4}

a.pop(3)

print(a)

a) {1: 5}

b) {1: 5, 2: 3}

c) Error, syntax error for pop() method

d) {1: 5, 3: 4}
16. What will be the output of the following Python code?

a={1:5,2:3,3:4}

a.pop(3)

print(a)

a) {1: 5}

b) {1: 5, 2: 3}

c) Error, syntax error for pop() method

d) {1: 5, 3: 4}

17. What will be the output of the following Python code?

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

for i in a:

print(i,end=" ")

a) 1 2 3

b) ‘A’ ‘B’ ‘C’

c) 1 ‘A’ 2 ‘B’ 3 ‘C’


d) Error, it should be: for i in a.items():

18. What will be the output of the following Python code?

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

> a.items()

a) Syntax error

b) dict_items([(‘A’), (‘B’), (‘C’)])

c) dict_items([(1,2,3)])

d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])


1. Answer the following questions.

(a) Can we modify the keys in the dictionary?


(b) Is dictionary mutable? Why?
(c) What type of objects can be used as keys in dictionary?
(d) Which key word is used to sort the records of a table in ascending order?
(e) Which clause is used to apply condition on group by?
(f) Which clause is used to sort the records of a table?

2. What will be the output of the following question?


Dc1={ }
Dc1[1]=1
Dc1[‘1’]=2
Dc1[1.0]=4
Sum=0
for k in Dc1:
Sum + = Dc1[k]
print(Sum)

3. Predict the output of the following code fragment.

fruit={ }
F1 = [‘Apple’ , ‘Banana’ , ‘apple’ , ‘Banana’]
for index in F1:
if index in fruit:
fruit[index]+=1
else:
fruit[index]=1
print(fruit)
print(len(fruit))

4. How are dictionaries different from lists?

5. What will be the output produced by the following code?

d1={5:"number","a":"string",(1,2):"tuple"}

print("Dictionary contents")

for x in d1.keys():

print(x,':',d1[x],end=' ')

print(d1[x]*3)

print()
6. Predict the output of the following code fragment.

list1=[2,3,3,2,5,3,2,5,1,1]
counts={}
ct=0

lst=[]

for num in list1:

if num not in lst:

lst.append(num)

counts[num]=0

ct=ct+1

counts[num]=counts[num]+1

print(counts)

for key in counts.keys():

counts[key]=key*counts[key]

print(counts)

You might also like