Skip to content

Commit a275979

Browse files
authored
Create sets_exercises_python.py
1 parent caf06ad commit a275979

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

sets_exercises_python.py

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
"""
2+
1. Write a Python program to create a set.
3+
"""
4+
5+
my_set = set()
6+
my_second_set = {'a', 'b'}
7+
print(my_set, type(my_set))
8+
print(my_second_set, type(my_second_set))
9+
10+
"""
11+
2. Write a Python program to iterate over sets.
12+
"""
13+
14+
my_set = {'a', 'b'}
15+
16+
for i in my_set:
17+
print(i)
18+
19+
"""
20+
3. Write a Python program to add member(s) in a set.
21+
"""
22+
23+
my_set = {'a', 'b'}
24+
my_set.add('c')
25+
print(my_set)
26+
27+
"""
28+
4. Write a Python program to remove item(s) from set
29+
"""
30+
31+
my_set = {'a', 'b'}
32+
my_set.discard('a')
33+
print(my_set)
34+
35+
my_set = {'a', 'b'}
36+
my_set.remove('a')
37+
print(my_set)
38+
39+
my_set = {'a', 'b'}
40+
my_set.pop()
41+
print(my_set)
42+
43+
"""
44+
5. Write a Python program to remove an item from a set if it is present in the set.
45+
"""
46+
47+
my_set = {'a', 'b'}
48+
my_set.discard('a')
49+
my_set.discard('c')
50+
print(my_set)
51+
52+
"""
53+
6. Write a Python program to create an intersection of sets.
54+
"""
55+
set_one = {'a', 'b', 'c'}
56+
set_two = {'a', 'x', 'f'}
57+
58+
set_inter1 = set_one.intersection(set_two)
59+
set_inter2 = set_one & set_two
60+
61+
print(set_inter1)
62+
print(set_inter2)
63+
64+
"""
65+
7. Write a Python program to create a union of sets.
66+
"""
67+
68+
set_one = {'a', 'b', 'c'}
69+
set_two = {'a', 'x', 'f'}
70+
71+
set_inter1 = set_one.union(set_two)
72+
set_inter2 = set_one | set_two
73+
74+
print(set_inter1)
75+
print(set_inter2)
76+
77+
"""
78+
8. Write a Python program to create set difference.
79+
"""
80+
81+
set_one = {'a', 'b', 'c'}
82+
set_two = {'a', 'x', 'f'}
83+
84+
set_inter1 = set_one.difference(set_two)
85+
set_inter2 = set_one - set_two
86+
87+
print(set_inter1)
88+
print(set_inter2)
89+
90+
"""
91+
9. Write a Python program to create a symmetric difference.
92+
"""
93+
94+
set_one = {'a', 'b', 'c'}
95+
set_two = {'a', 'x', 'f'}
96+
97+
set_inter1 = set_one.symmetric_difference(set_two)
98+
set_inter2 = set_one ^ set_two
99+
100+
print(set_inter1)
101+
print(set_inter2)
102+
103+
"""
104+
10. Write a Python program to issubset and issuperset.
105+
"""
106+
107+
set_one = {'a', 'b', 'c'}
108+
set_two = {'a', 'b'}
109+
110+
print(set_two.issubset(set_one))
111+
print(set_one.issuperset(set_two))
112+
113+
"""
114+
11. Write a Python program to create a shallow copy of sets.
115+
Note : Shallow copy is a bit-wise copy of an object. A new object is created
116+
that has an exact copy of the values in the original object.
117+
"""
118+
119+
set_one = {'a', 'b', 'c'}
120+
set_two = set_one.copy()
121+
print(set_one)
122+
print(set_two)
123+
124+
"""
125+
12. Write a Python program to clear a set.
126+
"""
127+
128+
set_one = {'a', 'b', 'c'}
129+
set_one.clear()
130+
print(set_one)
131+
132+
"""
133+
13. Write a Python program to use of frozensets.
134+
"""
135+
136+
set_one = frozenset((1, 2, 3))
137+
set_two = set((1, 2, 3))
138+
list_one = [4, 5, 6]
139+
print (set_one == set_two)
140+
print(set_one.isdisjoint(list_one))
141+
142+
"""
143+
14. Write a Python program to find maximum and the minimum value in a set.
144+
"""
145+
146+
set_one = frozenset((1, 2, 3))
147+
print(max(set_one))
148+
print(min(set_one))
149+
150+
"""
151+
15. Write a Python program to find the length of a set.
152+
"""
153+
154+
set_one = frozenset((1, 2, 3))
155+
print(len(set_one))
156+
length = 0
157+
for i in set_one:
158+
length += 1
159+
print(length)

0 commit comments

Comments
 (0)