0% found this document useful (0 votes)
49 views2 pages

2.3 Python

This document contains a student worksheet for a Python programming lab. It includes two programming tasks: 1) Write a program to combine two dictionaries by adding values for common keys. 2) Write a program to find the highest 3 values from keys in a dictionary. The source code provided performs these tasks, with outputs showing the results.

Uploaded by

Rishi Kasrija
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)
49 views2 pages

2.3 Python

This document contains a student worksheet for a Python programming lab. It includes two programming tasks: 1) Write a program to combine two dictionaries by adding values for common keys. 2) Write a program to find the highest 3 values from keys in a dictionary. The source code provided performs these tasks, with outputs showing the results.

Uploaded by

Rishi Kasrija
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/ 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 2.3

Student Name: SAGAR ROHILLA UID: 21BCS1383`


Branch: CSE Section/Group: 618 -A
Semester: 4th semester Date of Performance:24/04/23
Subject Name: Programming in Python Lab Subject Code: 21CSP-259

1. Aim:
Program to demonstrate creation, accessing of dictionary and apply different kinds of
operations on them.
 Write a Python program to combine two dictionary adding values for common
keys. d1 = {'a': 100, 'b': 200, 'c':300}, d2 = {'a': 300, 'b': 200, 'd':400}
 Write a Python program to find the highest 3 values of corresponding keys in a
dictionary.

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

print("SAGAR ROHILLA 21BCS1383")


from collections import Counter
dict1 = {'a': 100, 'b': 200, 'c':300}
dict2 = {'a': 300, 'b': 200, 'd':400}
new_dict = Counter(dict1) + Counter(dict2)
print("The new dict is:", new_dict)

#Write a Python program to find the highest 3 values of corresponding keys in a dictionary.
print("SAGAR ROHILLA 21BCS1383")
def highest_3_values_1(my_dict):
result_dict = {}
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
for i in range(3):
result_dict[sorted_dict[i][0]] = sorted_dict[i][1]
return result_dict
my_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69}
print(highest_3_values_1(my_dict))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Screenshot of outputs:
OUTPUT_1

OUTPUT_2

You might also like