0% found this document useful (0 votes)
2 views4 pages

Practical 09

This lab manual provides practical exercises for programming in Python, including dictionary operations such as merging, sorting, and finding unique values. It includes solved examples demonstrating the output of various Python scripts and exercises for students to practice. The manual is prepared by Sayyed Waliullah from Jamia Polytechnic Akkalkuwa.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Practical 09

This lab manual provides practical exercises for programming in Python, including dictionary operations such as merging, sorting, and finding unique values. It includes solved examples demonstrating the output of various Python scripts and exercises for students to practice. The manual is prepared by Sayyed Waliullah from Jamia Polytechnic Akkalkuwa.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming with ‘Python’ Practical: 09 Lab Manual (Solved)

Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code

1. What is the output of the following program? dictionary = {'MSBTE' : 'Maharashtra


State Board of Technical Education', 'CO' : 'Computer Engineering', 'SEM' : 'Sixth'
} del dictionary['CO']; for key, values in dictionary.items(): print(key)
dictionary.clear(); for key, values in dictionary.items(): print(key) del dictionary; for
key, values in dictionary.items(): print(key)
Output:
MSBTE
SEM
Traceback (most recent call last):
File "c:\Sayyed Lectures\PyCoding\A2.py", line 19, in <module>
for key, values in dictionary.items():
^^^^^^^^^^
NameError: name 'dictionary' is not defined

Explanation:
1. The dictionary initially contains three key-value pairs.
2. The del dictionary['CO'] statement removes the 'CO' key.
3. The first for loop prints the remaining keys: 'MSBTE' and 'SEM'.
4. The dictionary.clear() statement removes all items, making it an empty dictionary.
5. The second for loop does not print anything because the dictionary is empty.
6. The del dictionary statement deletes the dictionary entirely.

Jamia Polytechnic Akkalkuwa Page No:01 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 09 Lab Manual (Solved)

7. The third for loop attempts to access dictionary, but since it has been deleted, Python
raises a NameError.
2. 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) # Merges dictionary2 into dictionary1

for key, values in dictionary1.items():


print(key, values)
Prints all key-value pairs of the updated dictionary1

Loop Prints Key-Value Pairs:


Google 1
Facebook 2
Microsoft 2
GFG 1
Youtube 3
3. What is the output of the following program? temp = dict() temp['key1'] = {'key1' :
44, 'key2' : 566} temp['key2'] = [1, 2, 3, 4] for (key, values) in temp.items():
print(values, end = "")
Output:
{'key1': 44, 'key2': 566}[1, 2, 3, 4]

Exercise
1. Write a Python script to sort (ascending and descending) a dictionary by value.
my_dict = {'apple': 3, 'banana': 1, 'cherry': 5, 'date': 2}

# Sorting in ascending order by value


asc_sorted = dict(sorted(my_dict.items(), key=lambda item: item[1]))
print("Ascending Order:", asc_sorted)

Jamia Polytechnic Akkalkuwa Page No:02 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 09 Lab Manual (Solved)

# Sorting in descending order by value


desc_sorted = dict(sorted(my_dict.items(), key=lambda item: item[1], reverse=True))
print("Descending Order:", desc_sorted)
Output:
Ascending Order: {'banana': 1, 'date': 2, 'apple': 3, 'cherry': 5}
Descending Order: {'cherry': 5, 'apple': 3, 'date': 2, 'banana': 1}

2. Write a Python script to concatenate following dictionaries to create a new one. a.


Sample Dictionary: b. dic1 = {1:10, 2:20} c. dic2 = {3:30, 4:40} d. dic3 = {5:50,6:60}
# Given dictionaries
dic1 = {1: 10, 2: 20}
dic2 = {3: 30, 4: 40}
dic3 = {5: 50, 6: 60}

# Concatenating dictionaries
new_dict = {}
new_dict.update(dic1)
new_dict.update(dic2)
new_dict.update(dic3)

# Printing the final dictionary


print("Concatenated Dictionary:", new_dict)

3. Write a Python program to combine two dictionary adding values for common keys. a.
d1 = {'a': 100, 'b': 200, 'c':300} b. d2 = {'a': 300, 'b': 200, 'd':400}

from collections import Counter


# Given dictionaries
d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}

# Using Counter to add values for common keys


combined_dict = dict(Counter(d1) + Counter(d2))
# Printing the final dictionary
print("Combined Dictionary:", combined_dict)

Jamia Polytechnic Akkalkuwa Page No:03 Prepared by: Sayyed Waliullah


Programming with ‘Python’ Practical: 09 Lab Manual (Solved)

• Counter(d1) + Counter(d2) automatically adds values for matching keys.


• Keys that are unique in either dictionary are included as they are.
• Final result is converted back to a regular dictionary.

4. Write a Python program to print all unique values in a dictionary. a. Sample Data:
[{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"},
{"V":"S009"}, {"VIII":"S007"}]
# Sample Data
data = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"},
{"VII": "S005"}, {"V": "S009"}, {"VIII": "S007"}]

# Using a set to store unique values


unique_values = set()
# Loop through the list of dictionaries
for dictionary in data:
for value in dictionary.values():
unique_values.add(value)

# Print unique values


print("Unique Values:", unique_values)

• A set is used to store values because sets automatically eliminate duplicates.


• The program loops through each dictionary, and then through the values of each dictionary,
adding them to the set.
• The final set contains only the unique values.
5. Write a Python program to find the highest 3 values in a dictionary.
# Sample dictionary
my_dict = {'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 500}

# Sorting the dictionary by values in descending order and getting the top 3 items
highest_3 = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)[:3]

# Printing the top 3 values


print("Top 3 highest values:")
for key, value in highest_3:
print(f"{key}: {value}")

Jamia Polytechnic Akkalkuwa Page No:04 Prepared by: Sayyed Waliullah

You might also like