1.
Create a Dictionary and Display It
Program:
student = {
"Name": "Rahul",
"Class": 9,
"Section": "A"
}
print(student)
Output:
{'Name': 'Rahul', 'Class': 9, 'Section': 'A'}
2. Access Values Using Keys
Program:
student = {
"Name": "Rahul",
"Roll No": 15
}
print(student["Name"])
print(student["Roll No"])
Output:
Rahul
15
3. Add a New Key-Value Pair
Program:
student = {
"Name": "Anita",
"Class": 9
}
student["Section"] = "B"
print(student)
Output:
{'Name': 'Anita', 'Class': 9, 'Section': 'B'}
4. Change the Value of an Existing Key
Program:
student = {
"Name": "Aman",
"Marks": 75
}
student["Marks"] = 85
print(student)
Output:
{'Name': 'Aman', 'Marks': 85}
5. Delete an Element from a Dictionary
Program:
student = {
"Name": "Riya",
"Age": 14
}
del student["Age"]
print(student)
Output:
{'Name': 'Riya'}
6. Check if a Key Exists in a Dictionary
Program:
student = {
"Name": "Karan",
"Class": 9
}
if "Class" in student:
print("Key exists")
Output:
Key exists
7. Find the Number of Items in a Dictionary
Program:
student = {
"Name": "Neha",
"Roll No": 10,
"Section": "A"
}
print(len(student))
Output:
3
8. Display All Keys and Values
Program:
student = {
"Name": "Rohit",
"Marks": 88
}
for key, value in [Link]():
print(key, ":", value)
Output:
Name : Rohit
Marks : 88
9. Create a Dictionary Using User Input
Program:
student = {}
student["Name"] = input("Enter name: ")
student["Class"] = int(input("Enter class: "))
print(student)
Sample Output:
Enter name: Simran
Enter class: 9
{'Name': 'Simran', 'Class': 9}