-
PROFESSOR: All right.
-
Another day, another video.
-
In this topic, I'm going to
explain logical operators.
-
Logical operators are used
on conditional statements
-
such as an if statement.
-
There's three types
we'll talk about--
-
AND, OR, NOT.
-
Each exhibits
different behavior,
-
and we'll check to
see if two or more
-
conditions are true, or checks
if at least one condition is
-
true.
-
NOT is a little different.
-
NOT will change the
result of a condition.
-
If a condition is
true, it's now false.
-
If it was originally
false, it's now true.
-
Here's an example.
-
Say we have a program to
check the temperature.
-
Temp equals 25.
-
The temperature will be in
Celsius in this example.
-
If the temperature falls
within a certain range,
-
I would like to
print a message that
-
says the temperature is good.
-
I can check more
than one condition
-
using the AND logical operator.
-
Let's check if our temperature
falls between the range
-
of between 0 and 30.
-
If temp is greater than 0
and temp is less than 30,
-
then we will print the
temperature is good.
-
If this condition is true
and this condition is true,
-
then we will execute this code.
-
Else we will print the
temperature is bad.
-
Our temp is currently 25.
-
The temperature is good.
-
What if it was negative
10 degrees Celsius?
-
Well, the temperature is bad.
-
What if it was over 30,
like 40 degrees Celsius?
-
Well, then the
temperature is still bad.
-
Using an AND logical operator.
-
If one of these
conditions is false,
-
then this entire statement
will evaluate to be false.
-
We would then skip over any
sub-code and continue on.
-
Using the AND logical
operator is pretty convenient
-
if you need to check
to see if something
-
is within a certain
range, like a temperature.
-
There is another way
we can write this, too,
-
using the OR logical operator.
-
Let's replace AND with OR.
-
If at least one of these
conditions is true,
-
then the entire
statement is true.
-
How about if temp is less
than or equal to 0, or if temp
-
is greater than or equal to 30?
-
Then the temperature is bad,
else the temperature is good.
-
Currently, our
temperature is set to 40.
-
Well, the temperature is bad.
-
This is false, but this is true.
-
Using the OR logical operator,
only one of these conditions
-
needs to be true.
-
If our temperature
was negative 15, well,
-
the temperature is still bad.
-
But if it was 20, well, then
the temperature is good.
-
AND, as well as OR,
are fairly similar.
-
With AND, two or
more conditions need
-
to be true for the
statement to be true.
-
With OR only, one of
them needs to be true.
-
Is this true, or is this true?
-
Now, NOT is a little different.
-
Let's say we have a Boolean
variable named sunny.
-
Is it sunny outside?
-
I'll set this to be true
using an if statement
-
if sunny is equal to true.
-
But if you're working
with a Boolean,
-
you can just shorten this
to the Boolean itself.
-
If sunny, then print
it is sunny outside.
-
Else, print it is
cloudy outside.
-
Sunny is set to true.
-
Therefore, it is sunny outside.
-
If sunny were set to false,
it is cloudy outside.
-
Using the NOT logical operator
that would flip the result.
-
So if not sunny, then
it is cloudy outside.
-
Else, it is sunny outside.
-
Sunny is set to false.
-
It is cloudy outside.
-
If sunny were true, well,
then it's sunny outside.
-
The NOT logical operator
will change something
-
that's true to be false.
-
Or if it was originally
false, it's now true.
-
All right, everybody.
-
So those are logical operators.
-
They're used with
conditional statements.
-
There's AND, OR, NOT.
-
We'll have more practice
with these in the future.
-
And yeah, those are a few
logical operators in Python.
-