From the course: Programming Foundations: Data Structures

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Operations on sets in Python

Operations on sets in Python

- [Instructor] So far, we've kept our code pretty simple when dealing with sets. There are more complicated operations that exist, but they delve more into the field of mathematics. Let's consider these two sets. We can combine the elements of both sets using the union operation. This creates a new set containing all the elements from both sets. The original sets remain unmodified, and it ensures that no duplicates are included. Let's run it. In the output, we see the unique elements from set A and set B. We also have the intersection operation. This allows us to see what entries are in both sets. The intersection will only contain 30, 40, and 50 because these are the only items that are in both sets. Another operation is difference. Difference allows us to subtract or remove the contents of one set from another. Here we subtract the contents of set B from the contents of set A. Set B contains 30, 40, and 50, which are also in set A, so the difference of set A and set B would be 10…

Contents