## Equality Comparisons - Intro to Computer Science

• 0:00 - 0:03
Everything we've done so far has been pretty limited,
• 0:03 - 0:06
that we had to do the same thing on all data.
• 0:06 - 0:09
We couldn't do anything that really depended on what the data was.
• 0:09 - 0:12
What we're going to do next is figure out a way to make code behave differently
• 0:12 - 0:14
based on decisions.
• 0:14 - 0:17
The first thing we're going to do is figure out some ways to make comparisons,
• 0:17 - 0:20
so we have a way to test and decide what we should do.
• 0:20 - 0:24
Python provides lots of different operators for doing comparisons.
• 0:24 - 0:26
There are things similar to what we've used in math.
• 0:26 - 0:29
We have a less than sign that compares 2 numbers.
• 0:29 - 0:30
We have the greater than.
• 0:30 - 0:33
We have a less than or equal to. Things like this...
• 0:33 - 0:39
All of these operate on numbers, so we can have a number followed by
• 0:39 - 0:43
a comparison operator, followed by another number.
• 0:43 - 0:47
This is very similar to the grammar we saw earlier for arithmetic expressions,
• 0:47 - 0:50
but now instead of having a plus or times here,
• 0:50 - 0:52
we can have something that does a comparison.
• 0:52 - 0:54
The output of a comparison though is not a number.
• 0:54 - 0:59
It's a Boolean value, and a Boolean value is 1 of 2 things.
• 0:59 - 1:04
It's either the value True or the value False.
• 1:04 - 1:07
Let's see some examples in the Python interpreter.
• 1:07 - 1:11
First, we'll use the less than to compare 2 and 3.
• 1:11 - 1:14
So 2 is less than 3, so we expect the result to be true.
• 1:14 - 1:17
When we run this, we see that the result is true.
• 1:17 - 1:21
If we compare a number greater than 3, let's say 21 < 3.
• 1:21 - 1:23
The result will be false.
• 1:23 - 1:26
We can have any expression we want with a comparison,
• 1:26 - 1:30
so we can do 7 * 3 < 21.
• 1:30 - 1:34
When we run that, we also get false because 7 x 3 = 21,
• 1:34 - 1:36
which is not less than 21.
• 1:36 - 1:40
Another comparison operator we can use is not equal to.
• 1:40 - 1:44
So != means not equal to.
• 1:44 - 1:51
So 7 * 3 != 21 is false because 7 x 3 is equal to 21.
• 1:51 - 1:56
If we want to do equality comparison, we don't use the equal sign,
• 1:56 - 1:57
we use 2 equal signs.
• 1:57 - 1:59
We call that the double equal.
• 1:59 - 2:05
So now we have 7 * 3 == 21, and the result there is true.
• 2:05 - 2:08
Now we're going to have a quiz to see if you can figure out why we need to use
• 2:08 - 2:11
the == here instead of just the single =.
• 2:15 - 2:19
The question is, why is the equality comparison done using ==,
• 2:19 - 2:24
having 2 equals, instead of just a single = sign?
• 2:24 - 2:29
The possible answers: Because = means approximately equal,
• 2:29 - 2:32
and we want to do exact equality comparisons.
• 2:32 - 2:36
Because we needed to use 2 characters for the not equal comparison,
• 2:36 - 2:38
and we wanted the equal to be the same length.
• 2:38 - 2:42
Because Guido, the designer of Python, really likes = signs.
• 2:42 - 2:45
Because the single = sign means assignment,
• 2:45 - 2:49
or it doesn't really matter. We can use either == or =.
Title:
Equality Comparisons - Intro to Computer Science
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
CS101 - Intro to Computer Science
Duration:
02:50
 Udacity Robot edited English subtitles for 05-26 Equality Comparisons Udacity Robot edited English subtitles for 05-26 Equality Comparisons Udacity Robot edited English subtitles for 05-26 Equality Comparisons Udacity Robot edited English subtitles for 05-26 Equality Comparisons handfulofvoid edited English subtitles for 05-26 Equality Comparisons Edderso edited English subtitles for 05-26 Equality Comparisons Edderso edited English subtitles for 05-26 Equality Comparisons michaeljleclech edited English subtitles for 05-26 Equality Comparisons

# English subtitles

## Revisions Compare revisions

• API
Udacity Robot
• API
Udacity Robot
• API
Udacity Robot
• API
Udacity Robot
• Edited (legacy editor)
handfulofvoid
• Edderso
• Edderso
• michaeljleclech
• Amara Bot