This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: Set

Set operations

face Josiah Wang

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! 😜

1 2 3 4

Question 1

What is the output after executing the following piece of code? Type Error if you are expecting an error.

1
2
3
4
x = {1, 2}
y = {2, 3}
z = x.intersection(y)
print(z)

{2}
Explanation:

The .intersection() method returns the intersection between the two sets, i.e. set of elements that occur in both sets. In this case, this will be a set with a single element 2 (don't forget - it returns a set, not an int). You can also use z = x & y to achieve the same thing. Note that this method does NOT modify the value of x and y.

Question 2

What is the output after executing the following piece of code? Type Error if you are expecting an error.

1
2
3
4
x = {1, 2}
y = {2, 3}
z = x - y
print(z)

{1}
Explanation:

The - operator returns the set difference between the two sets, i.e. set of elements that occur in the first set but not in the second set. In this case, this will be a set with a single element 1. You can also use the .difference() method instead. To obtain the symmetric difference between two sets (elements that occur in either set but not both sets), use the ^ operator or the .symmetric_difference() method, which will return {1, 3} in this example.

Question 3

What is the output after executing the following piece of code? Type Error if you are expecting an error.

1
2
3
4
x = {1, 2}
y = {2, 3}
z = x | y
print(z)

{1,2,3}
Explanation:

The | operator returns the set union between the two sets, i.e. combines the elements in both sets. An alternative will be to use the .union() method (which might make your code more readable). Again, this operation/method does not modify the value of x and y.

Question 4

What is the output after executing the following piece of code? Type Error if you are expecting an error.

1
2
3
4
x = {1, 2}
y = {1, 2, 3}
z = x < y
print(z)

True
Explanation:

The < operator checks whether the set on the left hand side is a subset of the set on the right hand side. You can also use the .issubset() method to achieve the same thing, especially if you find the < operator confusing and hard to understand.