From the course: Programming Foundations: Data Structures
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Operations on sets in Python - Python Tutorial
From the course: Programming Foundations: Data Structures
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 contents of both sets using the union operation. It will create a new combined set. Let's try it. We can access either set first and then pass the other one in. This leaves the original sets unmodified and creates a new set with the contents of both. Remember, sets do not have duplicates, so it'll only contain one copy of each item that lives in set, A, B, or both. Let's print it out. We also have the intersection operation. This allows us to see what entries are in both sets. Here we get the intersection of set A and B with the intersection function. This will only contain 30, 40, and 50 because those are the only items in both sets. Another operation is difference. Difference allows us…