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

2.3 - Samad

Uploaded by

Abdul Samad khan
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)
14 views6 pages

2.3 - Samad

Uploaded by

Abdul Samad khan
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/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 2.3

Student Name: Abdul Samad Khan UID: 21BCS1843


Branch: CSE Section/Group: 604-A
Semester: 4th Date of Performance: April 17, 2023
Subject Name: Programming in Python Lab Subject Code: 21CSP-259

AIM: Program to demonstrate creation and accessing of dictionary and apply different kinds of
operations on them.

Python Dictionaries:

Each key is separated from its value by a colon (:), the items are separated by commas, and the
whole thing is enclosed in curly braces. An empty dictionary without any items is written with
just two curly braces, like this: {}.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be
of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

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}

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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


{'a': 300, 'b': 200, 'd': 400}
# Create a new dictionary to store the combined values d3 =
{}
# Iterate through keys in d1 and add their values to d3 for key
in d1: if key in d2:
d3[key] = d1[key] + d2[key] else:

d3[key] = d1[key]

# Iterate through keys in d2 and add any


remaining values to d3 for key in d2: if key
not in d1: d3[key] = d2[key] # Print the
final combined dictionary print(d3)

Output:

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2. Write a Python program to find the sum of all items in a dictionary


my_dict = {'a': 10, 'b': 20, 'c': 30, 'd': 40}

# Method 1
sum_of_values = sum(my_dict.values())
print("Sum of all values in the dictionary: ", sum_of_values)

# Method 2 sum_of_values =
0 for value in my_dict.values():
sum_of_values += value
Output:

3. Write a Python program to get the maximum and minimum values of a dictionary.

my_dict = {'a': 10, 'b': 20, 'c': 30, 'd': 40}

# Method 1 max_value = max(my_dict.values()) min_value = min(my_dict.values())


print("Maximum value in the dictionary: ",
max_value) print("Minimum value in the dictionary: ", min_value)

# Method 2 max_key = max(my_dict, key=my_dict.get) min_key = min(my_dict,


key=my_dict.get) print("Key with maximum value in the dictionary: ", max_key)
print("Key with minimum value in the dictionary: ",min_key)

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

4. Write a Python program to multiply all the items in a dictionary.

my_dict = {'a': 10, 'b': 20, 'c': 30, 'd': 40}

# Method 1
product_of_values = 1 for value in
my_dict.values():
product_of_values *= value
print("Product of all values in the dictionary: ", product_of_values)

# Method 2 from functools import reduce product_of_values = reduce(lambda x,


y: x * y, my_dict.values()) print("Product of all values in
the dictionary: ", product_of_values)

Output:

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Write a Python program to find the highest 3 values of corresponding keys in a dictionary.

# Define a dictionary with some values


d = {'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 500, 'f': 600}
# Create a list of the values in the dictionary values =
list(d.values())
# Sort the list of values in descending order
values.sort(reverse=True)
# Create a new dictionary to store the highest 3 values and their corresponding keys highest
= {}
# Iterate through the original dictionary and add keys to the new dictionary if their values
are in the top 3

for key, value in d.items(): if value in values[:3]:


highest[key] = value # Print the new dictionary
print(highest)

Output:

Abdul Samad Khan 21BCS1843


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Write a Python script to concatenate the following dictionaries to create a new one. Sample
Dictionary : dic1={1:10, 2:20} dic2={3:30, 4:40} dic3={5:50,6:60}.

dic1 = {1: 10, 2: 20} dic2 =


{3: 30, 4: 40} dic3 =
{5: 50, 6: 60}

# Method 1
concatenated_dict = {**dic1, **dic2, **dic3} print("Concatenated dictionary:
", concatenated_dict)

# Method 2
concatenated_dict = {} for d
in [dic1, dic2, dic3]:
concatenated_dict.update(d) print("Concatenated
dictionary: ", concatenated_dict)

Output:

Abdul Samad Khan 21BCS1843

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy