Locked lesson.
About this lesson
How to compare two things.
Exercise files
Download this lesson’s related exercise files.
Comparison Operators.docx58.9 KB Comparison Operators - Solution.docx
58.9 KB
Quick reference
Comparison Operators
Comparison operators allow us to compare things.
When to use
Use these whenever you need to compare things.
Instructions
Here are the main comparison operators:
== is equal to
!= is not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Hints & tips
- Comparison operators allow us to compare two or more things
- Comparison operatros are often used in conditional statements
- 00:04 In this video, I want to talk about comparison operators.
- 00:07 And comparing different things is super important whenever you're doing any sort
- 00:11 of computer programming, you're always going to want to compare things.
- 00:14 And in the next video, we're going to look in conditional statements.
- 00:17 They allow us to take our comparisons and do different things with them.
- 00:21 But before we do that, we have to learn about the actual comparison operators,
- 00:24 the little symbols that we use to compare two things.
- 00:26 So these are super simple, and the first one is double equal to sign.
- 00:30 So remember, we've looked at this many times.
- 00:33 Let's go first_name = "John".
- 00:37 So this is a single equal to sign and it's an assignment operator.
- 00:41 It is assigning the value of John and the first name.
- 00:44 This is a double equal to sign and it's not assigning, it's asking a question.
- 00:49 It's saying, is this equal?
- 00:50 So, to use this, we might say let's use a number.
- 00:53 Let's go 10 == 10, right?
- 00:56 This is a true statement.
- 00:58 So let's just get rid of this.
- 00:59 So in fact, if we print this out, what we're asking here.
- 01:04 We're asking Python, Python is asking is 10 == 10?
- 01:08 And that's using those two equal to sign.
- 01:10 So if we save this and run it, we're going to get back true.
- 01:13 So true, 10 does equal 10 and true and false or what's called Booleans.
- 01:18 And just a fancy word for true or false and a Boolean is actually a data type.
- 01:21 We didn't talk about it because it's just true or false, that's all there is to it.
- 01:24 But we can change this around, we can say 9, is 9 == 10.
- 01:29 If we save this and run it, we're going to get false because 9 and
- 01:32 10 are not equal obviously.
- 01:33 So that's the first one, first comparison operator.
- 01:36 And let's just make a quick list here, equal to.
- 01:39 The next one is not equal to, and we'll just use this exclamation point equal to.
- 01:45 And in most programming languages, exclamation marks mean not.
- 01:48 So this is saying, is this not equal to?
- 01:51 Is 9 != to 10?
- 01:53 And it is true that 9 and 10 are not equal.
- 01:56 So we should get true, or we could change this to 10.
- 02:00 In which case, it will be false because it's asking, is 10 not equal to 10?
- 02:05 Well, no, 10 and 10 are equal.
- 02:06 So this is false.
- 02:07 It's false that they are not equal, right?
- 02:10 So, those are the two big ones, right?
- 02:12 The rest you're going to be familiar from basic math as a child, and
- 02:17 I'm just going to write them down here.
- 02:18 Greater than equal to and less than or equal to.
- 02:22 So, greater than, is 10 greater than 10?
- 02:25 That can be false because no, 10 is not greater than 10.
- 02:28 Is 11 greater than 10?
- 02:31 True, right.
- 02:31 So same thing with the less than sign.
- 02:34 Just asking question, is 11 less than 10?
- 02:37 Nope. Is 1 less than 10?
- 02:41 True, right?
- 02:42 So finally, there's these greater than or equal to.
- 02:45 Is 1 greater than or equal to 10?
- 02:48 Absolutely not, is 10 greater than or equal to 10?
- 02:52 And in this case, one or the other have to be true or they can both be true.
- 02:56 So 10 is equal to 10.
- 02:58 So this should spit out true and we get true.
- 03:02 We change this to 11.
- 03:03 11 is greater than, it's not equal to.
- 03:05 11 is not an equal to 10 but it is greater than.
- 03:08 So, this will give us true.
- 03:10 Same thing for less than, less than or equal to.
- 03:12 Is 11 less than or equal to 10?
- 03:15 Nope, so we get false.
- 03:17 Is 10 less than or equal to 10?
- 03:19 Well it is equal to, so we get true.
- 03:21 Finally, is 9 less than or equal to 10?
- 03:25 Yes, it is less than so we get true.
- 03:27 So, those are our comparison operators.
- 03:29 They're very, very simple and we didn't do a whole lot in this video,
- 03:33 we just saw a bunch of trues and falses.
- 03:35 In the next video, we're going to look at conditional statements that will allow us
- 03:39 to use these things and then take certain action.
- 03:42 If it's true, do this.
- 03:44 Otherwise, do that.
- 03:45 Those are called if else statements.
- 03:47 Those are comparison operators.
- 03:48 In the next video, like I said, we'll look at conditional statements.
Lesson notes are only available for subscribers.