🌟 Diplomatech Academy 🌟
Manual Solutions and outputs
Python Programming Course Code: 314004
-Mohade Sir©
Practical No. 12: Write python program to perform following operation
on theDictionary: Create, Access, Update, Delete, Looping through
Dictionary, Create Dictionary from list
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.
✅ Python Program to Sort a Dictionary by Value (Ascending & Descending):
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)
# Sorting in descending order by value
desc_sort = dict(sorted(my_dict.items(), key=lambda item: item[1], reverse=True))
ad
print("Descending Order:", desc_sort)
✅ Output:
oh
Ascending Order: {'b': 10, 'c': 20, 'a': 30}
Descending Order: {'a': 30, 'c': 20, 'b': 10}
M
2) Write a Python script to concatenate following dictionaries to create a new
one.
Sample Dictionary:
dic1 = {1:10, 2:20}
dic2 = {3:30, 4:40}
dic3 = {5:50,6:60}
Ans.
✅ Python Program to Concatenate Dictionaries:
# Sample dictionaries
dic1 = {1: 10, 2: 20}
dic2 = {3: 30, 4: 40}
dic3 = {5: 50, 6: 60}
# Concatenating dictionaries
result = {}
for d in (dic1, dic2, dic3):
ir
result.update(d)
print("Concatenated Dictionary:", result)
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
intersection of sets, union
of sets, set difference, symmetric difference, clear a set.
✅ Python Program to Perform Set Operations
M
# Creating two sets
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
print("Union:", set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8}
# Set Difference (elements in set1 but not in set2)
ir
print("Difference (set1 - set2):", set1.difference(set2)) # {1, 2, 3}
eS
# Symmetric Difference (elements not common in both sets)
print("Symmetric Difference:", set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7,
8}
ad
# Clear the set
set1.clear()
oh
print("Set1 after clear():", set1) # set()
✅ Output:
M
Intersection: {4, 5}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Difference (set1 - set2): {1, 2, 3}
Symmetric Difference: {1, 2, 3, 6, 7, 8}
Set1 after clear(): set()
4) Write a Python program to combine two dictionary adding values for
common keys.
ir
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
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
for key in d1:
result[key] = d1[key] + d2.get(key, 0)
for key in d2:
M
if key not in result:
result[key] = d2[key]
print("Combined Dictionary:", result)
✅ 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.
✅ Python Program to Find the Highest 3 Values in a Dictionary:
# Sample dictionary
ir
my_dict = {'a': 100, 'b': 300, 'c': 200, 'd': 400, 'e': 250}
eS
# Finding highest 3 values
highest_3 = sorted(my_dict.values(), reverse=True)[:3]
ad
print("Highest 3 values:", highest_3)
✅ Output:
oh
Highest 3 values: [400, 300, 250]
M
6) What is the output of the following program?
dictionary1 = {'Google' : 1, 'Facebook' : 2, 'Microsoft' : 3}
dictionary2 = {'GFG' : 1, 'Microsoft' : 2, 'Youtube' : 3 }
dictionary1.update(dictionary2)
for key, values in dictionary1.items():
print(key,values)
Ans.
✅ Output of the given program:
dictionary1 = {'Google': 1, 'Facebook': 2, 'Microsoft': 3}
dictionary2 = {'GFG': 1, 'Microsoft': 2, 'Youtube': 3}
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