Skip to content

Commit 26f1ab2

Browse files
committed
sets
1 parent be6f44a commit 26f1ab2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

DSA-Python/DataStructures/sets.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
''' SETS
2+
Sets are a collection of unordered elements that are unique.
3+
'''
4+
5+
set = {1, 2, 1, 4, 'A' ,'B', 'A', 5}
6+
7+
print(f"All the Unique elements of Set are {set}", end="\n\n")
8+
9+
10+
''' ADDING ELEMENTS '''
11+
12+
print("**********Adding Elements**********", end="\n\n")
13+
14+
set.add('Z')
15+
16+
print(f"Set after adding Z is {set}", end="\n\n")
17+
18+
'''
19+
Set Operations
20+
The union() function combines the data present in both sets.
21+
The intersection() function finds the data present in both sets only.
22+
The difference() function deletes the data present in both and outputs data present only in the set passed.
23+
The symmetric_difference() does the same as the difference() function but outputs the data which is remaining in both sets.
24+
'''
25+
26+
print("**********Set Operations**********", end="\n\n")
27+
28+
set = {1, 2, 3, 4}
29+
set_2 = {3, 4, 5, 6, 'B'}
30+
31+
print("UNION ", set.union(set_2), '----------', set | set_2, end="\n\n")
32+
33+
print("INTERSECTION ", set.intersection(set_2), '----------', set & set_2, end="\n\n")
34+
35+
print("DIFFERENCE ", set.difference(set_2), '----------', set - set_2, end="\n\n")
36+
37+
print("SYMMETRIC-DIFFERENCE ",set.symmetric_difference(set_2), '----------', set ^ set_2, end="\n\n")
38+
39+
set.clear()
40+
41+
print(f"Deleted all elements of set {set}", end="\n\n")
42+
43+

0 commit comments

Comments
 (0)