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