Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Set Math Confusion

So this isn't a question to help me complete a Code Challenge, it's more of a question to help me understand the code. I'm almost done with the collections course with Python and I've been hardcore confused by set math. I understand what the data type is, and I can visualize them fine. But it's when you include their math operators, like .intersection and .union is when I get really confused. Does anyone have some words of advice to help push me in the right direction?

1 Answer

I'm not super great at set math either, but it is just like the union and intersection of inequalities in math (if you've learned that already―if not, that's fine too). So let's say that you have two sets: one of some odd numbers {1, 3, 5, 7, 9, 11, 13}, and one of some prime numbers {2, 3, 5, 7, 11, 13, 17}. The intersection function returns a set of all the numbers that can be found in both sets, so in this case it would be {3, 5, 7, 11, 13}. The union function returns a set that includes all the numbers from both sets, without repeats. In this case, it would be {1, 2, 3, 4, 7, 9, 11, 13, 17}. Notice how numbers that are in both sets (the intersection of both sets) don't appear twice. If you need more help, just let me know!