23BBS0100 Py Da2
23BBS0100 Py Da2
Lab Assignment-2
Ashutosh Dash
23BBS0100
6) Write a Python program to calculate the difference between the two lists.
9) In a supermarket there are two sections S1 and S2. The sales details of item1 to item n of
section1 and item1 to item p of section2 are maintained in a sorted order. Write a program
to merge the elements of the two sorted lists to form the consolidated list.
t1=(1,2,3,4,5,6)
l1=list(t1)
print(l1)
l1=[1,2,3,4,5,6]
t1=tuple(l1)
print(t1)
14) Write a Python program to replace the last value of tuples in a list.
Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]
15) Write a python program to find the sum of all items in a dictionary.
d={'A':1,'B':2,'C':3,'D':4}
sum=0
l=d.keys()
for i in d:
sum=sum+d[i]
print("Sum of Values is ",sum)
16) Given two Python sets, write a Python program to update the first set with items that exist
only in the first set and not in the second set. DO NOT USE ANY BUILT-IN FUNCTIONS
a={1,2,3,4,5,6}
b={3,4,5,6,7,8}
s=set()
for i in a:
if i not in b:
s.add(i)
a.clear()
a.update(s)
print("New Set A is ",a)
17) 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}
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}
final_dic={}
final_dic.update(dic1)
final_dic.update(dic2)
final_dic.update(dic3)
print("New Dictionary: ",final_dic)
18) Write a Python program to find the highest 3 values of corresponding keys in a dictionary
my_dict = {'a': 30, 'b': 50, 'c': 20, 'd': 70, 'e': 40}
items_list = list(my_dict.items())
for i in range(len(items_list)):
for j in range(i + 1, len(items_list)):
if items_list[i][1] < items_list[j][1]:
items_list[i], items_list[j] = items_list[j], items_list[i]
top_3_items = items_list[:3]
result_dict = dict(top_3_items)
print("Top 3 values of corresponding keys:", result_dict)
19) Write a Python program to create a set.
my_set = {1, 2, 3, 4, 5}
print("Created Set:", my_set)
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
print("Set after adding 6:", my_set)
my_set.remove(3)
print("Set after removing 3:", my_set)
21) Remove items from set1 that are not common to both set1 and set2 without using any built-in
functions.
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
common_items = set1.intersection(set2)
set1 = set1.intersection(common_items)
print("Set1 after removing non-common items:", set1)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print("Union of sets:", union_set)
23) Write a Python program to check if a set is a subset of another set.set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
is_subset = set1.issubset(set2)
print("Is set1 a subset of set2?", is_subset)
set1 = {1, 2, 3}
frozen_set = frozenset(set1)
print("Original set:", set1)
print("Frozen set:", frozen_set)
25) Write a Python program to find the maximum and minimum values in a set.