0% found this document useful (0 votes)
33 views6 pages

Yana Srivastava - Python Exp 2.3

Yana Srivastava completed a programming in Python lab worksheet. The worksheet involved writing code to combine two dictionaries by adding values for common keys, and finding the highest 3 values of keys in a dictionary. Yana wrote Python code to combine two dictionaries by updating one with the other's values if the keys matched, and used the nlargest function to find and print the 3 largest values from a dictionary based on the values of its keys. The code was run in a Python IDE and appropriate outputs were produced.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
33 views6 pages

Yana Srivastava - Python Exp 2.3

Yana Srivastava completed a programming in Python lab worksheet. The worksheet involved writing code to combine two dictionaries by adding values for common keys, and finding the highest 3 values of keys in a dictionary. Yana wrote Python code to combine two dictionaries by updating one with the other's values if the keys matched, and used the nlargest function to find and print the 3 largest values from a dictionary based on the values of its keys. The code was run in a Python IDE and appropriate outputs were produced.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

WORKSHEET – 2.

Name: Yana Srivastava


Section/Group: 611 / “A”
UID: 20BCS2279
Subject: Programming in Python Lab
Date of Submission: 28.3.2022
Branch: BE CSE (4th Semester)

Aim:
Write a python code that will implement basic concepts of dictionaries.

Task to be done:
1. 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}.
2. Write a Python program to find the highest 3 values of corresponding keys
in a dictionary.

Apparatus / Simulator Used:


Python IDE

CHANDIGARH UNIVERSITY PROGRAMMING IN PYTHON LAB


Algorithm / Flowchart:
1. To combine two dictionary adding values for common keys:
 Start the program.
 Take two dictionaries.
 Perform all arithmetic operations.
 Print all the values with a suitable message.
 End the program.

2. To find the highest 3 values of corresponding keys in a dictionary:


 Start the program.
 Take a dictionary.
 Check the highest three values of corresponding keys in a dictionary .
 Print all the values with a suitable message.
 End the program.

CHANDIGARH UNIVERSITY PROGRAMMING IN PYTHON LAB


Code:
1. To combine two dictionary adding values for common keys:

d1 = {'a': 100, 'b': 200, 'c':300}


d2 = {'a': 300, 'b': 200, 'd':400}
d3 = dict(d1)
d3.update(d2)
for i, j in d1.items():
for x, y in d2.items():
if i == x:
d3[i]=(j+y)
print(d3)

CHANDIGARH UNIVERSITY PROGRAMMING IN PYTHON LAB


2. To find the highest 3 values of corresponding keys in a
dictionary:

from heapq import nlargest


my_dict = {'a':500, 'b':5874, 'c': 560,'d':400, 'e':5874, 'f': 20}
three_largest = nlargest(3, my_dict, key=my_dict.get)
print(three_largest)

Result / Output:
1. To combine two dictionary adding values for common keys:

CHANDIGARH UNIVERSITY PROGRAMMING IN PYTHON LAB


2.To find the highest 3 values of corresponding keys in a dictionary:

CHANDIGARH UNIVERSITY PROGRAMMING IN PYTHON LAB


Learning Outcomes:
1. Learn how to implement the dictionaries of python.
2. Learn about dictionaries.
3. Learn about different dictionaries their uses.
4. Learn about import of dictionaries.
5. Learn how to write code in python , about indentation.

CHANDIGARH UNIVERSITY PROGRAMMING IN PYTHON LAB

You might also like