Dictionary Exercise:
Check-out the following exercise on Computer, and write the answer in your class-work
copy.
1. Create a dictionary to store names and marks of 5 students.
2. To add a new key–value pair "City": "Delhi" to an existing dictionary.
3. To print only the keys.
4. To check whether a key exists in a dictionary or not.
5. To delete a key from a dictionary using del
6. Write a program to remove duplicate values from a dictionary.
Example:
Input: {"a":10, "b":20, "c":10, "d":30}
Output: {"a":10, "b":20, "d":30}
7. Write a program to input N numbers and store them in a dictionary as:
Key → number itself
Value → square of number
Example:
Input: 5, 8, 2
Output: {5:25, 8:64, 2:4}
8. A school maintains a dictionary in the following format:
students = {
"A101": ["Rohan", 17, "Maths"],
"A102": ["Neha", 18, "Biology"],
"A103": ["Arjun", 17, "Commerce"]
}
Write programs to:
a) Display all student names
b) Display details of student with ID 'A102'
c) Count how many students belong to the Maths stream
Dictionary Output Exercise: Solve this exercise in your class-work copy
1 D = {"A": 1, "B": 2, "C": 3}
print(len(D))
2 D = {"one": 1, "two": 2, "three": 3}
print(D["two"])
3 d = {"x": 10, "y": 20}
d["z"] = d["x"] + d["y"]
print(d)
4 D = {"a": 10, "b": 20, "c": 30}
D["b"] = 50
print(D)
5 d = {"A": 5, "B": 10, "C": 15}
print("B" in d)
print("D" in d)
6 d = {"p": 1, "q": 2, "r": 3}
for k in d:
print(k, d[k], end=" ")
7 d = {"name": "Aman", "age": 17, "city": "Kota"}
print([Link]("age"))
print([Link]("class"))
8 d = {"A": 2, "B": 4, "C": 6}
print([Link]("B"))
print(d)
9 D = {"a": 1, "b": 2, "c": 3}
x = [Link]()
D["d"] = 4
print(list(x))
10 d1 = {1: "One", 2: "Two"}
d2 = {3: "Three"}
[Link](d2)
print(d1)
11 d = {"a": 1, "b": 2, "c": 3}
for k, v in [Link]():
if v % 2 == 1:
print(k, end=" ")
12 D = {"A": 100, "B": 200, "C": 300}
print([Link]("B", 500))
print([Link]("D", 400))
print(D)
13 d = {"x": [1, 2], "y": [3, 4]}
d["x"].append(5)
print(d)
14 student = {
"name": "Ria",
"marks": {"Math": 92, "Eng": 88}
}
print(student["marks"]["Eng"])