-
[MUSIC PLAYING]
-
-
MOSH HAMEDANI: In Python,
just like the other languages,
-
we have three logical operators.
-
We have the logical and,
the logical or, and not.
-
Let's take a look
at a few examples.
-
So I'm going to start by
defining a variable name,
-
setting it to Mosh.
-
Now, let's say this is the
input that we get from the user.
-
We want to check to see if
name is an empty string.
-
How do we do that?
-
Well, earlier I talked about
the falsy values in Python.
-
So we have 0, empty string,
none, and an empty list.
-
These are all considered falsy.
-
So let's talk about
an empty string.
-
If this is interpreted
as false, then
-
if we apply the not operator
on it, we will get true, right?
-
So to check to see if
name is an empty string,
-
we can simply write a statement
like this, if not name colon.
-
That's it.
-
Print Name is empty.
-
Now, let's run this program.
-
Obviously, we don't
get that message.
-
However, if I change
this to an empty string
-
and rerun the program,
we get Name is empty.
-
But what if I add
a whitespace here?
-
Well, whitespace is
not considered truthy
-
because it's not
an empty string.
-
So, look, if I run the program,
we don't get the message.
-
To fix this issue, we can simply
call .strip method to get rid
-
of all the whitespace.
-
Now, let's run it one more time.
-
Beautiful.
-
So this is our not operator.
-
Let's take a look at
an example of and.
-
So I'm going to set age to 22.
-
Now let's see if age is
greater than or equal to 18
-
and age is less than 65.
-
We say you are eligible.
-
Good.
-
Pretty easy, right?
-
Now, here's a question.
-
How do we write that
expression in math?
-
We write it like this.
-
-
Well, more accurately, we
should add the equal sign here.
-
So age is between
18 and 65, right?
-
Well, I have good news for you.
-
In Python, you can simply
write this expression.
-
That's perfectly fine.
-
This is what we call chaining
comparison operators.
-
So we can rewrite this
expression in a simpler
-
and more meaningful way.
-
This is one of the
reasons that Python
-
is a highly popular language,
because the syntax is
-
so clean, without any clutter.
-
It's easy to
understand, and it comes
-
with all these best
practices and standards
-
for writing clean code.
-
So let's rewrite this
to 18, like this.
-
Good.
-
Now, let's run the program.
-
Obviously, this
person is eligible.
-