Okey-Dokey. Let's take a look at comparison operators.
So I'm just going to create a new Jupyter Notebook here.
Now, when it comes to a comparison operator, these are the
little if-else statements that tell program what to do.
So if you ask me, "Kalob, what is your favorite color?"
And I said my favorite color was blue.
You could write a program that says, "if Kalob's favorite color
is blue, say, "Your favorite color is blue".
Now, that is a very simple, overly simplistic example.
But that is really how computers work.
That's even how a lot of advanced mathematics really works
when it comes to programming.
So machine learning, data science, artificial intelligence,
it's a lot of if-else statements.
So what that means is you can assign a variable such as
'age', and let's say my age is 30. It is.
And I can say 'if age >= 21', 'print(
"I can consume alcohols in the USA".
[no audio]
Boom, just like that.
And now that is really just the base behind a lot of programming.
So now I have this variable, and I can do something with it.
Now, what we're looking at here is a comparison operator.
So this says, 'if', that's a keyword.
So you know, probably don't use that as a variable
name, says, if the 'age' variable is greater than or equal to
21. So essentially, the computer is looking at this, and
it says if 30 is greater than or equal to 21, do a thing,
just do something.
Now, the reason that this was executed was not necessarily
because 30 is greater than 21.
It's actually because 30 is greater than or equal to 21 results
in a True statement.
So the Python interpreter sees this, if something is True,
do something.
If it is False, don't do something.
So let's undo that, and I will show you an example.
So 'if True: print("This was true")'.
Now this is going to run.
"This was true".
Now, if I said the opposite, 'if False: print("This was false")'.
This is not going to show up because this condition in here,
this 'if' condition is always looking for something to be True.
And so when I execute this, nothing happens.
Nothing was printed there because it was False.
Now we learned about all sorts of data types.
And the reason we learned about data types is to help us
with comparison operators.
Comparison operators allow you to compare strings against
strings, tuples against tuples, things like that.
So let's really dive into a few different things.
Now, before we do that, I'm going to show you one cool little
thing. In our Python interpreter,
so in here, or inside your Python shell, or your command line,
you don't have to write an 'if' statement to determine if something
is True or not. You can simply say 'True is True'.
Yep. Sure enough, it is.
You could say 30 > 29.
Yep, it is. You could say 40 > 50.
Nope, it's False.
And so these aren't actually really doing anything.
These aren't doing any extra commands.
These are just results.
Is 40 > 50?
No, it is not.
And so Python says, "Okay, it's not.
So declare it False".
So let's take a look at some comparison operators.
When we're comparing numbers, we can use greater than, '>'; greater
than is equal to, '>='; less than, '<'; less than is equal to, '<='; or is equal to, '=='.
So we could say a 'num = 10'.
And then we could say 'num == 10'.
This will be True.
'num > 10'.
It's False because number is 10.
It's not greater than, it's not less than, it is exactly.
But 'num < 10'.
No, it's not.
But what about if 'num <= 10'?
It's not less than 10, but it is in fact equal to 10, so
this will result as True.
Now, comparing numbers is pretty easy, but we can also compare
strings. So we could say 'string' == 'string'.
That's True.
But 'string' == 'string2', is False.
They are technically different.
So while this is the same as this, because this value in
here is different from this value, it's False.
There's a way to compare that as well.
And this gets a little bit more on the inverse side of things.
So we can say 'string' != 'string2'.
Now this is going to result in True.
And the reason for that is because we're saying, "Does this
not equal this?"
And so it's kind of like a double negative.
Does it not-not equal something?
And this is going to result in True, as we can see here,
because 'string' is in fact not like 'string2'.
If I told you that coffee and tea were different, and you
said 'if 'coffee' != 'tea'', that would be True, because 'coffee'
is not 'tea'.
So when we dive into comparison operators, all we're going
to do is really add an 'if' statement in front of this,
'if something is True', that is what Python is looking for,
'if something is True'. Most programming languages are actually
looking for that. They're just looking for something to be True.
So let's go ahead and ask for a 'name'.
'name = input(
"What is your name?")'.
And my name is going to be
"Kalob".
I can now display 'name'. Shows up as "Kalob".
So now I can say 'if name == 'Kalob'', spelt the exact
same way, because if this is lowercase, it's not the same.
For instance, 'kalob' does not equal 'Kalob'.
They're not the same thing in Python.
That capital 'K' versus that lowercase 'k' makes the world of
difference here. So it is very important.
So let's go ahead and use a capital 'K' and say, 'print("Kalob
Taulien is your Python teacher")'.
And boom, that shows up. Now, a nice little trick here is
when you're accepting user input, it's really hard to determine
if the user is going to have all capitals, or all lowercase,
or something spelt weird like this where it's capital 'KAL', lowercase 'ob'
So what we can do, is we can say 'if name.lower', which
is going to turn it all into lowercase, '== 'kalob': print(
"Name is still Kalob but we detected it by a lower case name")'.
[no audio]
Voila! just like that.
And on the inverse, because 'name' is "Kalob", we could say,
'if name != 'Jacob': print("This is anyone except Jacob")'.
And there it is.
And that 'name' could be literally anything except for 'Jacob'
with a capital J.
Now, those are just some basic comparison operators.
That's what we're going to stick with for this particular
lesson. What I would like you to do is get a little bit of
hands on experience with 'if' statements right now.
So just create a variable, could be 'name', could be 'course',
could be 'age',
and then if you're comparing a string such as where I had
'Kalob' ==, or != 'Jacob', make sure you're
comparing it to another string.
And if you're comparing a number, which was somewhere over
here, then you're going to want to make sure that your data
type is compared to a number.
Now, one thing to keep in mind is numbers don't have quotation
marks, so it's not "21",
it's just 21.
Floats don't have quotations either.
So it's not, "3.14",
it's just 3.14.
Python is smart enough to recognize when that is the exact
same thing, but that's not the point.
You want to always make sure you're comparing the same data
type to the same data type.
So here we're comparing a string to a string, here
we're comparing a number to a number, or an integer to an
integer. And remember, your 'if' statement is always looking
for something to be True.
So go ahead, give this a shot, and try something like, if your
name is "Matthew", then print, "Welcome, Matthew".
Or if your 'age > 18', then, "You are not a child".
Play around with the different operator types '>',
'<', '>=', '<=', '==',
with the two equal signs, or '!='.
And when you're done playing around with that, and yeah,
just feel free to experiment and break things.
That's totally okay. When you're done with that
let's head on over to that next lesson where we pick up
on a few comparison operator shortcuts.