- [Instructor] In this video we're going to
take a look at the Boolean Operators
called and, or, and not.
Now these operators enable you to create more complex
conditions then what we've been looking at so far.
So for example we've done conditions like great,
greater then or equal to 60
and that's just a simple condition
and it's true if the great is greater then or equal to 60
and false otherwise,
but sometimes we need to test multiple values
and form a more compound or complex condition
in order to get the results that we require.
So lets say we go and define a variable,
we'll call it gender and we'll set it to the string female
and lets create another variable,
we'll call it age and we'll set it to 70
and lets say we want to print the strings senior female,
only if the gender is female and the age is greater then or
equal to 65, so we can go and write such an expression.
So if gender double equal sign female
and of course you have to spell it correctly.
By the way if we were to use different key sensitivities
in the two strings they would not compare as equals,
so please keep that in mind.
And then, we want to combine this with another condition,
age greater then or equal to 65,
so if both of those are conditions are true,
then we would like a print statement to execute
that displays senior female.
(typing)
And in this case, both conditions are true,
so indeed, senior female gets displayed.
Now the way this expression works
is the condition to the left of the and gets evaluated.
If this is false then,
the if statement terminates immediately
and we would move onto the next statement
after the if statement.
If it's true,
I still don't yet know if the whole condition is true
because I need both of these conditions to be true
for the entire condition to be true,
so if this is true
and age is also greater then or equal to 65,
which it is, then and only then,
will this print statement actually execute as it did
in this particular case.
So, what's happening in the evaluation of this expression
is a case of short circuit evaluation in python.
If the left side of this expression were false,
the rest of the expression would not evaluate.
So that's a little bit of a performance feature
of the and operator and that's true of the
and operator in most sea based languages.
Now we also have the or operator.
So for this one, let's go and create another variable,
we'll call it semester average
and we'll give it the value of 83
and we'll create another one called final exam
and we'll give that 95,
by the way, notice the naming convention
on these two variables.
This is from the style guide from python code.
When you have multi word variable names,
they recommend for python,
that you separate the words with underscores
unless you are coding for some library that already has
or some code base I should say, that already has a different
naming convention in which case you should use
the naming convention of that code base,
but generally when you have mulit word names
you're going to separate them with underscores for variable
names and function names in the python programing language.
So let's say we want to use these two variables
in a condition that says,
if the semester average is greater then or equal to 90
and, or, excuse me, or the final exam grade
is greater then or equal to 90 in either of those two cases
we're going to give the student an A.
So lets try that,
so if, oops I didn't mean to hit the tab there,
so if semester average is greater then or equal to 90
or
final exam
is greater then or equal to 90
in either of those two cases or both of those cases
we would like to print,
quote student,
I cannot type,
student grade,
or student gets and A, sorry about that.
All right, so let's go ahead and execute that,
now in this case, semester average is not
greater then or equal to 90, so this is false.
But it's either or, either of the two can be true
in order for the entire thing to be true.
So if semester average is false, I'm sorry,
if the semester average is greater then 90, is false
or
gets evaluated
and I then look at the condition to the right of that
to see if the final exam is greater then or equal to 90.
In this case it is, so student gets an A, is displayed.
If both of these are false,
then the entire expression is false.
Now again, we get short circuit evaluation,
so if we already know the left side is true,
then the right side
would not get evaluated in that particular case.
So with the or operator,
if the left is true or the right is true
or both are true, the entire expression is true
and the body will execute, otherwise if both are false,
the entire expression is false.
Now there's one other logical operator
or Boolean operator as their called in python,
which is the not operator.
So, let's go and create a grade variable.
We'll give it the value 87 for the moment here
and lets do kind of a sentinel control loop type of test.
I'll do it with an if statement here
just cause we don't need a loop for the moment.
But let's say we have the condition,
not grade double equals minus one.
So you may remember
when we were doing our sentential controlled iteration,
we used a flag value to indicate when to terminate
a given loop, so in this case, we have the not operator.
So what's going to happen is the not operator will reverse
the condition to its right,
so if grade is equal to minus one,
this highlighted piece would be true
and not true would evaluate to false,
if the highlighted piece is false,
not false would evaluate to true.
So it just reverses the value of the condition,
so in this case I would print
(typing) the next grade
(typing)
is and then we'll simply output the grade.
We could use a format string
or an f-string if we want to here as well.
Now at the moment,
grade of course is
not equal to minus one,
so grade double equals minus one will be false,
not false will be true,
and therefore this print statement will execute
and you see the next grade is 87 does indeed get displayed
in that case, so,
this is just a way to reverse the value of a condition,
you can also apply the not operator to other values,
so remember,
every object has a true or false value associated with it
any numeric value that is zero is considered to be false,
any numeric that is nonzero is considered to be true,
so the not operator would simply reverse those conditions
and as I told you, you'll see more complex objects
be used as true false values in
number of scenarios in python
and will encounter a lot of those as we move forward.