Basic Python Programming (313011) Practical No.9: Create a program to perform the following operations on Dictionaries in Python: a) Create b) Access c) Update d) Delete e) Looping through Dictionary
Python dictionaries allow us to associate a value to a unique key, and then to quickly access this value. Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered, changeable and does not allow duplicates.
Practical related Questions
1. Write the output of the following program.
dict = {1:"Photodiode", 2:"Zener diode", 3:" Schottky diode",4: "P-N junction diode"}
del dict[1];
for key, val in dict.items():
print(key)
for key, val in dict.items():
print(key,values)
del dictionary:
for key, val in dict.items():
print(key)
Answer:
2
3
4
2 Zener diode
3 Schottky diode
4 P-N junction diode
2. Write the output of the following program.
dict1 = {'Amplifiers': ‘BJT’, 'Transistor': ‘PNP’}
dict2 = {'FET':10, 'MOSFET': 5}
dict1.update(dict2)
print(dict1)
Answer:
{'Amplifiers': 'BJT', 'Transistor': 'PNP', 'FET': 10, 'MOSFET': 5}
Exercise
1. Write a Python script to iterate over dictionaries using for loops.
Answer:
2. Write a Python script to change the value of a dictionary element by referring to its key.
Answer: