-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsets.py
More file actions
229 lines (159 loc) · 4.6 KB
/
Copy pathsets.py
File metadata and controls
229 lines (159 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
##sets
#1.unordered(no indexing)
##2. Semi - mutable(can add, but cannot change or remove)
##3. unique elements(no duplicates)
## 4. hetrogeneous(can contain different data types)
# creating a set
# a = []
# b = {}
# c = set()# Type conversion
# s = set() # Empty set
# s = {1,2,3,4,5,5,5,5,5,3,5}
# print(type(s))
## methods in sets """"""
"""1.add()
2.update()
3.remove()
4.discard()
5.pop()
6.clear()"""
##1.add()--> for adding single value/element
# s = {8,2,3,4,4,5,2,3,5}
# s.add(6)
#print(s)
##2.update()--> for adding multiple element /values but value must be in list
#s.update([7,8,9])
#print(s)
## 3.remove() --> if value is not present we will get an error
# s.remove(1)
# s.remove(10)
# print(s)
##4. discard()
# s.discard(10)
# print(s)
## 5.pop() --> it remove smallest element from set
# s.pop()
# print(s)
## 6. clear() --> it removes all the element and gives us empty set.
# s.clear()
# print(s)
""""1.INTERSECTION
2.UNION
3.DIFFERENCE
4.symmetric difference
"""
# ### 1.intersection -- it give us common element
# s1 = {1,2,3,4}
# s2 = {2,3,4,6}
# print(f"INTERSETION:--{s1.intersection(s2)}")
# ##2.union -- it give us all element
# print(f"union:--{s1.union(s2)}")
# ## 3. difference-- it give us element
# print(f"difference:--{s1.difference(s2)}")
# print(f"difference:--{s2.difference(s1)}")
# ##4.symmetric difference --- it gives us element which are not common in both set
# print(f"symmetric difference:-{s1.symmetric_difference(s2)}")
# ## frozen set
# fs = {10,20,30,40,50}
# fs = frozenset(fs) ## -- it is a type of set where we can't add ,update or remove values
# #fs.add(60)
# fs.remove(10)
# print(fs)
## Practice
"""
### Question 1 – Create a Set
Create a set of 5 numbers and print it.
**Expected Output:** `{1, 2, 3, 4, 5}` *(order may vary)*
💡 *Hint: Use curly braces `{ }` to create a set.*"""
# a = {1,2,3,4,5}
# b = set(a)
# print(b)
"""
### Question 2 – Sets Remove Duplicates
Create a set from the list below and print it. Notice what happens to duplicates.
**Input:** `[1, 2, 2, 3, 4, 4, 5]`
**Expected Output:** `{1, 2, 3, 4, 5}`
💡 *Hint: Pass the list into `set()`.*"""
# a = [1,2,2,3,4,4,5]
# b = set(a)
# print(b)
"""
### Question 3 – Length of a Set
Find the number of elements in the set.
**Input:** `{10, 20, 30, 40, 50}`
**Expected Output:** `Length = 5`
💡 *Hint: Use `len()`.*"""
# a = {10,20,30,40,50}
# b = set(a)
# print("length of set:", len(b))
"""
### Question 4 – Add an Element
Add the number `6` to the set and print the updated set.
**Input:** `{1, 2, 3, 4, 5}`
**Expected Output:** `{1, 2, 3, 4, 5, 6}` *(order may vary)*
💡 *Hint: Use `.add()`.*"""
# a = {1,2,3,4,5}
# a.add(6)
# print(a)
"""
### Question 5 – Remove an Element
Remove `30` from the set and print the updated set.
**Input:** `{10, 20, 30, 40, 50}`
**Expected Output:** `{10, 20, 40, 50}` *(order may vary)*
💡 *Hint: Use `.remove()` or `.discard()`.*"""
# a = {10,20,30,40,50}
# a.remove(30)
# print(a)
"""
### Question 6 – Check Membership
Check if `"banana"` is present in the set.
**Input:** `{"apple", "banana", "mango"}`, check `"banana"`
**Expected Output:** `Present`
💡 *Hint: Use the `in` keyword.*"""
# fruits = {"apple", "banana", "mango"}
# item = input("Enter fruit to check: ")
# if item in fruits:
# print("Present")
# else:
# print("Not Present")
"""
### Question 7 – Loop Through a Set
Print each element of the set using a loop.
**Input:** `{"red", "green", "blue"}`
**Expected Output:** *(one colour per line, order may vary)*
```
red
green
blue
```
💡 *Hint: Use a `for` loop.*"""
# colour = {"red","green","blue"}
# for color in colour:
# print(color)
"""
### Question 8 – Union of Two Sets
Find the **union** of the two sets (all elements from both).
**Input:** `A = {1, 2, 3}`, `B = {3, 4, 5}`
**Expected Output:** `{1, 2, 3, 4, 5}`
💡 *Hint: Use `.union()` or the `|` operator.*"""
# A = {1,2,3}
# B = {3,4,5}
# print(f"union:--{A.union(B)}")
"""
### Question 9 – Intersection of Two Sets
Find the **intersection** of the two sets (only common elements).
**Input:** `A = {1, 2, 3, 4}`, `B = {3, 4, 5, 6}`
**Expected Output:** `{3, 4}`
💡 *Hint: Use `.intersection()` or the `&` operator.*"""
A = {1,2,3,4}
B = {3,4,5,6}
print(f"INTERSETION:--{A.intersection(B)}")
"""
### Question 10 – Difference of Two Sets
Find the **difference** — elements in A that are **not** in B.
**Input:** `A = {1, 2, 3, 4, 5}`, `B = {3, 4, 5}`
**Expected Output:** `{1, 2}`
💡 *Hint: Use `.difference()` or the `-` operator.*"""
A = {1,2,3,4,5}
B = {3,4,5}
print(f"difference:---{A.difference(B)}")