Okay, let's take a look at some comparison operator shortcuts.
So in the last video, if you didn't watch it, you might actually
want to go back and watch it,
there was a point near the beginning of the video where I
said a comparison operator behind the scenes is always looking
for something to be True.
So if something is True, 'print("This is a true statement")'.
And sure enough, this prints because 'if True', well 'True'
is always True. So if it's True, print the statement.
That in itself is somewhat of a shortcut.
But with different data types, things can be cast as either
True or False.
So we sort of saw this back when we were looking at Booleans.
So let's do a string example.
A 'string' example is going to be "Python for Everybody".
So we can say 'if string: print("String has a value"),
And we can also say, 'if not string',
So if 'string' does not have a value, 'print("String does not
have a value")''.
So if we just look over this, 'string = "Python for
Everybody", it's going to execute this one.
And there's another 'if' statement.
If there is nothing in the string, it's going to say "String
does not have a value")'.
And instead of printing the two statements, it only prints the one.
Now let's go ahead and get rid of this.
Let's just use an empty string and rerun this cell.
Sure enough, that changes.
Says, "String does not have a value".
That's because this 'string' is empty.
Now, the reason for that is because if we cast our string
as a Boolean, we can actually see what that's going to be.
The 'string' is currently False.
But if I rerun this, 'string' is "Python for Everybody", rerun
that one, and let's rerun this one,
this will now be True,
and that's because an empty string is always considered False.
And anything that is inside of a string, literally anything
makes it True, at least in the eyes of a Boolean.
And now that comes back up to here where we're saying, if
something is True.
So if a string is empty and we cast it as a Boolean, that
comes back as False.
And so that's basically the exact same as saying 'if False',
well, that's not going to run because False comparison operators
never, ever get executed.
So let's run this one more time and just keep that data consistent.
Let's look at some sort of list.
So 'lst' is equal to an empty list, and we can do all this
one in one cell.
So we can now say 'if lst: print("There are items in the
list")'. And let's count the number of items in this list.
And we can also say, 'if not lst',
so if this list is False, 'print("There are no items in this
list")'. Let's go ahead and execute that.
Sure enough, it says, "There are no items in this list", because
an empty list is also False.
But we could put 'Python' in here, and we could also put 'JavaScript',
for example.
And let's rerun this cell.
And now it says, there are two items in the list and the number
2 shows up, because that's how many items are in here.
Now I'm going to copy this entire thing.
And let's change this from a list to a tuple.
And let's call that 'tup'.
I probably should have just wrote this out from scratch,
it would have been a little easier than doing it this way.
'len(tup)'.
"There are items in the tuple", and "There are no items in the
tuple". Yep, that was a little painful, I bet.
Okay, so if we run this, this tuple has two items in it. Based
on what we know about a list,
do you think this is going to say, "There are items in the
tuple", with a length of the tuple,
or do you think the 'print' statement is going to say, "There are
no items in this tuple."?
[no audio]
It is actually going to say that, "There are items in this
tuple". Sure enough, that's how it works.
And if we get rid of this, just an empty tuple, "There are
no items in the tuple".
Now the same thing works with dictionaries using curly braces,
as well as set, which will be evaluated the same way when
they're empty anyways.
So an empty dictionary will return False, and an empty set
will also return False as well.
Now, here's an interesting one.
What if we look at None?
So let's say 'total_items' of a thing is 'None'.
'if total_items: print("There are items")'. We can say, 'if not
total_items: print("There are no items")'.
Now, this is an interesting one, because 'None' technically
has nothing.
And so should 'None' by default evaluate to be True or to be
False? So let's run this and find out. It says, "There are no items".
By default, 'None' is always False in Python.
Now there's another operator that I'm going to talk about
now, and that is the 'is' operator, and that is not the same
as equal sign.
So a lot of people think 'if total == None' is the
exact same as saying 'if total is None', because when we read
this, we read it in our brains the same way.
If total is equal to None, I mean, I just said it, if total
is None, then what do we do? 'print',
let's do '== None'.
And in here, let's do 'print("is none")'.
And it wasn't 'total',
the reason that didn't work,
it was because it is 'total_items'.
Let's rerun that. There we go.
And they're both triggered.
Now, what's interesting about this is when you're comparing
a number or a string, always use the '==' sign,
and when you're looking for 'None', then use 'None'.
What 'is' is doing, is it's looking for an exact location in
memory. It's trying to make sure that something is literally
the exact same thing.
So if I said 'name1 = "kalob"', and 'name2'
also '= "kalob"', spelt the exact same way. Behind the scenes,
what Python is going to do is say, "Oh, this was set already
once, I'm going to point this variable to this piece of memory,
and I'm going to point this variable to the exact same piece of memory."
And so let's set that, and do 'name1 is name 2',
and we'll see that it's True.
Now this gets into the inner workings of Python, which I
don't want to talk too much about, because for some people,
that can be pretty boring.
But because these are identical, we can use 'is'.
So this has a piece of memory, and this has a piece of memory.
And in Python they're pointing to the exact same piece of
memory. So 'is' is going to work.
The reason why it works with None, and the reason why you'll
see it a lot in Python when comparing to None is because
None is always going to be the same piece of memory in Python as well.
So 'total_items', if that's None it's going to point to the exact
same value, the exact same piece of memory that Python has
already dedicated to just having nothing in there.
Now, if that was complicated, if that was too confusing,
for now, just forget about it and just stick with '=='.
Last but not least, don't forget
when you are comparing variables, you need to make sure that
you are comparing the exact same type.
So if you're comparing a string, if 'name1' is a string,
and 'name2' is not a string, it's not going to work.
You need to make sure you're comparing strings against strings,
numbers against numbers, and lists again against lists.
And actually, as an example, let's do this.
'ls1' = ['hello']'.
It only has one item in there, and 'ls2 = ['hello']' as well.
And then we can say 'if ls1 == ls2: print()',
that is very close to the bottom of the screen there, 'print(
"HELLO IS IN BOTH LISTS")'
And bam,
we are now comparing lists against lists.
Got to make sure they're the same though, because if this
changes at all, let's say this has a second item in here,
it no longer works.
So you got to make sure they're the exact same.
But you can compare lists and tuples, and dictionaries, and
sets against each other.