Set Operations
Lets see some operations we can use with sets. We apply these to set to get new sets. We’ll see Union (∪), Intersection (∩), and Relative (\) and Absolute (Ac) Compliment.
Watch the video and then answer the questions below.
Eleven-minute video
You can also view this video on YouTube
Set Operations in Java
The java.util.Set
interface does not provide the union (∪), intersection (∩), or difference (\) operations. These would need to be implemented yourself.
Set Operations in Python
Helpfully, Python contains all the basic set operations built in.
Set union (∪) uses the union()
method.
set_a = {"a", "b", "c"}
set_b = {"b", "c", "d"}
set_c = set_a.union(set_b_)
print(set3)
Set intersection (∩) uses the intersection()
method.
set_a = {"a", "b", "c"}
set_b = {"b", "c", "d"}
set_c = set_a.intersection(set_b_)
print(set3)
Set difference (\) uses the difference()
method.
set_a = {"a", "b", "c"}
set_b = {"b", "c", "d"}
set_c = set_a.difference(set_b_)
print(set3)
Questions
1. Check your understanding
Calculate the following, e,g,
Expression | Answer | |
---|---|---|
0. | { } |
1. Union
Expression | Answer | ||
---|---|---|---|
1. | { } | ||
2. | { } | ||
3. | { } | ||
4. | { } |
2. Intersection
Expression | Answer | ||
---|---|---|---|
1. | { } | ||
2. | { } | ||
3. | { } | ||
4. | { } |
3. Difference
Expression | Answer | ||
---|---|---|---|
1. | { } | ||
2. | { } | ||
3. | { } | ||
4. | { } |
Summary
In this section we have learned about the operations you can perform on sets. Now you can move on to the set theory challenges.
- Previous
- Next