1
00:00:00,000 --> 00:00:03,990
Python also has a complete set of comparison operators.

2
00:00:04,000 --> 00:00:10,990
For example, if I say 5 < 6, I'll get True.

3
00:00:11,000 --> 00:00:14,990
If I say 6 < 5, I'll get False.

4
00:00:15,000 --> 00:00:26,990
Likewise, if I say 5 <= 6, I'll get True, or 5 <= 5, I'll also get True.

5
00:00:27,000 --> 00:00:35,990
It also has greater than or equal to, 6 >= 5 or 6 >= 6.

6
00:00:36,000 --> 00:00:39,990
6 is not of course >= 7.

7
00:00:40,000 --> 00:00:46,990
And equality is tested with the double equal signs. So 5 == 5 is True.

8
00:00:47,000 --> 00:00:50,990
And 5 == 6 is not True.

9
00:00:51,000 --> 00:00:58,990
Then it has a not equal operator. So 6 != 7 is True.

10
00:00:59,000 --> 00:01:02,990
And 6 != 6 is False.

11
00:01:03,000 --> 00:01:08,990
It also has is and is not, and these are for testing id.

12
00:01:09,000 --> 00:01:15,990
So for example, if I have two variables x and y, and we'll assign them values

13
00:01:16,000 --> 00:01:18,990
of 5 and 6.

14
00:01:19,000 --> 00:01:25,990
So the id(x) is that, and the id(y) is that.

15
00:01:26,000 --> 00:01:27,990
So they're not the same.

16
00:01:28,000 --> 00:01:30,990
So if I say x is y, I get False.

17
00:01:31,000 --> 00:01:36,990
If I say x is not y, I get True.

18
00:01:37,000 --> 00:01:43,990
Likewise, if I assign 5 to y, now their ids will be the same.

19
00:01:44,000 --> 00:01:48,990
Id(y) is the same as id(x).

20
00:01:49,000 --> 00:01:55,990
Now x is y. For immutables like integers, the ids are always going to be the

21
00:01:56,000 --> 00:01:56,990
same if the value is the same.

22
00:01:57,000 --> 00:02:02,990
So testing equality and testing id is almost the same thing, but for immutables,

23
00:02:03,000 --> 00:02:10,990
like for example, if I have a list, I say x, y = a list with 5 and another

24
00:02:11,000 --> 00:02:16,990
list with 5,

25
00:02:17,000 --> 00:02:24,990
now they have different ids, id(x) is that and id(y),

26
00:02:25,000 --> 00:02:28,990
you can see that they have two different ids now, even know their values are the same.

27
00:02:29,000 --> 00:02:31,990
So if I test x = y, it's True,

28
00:02:32,000 --> 00:02:36,990
but if I test x is y, it is now not True, because they have different ids.

29
00:02:37,000 --> 00:02:39,990
They're actually different objects.

30
00:02:40,000 --> 00:02:42,990
So those are the comparison operators in Python.

31
00:02:43,000 --> 00:02:47,990
You can compare values, and you can also compare ids with is and is not.

32
00:02:48,000 --> 00:02:50,990
So when would I want to compare ids?

33
00:02:51,000 --> 00:02:54,990
Because Python is fundamental an object-oriented language, everything in

34
00:02:55,000 --> 00:02:57,990
Python is an object.

35
00:02:58,000 --> 00:03:01,990
And because Python, of course, supports polymorphism there's going to be times

36
00:03:02,000 --> 00:03:04,990
when you're going to be using objects.

37
00:03:05,000 --> 00:03:09,990
You may need to know is this exactly the same object as this other object over there?

38
00:03:10,000 --> 00:03:14,990
Which object is it that I'm dealing with? And you can't know which is by testing value.

39
00:03:15,000 --> 00:03:19,990
So this is a time when you might want to use is and is not to test for the

40
00:03:20,000 --> 00:03:23,990
identity of the object rather than just testing the value of the object.

41
00:03:24,000 --> 00:03:26,990
So those are Python's comparison operators.

42
00:03:27,000 --> 00:03:37,000
Of course, you can compare value, as well as comparing identity.

