1
00:00:05,280 --> 00:00:10,320
We've seen some of the basic theory of sets. 
Now let's see how that translates to Python code. 

2
00:00:11,040 --> 00:00:14,720
I'll continue with the DictAndSet project 
that we've been using throughout this section, 

3
00:00:14,720 --> 00:00:17,760
and I've already created a new 
Python file called set_intro.py. 

4
00:00:19,120 --> 00:00:21,760
Pause the video, while you 
create your new Python file. 

5
00:00:24,080 --> 00:00:29,040
There are several ways to create a set in Python – 
just like there are several ways to create lists, 

6
00:00:29,040 --> 00:00:32,720
dictionaries and tuples. I'll 
start by typing a set literal: 

7
00:00:58,160 --> 00:01:01,760
 A Python set is enclosed in curly 
braces – just like a dictionary. 

8
00:01:02,480 --> 00:01:07,040
Python can tell this is a set, rather than a 
dictionary, because we don't have key/value 

9
00:01:07,040 --> 00:01:12,400
pairs. For a dictionary, each item would 
be a key and a value, separated by a colon. 

10
00:01:13,200 --> 00:01:16,640
Here, we've got the 5 items that 
make up our farm_animals set. 

11
00:01:17,360 --> 00:01:21,920
We've used curly braces, on line 1.
If we'd used square brackets, then 

12
00:01:21,920 --> 00:01:27,120
we'd have a list. And if we'd used parentheses 
– or no brackets at all – we'd have a tuple. 

13
00:01:27,920 --> 00:01:32,720
Pay careful attention to the kind of brackets, 
when you're typing in your code. If you use 

14
00:01:32,720 --> 00:01:36,320
the wrong kind of bracket, you won't be 
creating the type of object that you expect. 

15
00:01:37,120 --> 00:01:41,520
I use a large font when recording these videos, 
but it can still be hard to see the difference 

16
00:01:41,520 --> 00:01:46,080
between parentheses and curly braces – especially 
if you're watching these videos on a mobile phone. 

17
00:01:46,960 --> 00:01:49,120
Ok, let's run the program to see what we get. 

18
00:01:51,360 --> 00:01:54,720
Our set prints out with curly 
braces, to indicate that it's a set. 

19
00:01:55,520 --> 00:02:00,880
We've got 5 items in the set, which is what 
you'd expect. What you might not have expected, 

20
00:02:00,880 --> 00:02:04,800
is that the items aren't necessarily in the 
same order that I typed them – on line 1. 

21
00:02:05,600 --> 00:02:09,440
Run the program a few more times, and make 
a note of the order that the animals appear. 

22
00:02:11,840 --> 00:02:14,720
They probably won't appear in the same 
order as you can see on my screen. 

23
00:02:15,440 --> 00:02:20,400
The order changes each time you run the program.
I'm labouring the point, because it's important 

24
00:02:20,400 --> 00:02:25,440
to understand that sets are unordered.
I had to type them in some order, on line 1, 

25
00:02:25,440 --> 00:02:28,240
but that doesn't truly represent 
how the items are stored in the set. 

26
00:02:30,480 --> 00:02:33,840
It would be more accurate to represent 
the set of farm_animals like this. 

27
00:02:34,720 --> 00:02:37,360
That's just a collection – or group – of things. 

28
00:02:38,240 --> 00:02:42,240
That's more obviously unordered.
If it was ordered, what order would you use? 

29
00:02:44,560 --> 00:02:48,960
Would you go from left to right, and 
get cow, sheep, goat, hen, horse? 

30
00:02:49,760 --> 00:02:54,480
Or would you go from top to bottom, 
and get sheep, hen, cow, horse, goat? 

31
00:02:56,880 --> 00:03:00,960
Ok, back to our code.
Looking at line 1, the items 

32
00:03:00,960 --> 00:03:06,320
inside the curly braces are the items in our set.
When we create a set using a set literal – by 

33
00:03:06,320 --> 00:03:10,000
typing in the items in other words – we 
obviously have to type them in some order. 

34
00:03:10,720 --> 00:03:14,560
We can't put images into our Python code .
But the set isn't ordered. 

35
00:03:15,280 --> 00:03:19,040
Another way to demonstrate that, is to see 
what happens when we iterate over our set. 

36
00:03:19,840 --> 00:03:23,680
Sets aren't a sequence type – sequence 
types are ordered, so a set can't be 

37
00:03:23,680 --> 00:03:27,680
one – but we can still iterate over 
a set. I'll add the code to do that:

38
00:03:38,000 --> 00:03:39,360
Run the program a few times. 

39
00:03:44,560 --> 00:03:48,560
Iterating over the set is obviously working, 
but we don't get the same order each time. 

40
00:03:49,360 --> 00:03:53,120
Because you can't rely on the order, you 
probably won't iterate over sets very often. 

41
00:03:54,080 --> 00:03:58,560
Alright, you now know that sets are unordered. 
In fact, you're probably wishing I'd shut 

42
00:03:58,560 --> 00:04:02,240
up about sets being unordered .
I'll move on – with a question. 

43
00:04:03,120 --> 00:04:05,600
Have you thought about the 
implications of sets being unordered? 

44
00:04:06,320 --> 00:04:09,920
What might we not be able to do with a set?
And how does all this impact 

45
00:04:09,920 --> 00:04:13,840
on checking if two sets are equal?
I'll answer those questions in the next lecture. 

46
00:04:14,640 --> 00:04:17,760
I'll finish this video by pointing out 
something that you may have already worked out. 

47
00:04:20,079 --> 00:04:22,400
Because sets use hashes to store the items, 

48
00:04:22,400 --> 00:04:24,720
anything that you want to put 
into a set must be hashable. 

49
00:04:25,520 --> 00:04:28,400
That's exactly the same requirement 
that we had for dictionary keys. 

50
00:04:29,280 --> 00:04:34,400
If you try to put a list into a set, for example, 
you'll get an error – the same "unhashable 

51
00:04:34,400 --> 00:04:38,880
type" error that we talked about, in The 
dict documentation video, earlier in this section. 

52
00:04:39,760 --> 00:04:42,240
Although that's obvious, you 
may not have thought about it. 

53
00:04:44,640 --> 00:04:48,400
See you in the next video.

