[go: up one dir, main page]

0% found this document useful (0 votes)
6 views4 pages

Dictionary Worksheet Full

The document contains a series of programming exercises focused on Python dictionaries, asking for the output of various code snippets. Each exercise tests different functionalities of dictionaries, such as creation, updating, deleting items, and using methods like pop and setdefault. The document serves as a worksheet for practicing dictionary operations in Python.

Uploaded by

Saradha S
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)
6 views4 pages

Dictionary Worksheet Full

The document contains a series of programming exercises focused on Python dictionaries, asking for the output of various code snippets. Each exercise tests different functionalities of dictionaries, such as creation, updating, deleting items, and using methods like pop and setdefault. The document serves as a worksheet for practicing dictionary operations in Python.

Uploaded by

Saradha S
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/ 4

WORKSHEET – DICTIONARY

1. What will be the output of the following code?

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

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

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


a

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

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

4. What will be the output of the following code?

a={i: i*i for i in range(6)}


a

5. What will be the output of the following code?

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

6. What will be the output of the following program?

dictionary = {1:'1', 2:'2', 3:'3'}


del dictionary[1]
dictionary[1] = '10'
del dictionary[2]
print(len(dictionary))

7. Predict the output:


dict1 = {"name": "Mike", "salary": 8000}
temp = dict1.pop("age")
print(temp)

8. What will be the output of the following program?

dict1 = {"key1":1, "key2":2}


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

9. What will be the output of the following program?

dict={"Virat":1,"Rohit":2}
dict.update({"Rahul":2})
print(dict)

10. What will be the output of the following program?

a=dict()
a[1]

11. What will be the output of the following program?

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

12. What will be the output of the following program?

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

13. What will be the output of the following program?

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

14. What will be the output of the following program?

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

15. What will be the output of the following program?

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

16. What will be the output of the following program?

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

17. What will be the output of the following program?

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

18. What will be the output of the following program?

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

19. What will be the output of the following program?

a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

20. What will be the output of the following program?

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

21. What will be the output of the following program?


box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
print(len([crates]))

22. What will be the output of the following program?

dict = {'c': 97, 'a': 96, 'b': 98}


for _ in sorted(dict):
print(dict[_])

23. What will be the output of the following program?

rec = {"Name" : "Python", "Age":"20"}


r = rec.copy()
print(id(r) == id(rec))

24. What will be the output of the 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)

You might also like