1
00:00:05,200 --> 00:00:10,160
Before we can look at the intersections of sets, 
we're going to need some sets to work with. 

2
00:00:10,160 --> 00:00:13,840
Rather than typing them, this is a good 
opportunity to see the other advantage of using 

3
00:00:13,840 --> 00:00:17,920
the methods, rather than the operators.
We can provide any iterable, 

4
00:00:17,920 --> 00:00:21,440
as the argument to the methods.
I'll start by showing how we can 

5
00:00:21,440 --> 00:00:26,849
create sets from iterables. We have seen 
this before, but it's worth reviewing. 

6
00:00:26,849 --> 00:00:34,270
Create a new Python file. I'll 
call mine set_intersection.py 

7
00:00:38,160 --> 00:00:42,161
I'll use ranges, to create sets 
for the even and odd numbers: 

8
00:01:16,560 --> 00:01:20,376
The range function returns an 
iterable. For the evens set, 

9
00:01:20,376 --> 00:01:24,849
we get it to generate the even numbers 
from 0, up to but not including 50. 

10
00:01:24,849 --> 00:01:30,400
We do the same for the odds set, but start at 
1. Because the step is 2, we get the even and 

11
00:01:30,400 --> 00:01:33,840
odd numbers, respectively.
Run the program, 

12
00:01:33,840 --> 00:01:36,865
and check that the sets do 
contain what we intended. 

13
00:01:39,200 --> 00:01:44,881
Ok, these 2 sets will be useful soon, but they're 
not much good for forming an intersection. 

14
00:01:44,881 --> 00:01:48,529
The sets are disjoint, and don't 
have any elements in common. 

15
00:01:48,529 --> 00:01:54,489
We need some more sets. Once again, we're going 
to get Python code to generate them for us. 

16
00:01:54,489 --> 00:01:58,400
Download primes_and_squares.txt 
from the resources for this video, 

17
00:01:58,400 --> 00:02:04,942
then copy and paste the code into a new Python 
file. I'll call mine primes_and_squares.py 

18
00:02:12,480 --> 00:02:16,777
Don't try to understand this code – 
it uses some advanced techniques. 

19
00:02:16,777 --> 00:02:19,777
By the end of the course, it will all make sense. 

20
00:02:19,840 --> 00:02:23,840
For now, it's enough to know that 
the two functions return iterables. 

21
00:02:23,840 --> 00:02:27,760
Close the primes and squares file, 
and let's see what we can produce. 

22
00:02:27,760 --> 00:02:30,493
We start by importing the functions that we need: 

23
00:02:41,520 --> 00:02:46,880
Ok. We don't need to do this, but it'll be easier 
to understand the next bit if we check the values 

24
00:02:46,880 --> 00:02:49,034
produced by these functions:

25
00:03:20,146 --> 00:03:22,085
I'll run the program.

26
00:03:23,360 --> 00:03:27,840
The sets may not print out in order, but we 
get sets of the prime numbers less than 100, 

27
00:03:27,840 --> 00:03:32,080
and the perfect squares less than 100.
We'll use the set of primes soon, 

28
00:03:32,080 --> 00:03:35,769
which is why I included that function 
in the code you've just pasted. 

29
00:03:35,769 --> 00:03:40,485
If we wanted the odd perfect squares, we can 
form the intersection of odds and squares: 

30
00:03:50,080 --> 00:03:53,920
Run the program again.
The only odd, 

31
00:03:53,920 --> 00:04:02,000
perfect squares less than 50 are 1, 9, 25 and 49.
We could have used the operator, &, instead. 

32
00:04:02,000 --> 00:04:05,520
I'll get the intersection of evens and 
squares this time, to see how many even 

33
00:04:05,520 --> 00:04:07,546
perfect squares there are:

34
00:04:14,845 --> 00:04:16,841
I'll run the program

35
00:04:17,814 --> 00:04:25,680
This time, we get 4, 16 and 36. Those are 
the even, perfect squares, less than 50. 

36
00:04:25,680 --> 00:04:30,800
Alright, that's set intersection. It produces 
a new set, containing those elements that are 

37
00:04:30,800 --> 00:04:36,720
in both the sets. Review the earlier slides, 
if you need to remind yourself about that. 

38
00:04:36,720 --> 00:04:39,840
The intersection method is more readable 
than the operator, if you're not a 

39
00:04:39,840 --> 00:04:44,320
mathematician working with set theory.
It has another advantage – as do all 

40
00:04:44,320 --> 00:04:48,976
these methods. You can pass an 
iterable to it, instead of a set. 

41
00:04:48,976 --> 00:04:51,690
What that means is, we can write that as:

42
00:05:13,465 --> 00:05:18,069
Run the program, to confirm that it works, 
and we get the same result. 

43
00:05:21,680 --> 00:05:26,800
You can't do that with the operators. 
They only work when both objects are sets. 

44
00:05:26,800 --> 00:05:31,760
Next, we'll see how a set intersection might 
be useful for our clinical trials example. 

45
00:05:31,760 --> 00:05:35,680
I'll see you in the next video.

