0% found this document useful (0 votes)
10 views

Assignment 7 Ankit shukla

Yes
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)
10 views

Assignment 7 Ankit shukla

Yes
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/ 7

Assignment 6

assignment-6

July 31, 2024

1. Write a Python program to create a set.


[1]: s1 = {1,2,3,4,'ram',34.2,3+2j,'soham'}
print("Set is : ",s1)

Set is : {1, 2, 3, 4, 34.2, 'ram', (3+2j), 'soham'}

2. Write a Python program to iterate over sets.


[2]: s1 = {1,2,3,4,'ram',34.2,3+2j,'soham'}
print("Set is : ",s1)

for i in s1:
print(i)

Set is : {1, 2, 3, 4, 34.2, 'ram', (3+2j), 'soham'}


1
2
3
4
34.2
ram
(3+2j)
soham

3. Write a Python program to add member(s) to a set.


[5]: s1 = {1,2,3,4,5}
s1.add(20)
s1

[5]: {1, 2, 3, 4, 5, 20}

4. Write a Python program to remove item(s) from a given set.


[7]: s1 = {1,2,3,4,5,6,7,8,9,10}
print("Before removing : ", s1)
s1.remove(5)
print("After Removing : ",s1)

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)

Enter The Element You Wants To Remove : 2


Succefffully Removed : None {1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

6. Write a Python program to create an intersection of sets.


[15]: s1 = {'a','b','c','d','e','f','g'}
s2 = {'a','i','c','e','g','m','n'}
result = s1.intersection(s2)
print("After intersection : ",result)

After intersection : {'c', 'e', 'g', 'a'}

7. Write a Python program to create a union of sets.


[16]: set1 = {2, 4, 6, 8, 10}
set2 = {3, 6, 9, 12}

s3 = set1.union(set2)
print(s3)

{2, 3, 4, 6, 8, 9, 10, 12}

8. Write a Python program to create set difference.


[17]: s1 = {1,2,3,4,5,6}
s2 = {1,2,4,5,7,8}
s3 = s1.difference(s2)
print(s3)

{3, 6}

9. Write a Python program to create a symmetric difference.


[18]: s1 = {1,2,3,4,5,6}
s2 = {1,2,4,5,7,8}
s3 = s1.symmetric_difference(s2)
print(s3)

{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

11. Write a Python program to create a shallow copy of sets.


[25]: og_set = {1, 2, 3, 4}

shallow = og_set.copy()

shallow.add(5)
print("Original Set:", og_set)
print("Shallow Copy:", shallow)

Original Set: {1, 2, 3, 4}


Shallow Copy: {1, 2, 3, 4, 5}

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()

13. Write a Python program that uses frozensets.


[ ]:

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

15. Write a Python program to find the length of a set.

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)

Intersection Items : {4, 5}


Removed items : {1, 2, 3}

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 = {}

for string in strings:

words = string.split()

for word in words:

word = word.lower()

unique_words.add(word)

if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1

return unique_words, word_count

5
strings = [
"Hello world",
"Hello from the other side",
"World of Python",
"Python is amazing",
"Hello Python world"
]

unique_words, word_count = count_word_frequencies(strings)

print("Unique words:", unique_words)


print("Word frequencies:", word_count)

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()

for num in nums:


complement = target_sum - num
if complement in seen:
pairs.append((num, complement))
seen.add(num)

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}")

Pairs with sum 10 in the list [3, 4, 5, 6, 7, 8, 9, 1, 2]:


(6, 4)
(7, 3)
(1, 9)
(2, 8)

6
[ ]:

You might also like