1
00:00:05,200 --> 00:00:08,720
At the end of the previous lecture, I left 
you thinking about why it's important to 

2
00:00:08,720 --> 00:00:12,240
understand that sets are unordered.
What are the implications of that, 

3
00:00:12,240 --> 00:00:15,920
when we write our code?
There are two that immediately spring to mind. 

4
00:00:16,720 --> 00:00:22,240
The first is, you can't index into a set. If 
items in a set don't have a fixed position, 

5
00:00:22,240 --> 00:00:25,360
then it just doesn't make sense to try 
to get an item at a certain position. 

6
00:00:26,160 --> 00:00:27,680
Let's demonstrate that with some code: 

7
00:01:09,520 --> 00:01:15,600
I've created a list, on line 9. On line 10, 
we retrieve the item at index position 3. 

8
00:01:16,400 --> 00:01:21,120
"goat" is the fourth animal in the list, and when 
we run the program, we get "goat" printed out. 

9
00:01:25,520 --> 00:01:29,040
I'll run the program a few times, to 
make sure we get "goat" every time. 

10
00:01:36,880 --> 00:01:40,400
Ok, I'm in danger of getting your 
goat, if I keep running the program. 

11
00:01:41,120 --> 00:01:45,920
Let's see what happens if we try that with our 
set. I'll add the code to do that, on line 13: 

12
00:02:03,760 --> 00:02:08,320
I'm not going to type the print statement, 
because I've already got a warning from my IDE. 

13
00:02:08,320 --> 00:02:10,400
I'll hover over the warning, so we can see it: 

14
00:02:11,440 --> 00:02:16,560
The warning is a bit cryptic. Don't worry – 
the way it's phrased will make perfect sense, 

15
00:02:16,560 --> 00:02:21,600
after you've watched the OOP section.
I'll run the program. We'll get an error, 

16
00:02:21,600 --> 00:02:26,640
but the error message is more understandable.
TypeError: 'set' object is not subscriptable 

17
00:02:27,440 --> 00:02:30,320
not subscriptable is another way 
of saying that you can't index it. 

18
00:02:31,040 --> 00:02:35,040
You can't use an index with a set.
Which makes sense, now that you know that 

19
00:02:35,040 --> 00:02:40,000
sets are unordered. If you could ask for the 
item at position 3, which item would you get? 

20
00:02:40,800 --> 00:02:43,360
It might take many attempts 
before you eventually got "goat". 

21
00:02:44,160 --> 00:02:48,640
A program that you have to run many times, in the 
hope that you eventually get the right answer, 

22
00:02:48,640 --> 00:02:52,640
isn't much use to anyone.
So Python doesn't let us index into a set. 

23
00:02:53,360 --> 00:02:58,240
That also means we can't slice a set.
The first implication of sets being unordered, 

24
00:02:58,240 --> 00:03:02,480
is that you can't index or slice a set.
The second implication also won't be 

25
00:03:02,480 --> 00:03:06,640
surprising, once you've seen it.
I'll comment out those two lines, 

26
00:03:06,640 --> 00:03:09,680
to remove the error, and type 
the code to create a new set:

27
00:03:09,680 --> 00:03:32,880
Ok, more_animals contains the 
same items as farm_animals. 

28
00:03:34,160 --> 00:03:38,560
They're just in a different order.
If ordering isn't important with sets, then these 

29
00:03:38,560 --> 00:03:44,240
two sets should compare equal. Let's see if that's 
true. I'll add the code to test if they're equal: 

30
00:04:08,080 --> 00:04:11,680
Run the program, to prove that Python 
considers the two sets to be equal. 

31
00:04:12,800 --> 00:04:17,200
The sets are equal.
Python considers sets to be equal, if they 

32
00:04:17,200 --> 00:04:22,720
contain the same items. Ordering is unimportant 
– it's the items contained in the set that count. 

33
00:04:23,520 --> 00:04:28,400
That's different behaviour to lists and tuples. 
Lists and tuples will compare equal if they 

34
00:04:28,400 --> 00:04:32,880
contain the same items, in the same order.
You now know a couple of ways that sets 

35
00:04:32,880 --> 00:04:37,600
differ from lists or tuples. What you may 
be wondering is, why bother using a set? 

36
00:04:38,400 --> 00:04:43,680
Sets seem to be similar to lists and tuples, 
but more restricted. So why should you use them? 

37
00:04:44,400 --> 00:04:48,480
In the introduction to this section, I said 
that a set can be used to fix a bug in one 

38
00:04:48,480 --> 00:04:55,840
of our early programs. We'll have a look 
at that next. See you in the next video.

