Python Programming PRACTICAL NO.12 ANSWERS
Python Programming PRACTICAL NO.12 ANSWERS
ir
eS
ad
oh
M
IX Conclusion:
Ans
We have successfully learned how to perform various operations on Python
Dictionaries such as Create, Access, Update, and Delete data.
We also practiced looping through dictionaries to display key-value pairs
efficiently.
We understood how to create dictionaries from lists for structured data storage.
Dictionaries are mutable, making them highly flexible for dynamic data
management.
X Practical related questions
1) Write a Python script to sort (ascending and descending) a dictionary by
value.
Ans.
ir
# Sample dictionary
my_dict = {'a': 30, 'b': 10, 'c': 20}
eS
# Sorting in ascending order by value
asc_sort = dict(sorted(my_dict.items(), key=lambda item: item[1]))
print("Ascending Order:", asc_sort)
✅ Output:
oh
# Concatenating dictionaries
result = {}
for d in (dic1, dic2, dic3):
ir
result.update(d)
eS
✅ Output:
Concatenated Dictionary: {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
ad
3) Write a Python program to perform following operations on set:
oh
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# Intersection of sets
print("Intersection:", set1.intersection(set2)) # {4, 5}
# Union of sets
ir
print("Difference (set1 - set2):", set1.difference(set2)) # {1, 2, 3}
eS
# Symmetric Difference (elements not common in both sets)
set1.clear()
oh
✅ Output:
M
Intersection: {4, 5}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
ir
d1 = {'a': 100, 'b': 200, 'c':300}
eS
✅ Python Program to Combine Two Dictionaries and Add Values for Common
Keys:
# Sample dictionaries
d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}
ad
# Combine dictionaries and add values for common keys
result = {}
oh
✅ Output:
Combined Dictionary: {'a': 400, 'b': 400, 'c': 300, 'd': 400}
5) Write a Python program to find the highest 3 values in a dictionary.
Ans.
ir
my_dict = {'a': 100, 'b': 300, 'c': 200, 'd': 400, 'e': 250}
eS
# Finding highest 3 values
✅ Output:
oh
dictionary1.update(dictionary2)
Ans.
ir
dictionary1.update(dictionary2)
eS
for key, values in dictionary1.items():
print(key, values)
ad
✅ Output:
Google 1
oh
Facebook 2
Microsoft 2
GFG 1
M
Youtube 3
✅ Explanation:
● The .update() method merges dictionary2 into dictionary1.
● If there are common keys, like 'Microsoft', the value from
dictionary2 (2) overwrites the value in dictionary1 (3).
● After merging, dictionary1 contains all unique keys from both
dictionaries, with updated values where keys overlap.
🚀 Note:
Indentation of print() inside the loop is important.
ir
eS
ad
oh
M