-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSets.py
82 lines (52 loc) · 2.09 KB
/
Sets.py
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
#* Sets - Python
# In Python, a set is an unordered collection without duplicate elements. Sets are useful
# to perform operations such as union, intersection, difference, and more. Here I present information
# basic on how to work with sets in Python:
#? Creating Sets:
# You can create sets using curly braces `{}` or the `set()` function.
empty_set = set() # creates an empty set
number_set = {1, 2, 3, 4, 5}
letter_set = set("abcdef")
#? Basic Operations with Sets:
# - **Add Elements:**
set = {1, 2, 3}
set.add(4)
set.update({5, 6})
# - **Delete Elements:**
set = {1, 2, 3, 4, 5}
set.remove(3)
set.discard(4)
# `remove` removes the specified element and raises an error if the element is not present, while
# that `discard` removes the element if it is present, but does not raise an error if it is not found.
# - **Set Operations:**
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
# Union
union_AB = A.union(B) # or also A | b
# Intersection
intersection_AB = A.intersection(B) # or also A & B
# Difference
difference_AB = A.difference(B) # or also A - B
# Symmetric Difference (elements that are in A or B, but not both)
symmetric_difference_AB = A.symmetric_difference(B) # or also A ^ B
#? Verification of Membership:
# You can check if an element is present in a set using the `in` keyword.
set = {1, 2, 3, 4, 5}
print(3 in set) # True
print(6 in set) # False
#? Iteration on Sets:
# You can iterate over the elements of a set using a `for` loop.
set = {1, 2, 3, 4, 5}
for element in set:
print(element)
#? Empty a Set:
# You can clear an array using the `clear()` method.
set = {1, 2, 3}
set.clear() # empty set
#? Immutable Sets:
# Python also has an immutable set type called "frozenset". Unlike the sets
# regular, "frozensets" cannot be modified after they are created.
immutable_set = frozenset([1, 2, 3])
# Sets are useful when you need to perform set math operations or when you want
# make sure there are no duplicate elements in a collection.
# Experiment and practice to get familiar with handling sets in Python!