Assignment 7 Ankit shukla
Assignment 7 Ankit shukla
assignment-6
for i in s1:
print(i)
1
Before removing : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
After Removing : {1, 2, 3, 4, 6, 7, 8, 9, 10}
5. Write a Python program to remove an item from a set if it is present in the set.
[12]: s1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
n = int(input("Enter The Element You Wants To Remove : "))
if n in s1:
print("Succefffully Removed :",s1.remove(n),s1)
else:
print("Element not found : ",n)
s3 = set1.union(set2)
print(s3)
{3, 6}
{3, 6, 7, 8}
2
10. Write a Python program to check if a set is a subset of another set. ‘
[23]: s1 = {1,2,4}
s2 = {1,2,4,5,7,8}
if s1.issubset(s2)== True:
print("S1 is Subset of S2")
else:
print("Error")
S1 is Subset of S2
shallow = og_set.copy()
shallow.add(5)
print("Original Set:", og_set)
print("Shallow Copy:", shallow)
12. Write a Python program to remove all elements from a given set.
[26]: s1 = {1,2,3,4,5,6}
s1.clear()
print(s1)
set()
14. Write a Python program to find the maximum and minimum values in a set.
[28]: s1 = {1,2,3,4,5,6,7,8,9,9999}
m = max(s1)
n = min(s1)
print("Max is :",m)
print("Min is :",n)
Max is : 9999
Min is : 1
3
[30]: s1 = {1,2,3,4,5,6,7,8,9,9999}
s2 = len(s1)
s2
[30]: 10
16. Write a Python program to check if a given value is present in a set or not.
[32]: n = 12
s1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
if n in s1:
print("Present")
else:
print("Not Present")
Present
17. Write a Python program to check if two given sets have no elements in common.
[33]: set1={1,2,3,4,5,6,7,8}
set2={1,2,3,4}
print("Set1 : ", set1)
print("Set2 : ", set2)
print(set1.isdisjoint(set2))
print(set2.isdisjoint(set1))
Set1 : {1, 2, 3, 4, 5, 6, 7, 8}
Set2 : {1, 2, 3, 4}
False
False
18. Write a Python program to check if a given set is a superset of itself and a superset
of another given set.
[35]: s1 = {1, 2, 3, 4, 5}
s2 = {1, 2, 3}
self = s1.issuperset(s1)
other = s1.issuperset(s2)
print("Self : ",self)
print("Other : ",other)
Self : True
Other : True
19. Write a Python program to find elements in a given set that are not in another
set.
[36]: s1 = {1, 2, 3, 4, 5}
s2 = {1, 2, 3}
not_elements = s1-s2
4
print(not_elements)
{4, 5}
20. Write a Python program to remove the intersection of a second set with a first
set.
[12]: set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7}
intersection = set1.intersection(set2)
print("Intersection Items : ",intersection)
set1.difference_update(intersection)
print("Removed items : ",set1)
21. Write a Python program to find all the unique words and count the frequency of
occurrence from a given list of strings. Use Python set data type.
[6]: def count_word_frequencies(strings):
unique_words = set()
word_count = {}
words = string.split()
word = word.lower()
unique_words.add(word)
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
5
strings = [
"Hello world",
"Hello from the other side",
"World of Python",
"Python is amazing",
"Hello Python world"
]
22. Write a Python program that finds all pairs of elements in a list whose sum is
equal to a given value.
[2]: def find_pairs_with_sum(nums, target_sum):
pairs = []
seen = set()
return pairs
nums = [3, 4, 5, 6, 7, 8, 9, 1, 2]
target_sum = 10
result_pairs = find_pairs_with_sum(nums, target_sum)
if result_pairs:
print(f"Pairs with sum {target_sum} in the list {nums}:")
for pair in result_pairs:
print(pair)
else:
print(f"No pairs found with sum {target_sum} in the list {nums}")
6
[ ]: