Lesson 8
Making Objects the Main Star!
Chapter 2: Set
Set operations
Python provides overloaded operators for various set
operations. Since set
is also an object, some of these are also available as methods. I will let you explore these on your own.
Operation | Math Notation | Python Operator | Object Methods |
---|---|---|---|
Union | A \cup B | a_set | b_set |
a_set.union(b_set) |
Intersection | A \cap B | a_set & b_set |
a_set.intersection(b_set) |
Difference | A \setminus B | a_set - b_set |
a_set.difference(b_set) |
Symmetric difference | A \triangle B | a_set ^ b_set |
a_set.symmetric_difference(b_set) |
Membership | x \in B | x in b_set |
|
Non-membership | x \notin B | x not in b_set |
|
Proper Subset | A \subset B | a_set < b_set |
|
Subset | A \subseteq B | a_set <= b_set |
a_set.issubset(b_set) |
Proper Superset | A \supset B | a_set > b_set |
|
Superset | A \supseteq B | a_set >= b_set |
a_set.issuperset(b_set) |
Here is a quick quiz to make sure that you have examined the table above! 😜
❮
❯