0% found this document useful (0 votes)
18 views8 pages

Practical - 7

The document contains a series of Python programming exercises focused on dictionaries and sets. It includes tasks such as concatenating dictionaries, checking for key existence, generating a dictionary of squares, merging dictionaries, finding maximum and minimum values, and performing set operations like union and intersection. Each task is accompanied by sample code and expected outputs.

Uploaded by

Pratik Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Practical - 7

The document contains a series of Python programming exercises focused on dictionaries and sets. It includes tasks such as concatenating dictionaries, checking for key existence, generating a dictionary of squares, merging dictionaries, finding maximum and minimum values, and performing set operations like union and intersection. Each task is accompanied by sample code and expected outputs.

Uploaded by

Pratik Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Programs on set and dictionary

1.Write a Python program 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}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
2.Write a Python program to check whether a given key already exists in a dictionary
3.Write a Python script to generate and print a dictionary that contains a number
(between 1 and n) in the form (x, x*x)
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
4.Write a Python program to merge two Python dictionaries
5.Write a Python program to get the maximum and minimum value in a dictionary
6.Write a Python program to create set difference, Union and intersection
7.Write a Python program to check if two given sets have no elements in common
1. Write a Python program 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}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3): dic4.update(d)
print(dic4)

Output

{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}


2.Write a Python program to check whether a given key already exists in a dictionary

def ck(dic, key):


if key in dic.keys():
print(key," Exist, ", end =" ")
print("value =", dic[key])
else:
print(key," Not Exist")

# Driver Code
dic1 = {'val1': 10, 'val2':20, 'val3':30}
key1 = 'val3'
print(dic1)
ck(dic1, key1)

key1 = 'val5'
ck(dic1, key1)

Output

{'val1': 10, 'val2': 20, 'val3': 30}


val3 Exist, value = 30
val5 Not Exist
3.Write a Python script to generate and print a dictionary that contains a number
(between 1 and n) in the form (x, x*x)
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

m = int(input("Enter any number.. "))


dict_gen = dict()
for i in range(1,m+1):
dict_gen[i] = i*i
print("The Dictionary generated = : \n ")
print(dict_gen)

Output

Enter any number.. 6


The Dictionary generated = :

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}


4.Write a Python program to merge two Python dictionaries

#Program using ** Operator

dict1 = {1: 'a', 2: 'b'}


dict2 = {3: 'c', 4: 'd'}

print({**dict1, **dict2})

#Program using copy and update

dict1 = {1: 'a', 2: 'b'}


dict2 = {3: 'c', 4: 'd'}
dict3 = dict2.copy()
dict3.update(dict1)

print(dict3)

Output

{1: 'a', 2: 'b', 3: 'c', 4: 'd'}


5.Write a Python program to get the maximum and minimum value in a dictionary

sal={"e1":7000 , "e2":7800 , "e3":6400 , "e4":8500 , "e5":5000}

v= sal.values()
max1 = max(v)
min1 = min(v)

print("Maximum :",max1)
print("Minimum :",min1)

Output

Maximum : 8500
Minimum : 5000
6.Write a Python program to create set difference, Union and intersection

set1 = {9, 2, 4, 6, 8}
set2 = {7, 2, 3, 4, 5}
print("Union :", set1 | set2)
print("Intersection :",set1 & set2)
print("Difference :", set1 - set2)
print("Symmetric difference :", set1 ^ set2)

Output

Union : {2, 3, 4, 5, 6, 7, 8, 9}
Intersection : {2, 4}
Difference : {8, 9, 6}
Symmetric difference : {3, 5, 6, 7, 8, 9}
7.Write a Python program to check if two given sets have no elements in common

x = {11,22,31,45}
y = {44,51,22,74}
z = {84}
print("Original set elements:")
print(x)
print(y)
print(z)
print("\nConfirm two given sets have no element(s) in common:")
print("\nCompare x and y:")
print(x.isdisjoint(y))
print("\nCompare x and z:")
print(z.isdisjoint(x))
print("\nCompare y and z:")
print(y.isdisjoint(z))

Original set elements:


{11, 45, 22, 31}
{74, 51, 44, 22}
{84}

Confirm two given sets have no element(s) in common:

Compare x and y:
False

Compare x and z:
True

Compare y and z:
True

You might also like