This course will be retired on January 6, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sets let us easily compare and contrast them.
The docs about set
s are pretty good and I suggest you check them out. Here's yet another brief explanation of the common set
operations:


or.union(*others)
 all of the items from all of the sets 
&
or.intersection(*others)
 all of the common items between all of the sets 

or.difference(*others)
 all of the items in the first set that are not in the other sets 
^
or.symmetric_difference(other)
 all of the items that are not shared by the two sets
(also: notice how those are using *others
? That's a tuple of other sets. See, I told you that pattern was everywhere!)

0:00
Sets have several different operations that they can do.

0:03
I'll talk briefly about each of them in this video,

0:05
and we'll use them together in work spaces.

0:07
But you should check the documentation which I've linked in the teacher's notes,

0:10
and practice on your own.

0:11
They can be really tricky until you get used to them.

0:13
Let's pretend we have two sets.

0:15
The first set will be the first ten positive whole numbers.

0:18
The second will be the first ten prime numbers.

0:20
We'll put them in two circles.

0:22
Hey, that kind of looks like a Venn diagram.

0:24
Well it should because Venn diagrams and sets are pretty tightly linked.

0:28
The first operation to talk about is union.

0:30
This is the combination of two or more sets.

0:33
In this case, we'd get each number in both sets.

0:35
But since sets are unique, we wouldn't get multiple ones, or fives, or anything.

0:39
Next is difference.

0:41
This operation finds everything that's in the first set, but not in the second.

0:45
There's also a symmetric difference which is everything that is unique to

0:48
either set.

0:49
So, no shared numbers.

0:51
Sorry primes below ten.

0:52
And lastly, we have the intersection.

0:54
An intersection of sets gives a new set of all items that are in both sets.

0:59
The intersection of these two sets gives us only the primes below ten.

1:03
In python, we can do all of these operations as both methods and operators.

1:07
Let's go look at them in work spaces.

1:10
So I'm actually gonna go ahead and recreate those two sets.

1:12
So set1 was a set of the first ten numbers.

1:17
The first 10 positive numbers.

1:18
And set2 was 1,2,3,5,7,11,13,17,19, and 23.

1:22
So let's get the union

1:27
of these two sets using

1:31
the union method.

1:36
So set1.union(set2), and we get a new set.

1:41
Now, if I had done set1 dot update set2,

1:44
it would have effectively done the same thing, right?

1:47
But it would have changed set one.

1:49
Calling union does not change set1 or set2.

1:53
See, they're still the same.

1:55
So that's cool.

1:56
Now, I can do the same thing with the union operand which is the pipe character.

2:00
So set1  set2, there we go, same output.

2:04
Now, to get the difference between the two sets, I can use the difference method.

2:08
set1.difference(set2), there is what's different between those two.

2:15
Or I can turn that around set2.difference(set1).

2:21
And there's the difference on those two, 19, 17, 11, 13, and 23.

2:27
The difference operator is a hyphen.

2:29
So set1 set2 or set2 set1.

2:34
For the symmetric difference, or

2:36
the things that are unique to each side, I use the carrot symbol.

2:40
It looks kinda like the top of the house or an arrow pointing up.

2:43
So set1 ^ set2, gets me the things that are unique to each side.

2:50
And I can, of course, do this with a method as well.

2:52
So set2.symmetric_difference(set1), and I misspell this one constantly.

3:01
There we go.

3:02
And last, but definitely not least, is the intersection of the sets.

3:07
So set1.intersection(set2), has those items in it.

3:14
And the operand for that is the ampersand.

3:17
So set1 & set2, gets the same numbers.

3:21
Those are all pretty handy.

3:24
For union and symmetric difference, the order of the sets isn't important

3:27
because valid items from both sets get put into the final set.

3:30
For the other operators though, order matters.

3:32
Before you jump in the core challenges in the next video,

3:35
take a little while to play with sets on your own, and look at the docs.

3:37
Be sure to look at how to determine if one set is a superset or subset of another, or

3:42
if two sets are completely disjointed.

3:44
That last one is often really useful to make sure the two sets have no overlap.
You need to sign up for Treehouse in order to download course files.
Sign up