1
00:00:05,280 --> 00:00:07,680
Back in the Introduction to Python sets video, 

2
00:00:07,680 --> 00:00:12,400
we had two sets; a set of farm 
animals, and a set of wild animals. 

3
00:00:12,400 --> 00:00:16,721
We'll use these sets to see how we 
can create the union of two sets 

4
00:00:18,720 --> 00:00:23,385
The union of two (or more), sets is 
the set of elements from all the sets. 

5
00:00:23,440 --> 00:00:29,278
As the diagram shows, the union of farm_animals 
and wild_animals contains all the animals. 

6
00:00:29,520 --> 00:00:32,948
Let's see how to create a union, in Python. 

7
00:00:34,080 --> 00:00:41,030
I'll create a new Python file called 
set_union.py, and define our two sets: 

8
00:01:27,680 --> 00:01:33,545
Okay. If we want a set containing all the 
animals, we can create the union of the two sets. 

9
00:01:33,600 --> 00:01:38,000
Sets have a union method that does just 
that. Before we look at the code, 

10
00:01:38,000 --> 00:01:42,315
let's have a quick look at the documentation.
That's something I suggest you do, 

11
00:01:42,315 --> 00:01:48,053
before using any Python feature for the first 
time. I'll go to the documentation for sets. 

12
00:01:50,160 --> 00:01:54,880
The URL will be in the resources for this video.
The first part documents the methods that are 

13
00:01:54,880 --> 00:02:01,120
available for both set types — set and frozenset.
frozenset is an immutable version of set. 

14
00:02:01,120 --> 00:02:05,440
Other than being immutable, it behaves 
exactly the same as the set type. 

15
00:02:05,440 --> 00:02:09,812
I won't go through all these methods. 
You've seen the len function a few times, 

16
00:02:09,812 --> 00:02:13,760
and it should be obvious. And we've 
already covered set membership. 

17
00:02:13,760 --> 00:02:17,120
I'll be talking about subsets 
and supersets shortly. 

18
00:02:17,120 --> 00:02:20,240
At the moment, we're interested 
in the penultimate four methods, 

19
00:02:20,240 --> 00:02:24,927
starting with union. I'll scroll 
them up, so we can see them. 

20
00:02:27,280 --> 00:02:32,009
We'll look at intersection, difference and 
symmetric difference in the next few videos. 

21
00:02:32,080 --> 00:02:35,357
In this video, we'll see some uses for set unions. 

22
00:02:35,440 --> 00:02:40,320
Before I move on, notice that we can use either 
an operator – the pipe or vertical bar – or the 

23
00:02:40,320 --> 00:02:46,069
union method. The same for the other operations 
– you can use either a method or an operator. 

24
00:02:46,069 --> 00:02:50,400
We'll see examples using both, as 
we examine these set operations. 

25
00:02:50,400 --> 00:02:55,840
Alright, back to our code, and we've got two sets.
If we wanted to do something with all the animals, 

26
00:02:55,840 --> 00:03:00,000
we can combine the sets, to create the union:

27
00:03:16,686 --> 00:03:20,161
Run the program,

28
00:03:20,161 --> 00:03:25,049
and we get a set containing 9 animals. 
Those are the animals that appear in both sets –

29
00:03:25,049 --> 00:03:28,988
as I showed in the last slide.
When forming the union of sets, 

30
00:03:28,988 --> 00:03:32,000
it doesn't matter which way round 
you do it. Instead of calling 

31
00:03:32,000 --> 00:03:37,565
the union method of farm_animals, we can call 
it on wild_animals and get the same result: 

32
00:03:55,680 --> 00:03:59,840
Run the program again.
The animals might not appear in 

33
00:03:59,840 --> 00:04:05,484
the same order – and I don't have to mention why 
again – but they both contain the same animals. 

34
00:04:06,800 --> 00:04:10,421
The union of two (or more) sets is commutative. 

35
00:04:10,421 --> 00:04:13,899
That's a posh way of saying that it 
doesn't matter which way round you do it. 

36
00:04:13,899 --> 00:04:17,685
It's the same as addition and 
multiplication, in arithmetic. 

37
00:04:17,839 --> 00:04:23,783
a + b is the same as b + a.
2 * 4 is the same as 4 * 2. 

38
00:04:25,120 --> 00:04:29,303
We can also use the union operator, the pipe:

39
00:04:43,891 --> 00:04:47,816
Once again, when I run the program,

40
00:04:48,000 --> 00:04:51,520
we get the same animals in the set.
So which should you use, 

41
00:04:51,520 --> 00:04:55,280
the union method or the union operator?
Most of the time, 

42
00:04:55,280 --> 00:04:59,680
it doesn't matter. A mathematician might 
prefer the union operator, because that's 

43
00:04:59,680 --> 00:05:03,360
how they'd normally form set unions.
The methods have the advantage that 

44
00:05:03,360 --> 00:05:08,637
you don't have to remember what the various set 
operator symbols mean – the code is more readable. 

45
00:05:08,637 --> 00:05:12,640
There is one important difference between the 
methods and the operators though. I'll talk 

46
00:05:12,640 --> 00:05:17,917
more about that soon, when we've got some data 
that I can use, to demonstrate the difference. 

47
00:05:18,000 --> 00:05:22,497
We'll see an advantage that the operators 
have, and an advantage of using the methods. 

48
00:05:22,497 --> 00:05:26,400
Alright, that's how you can form 
the union of sets, in Python. 

49
00:05:26,400 --> 00:05:32,640
In the next video, we'll see a more 
practical application of a set union.

