-
Hi. This lecture is all about true and
false values, also known as Boolean
-
values. In this lecture, we'll explore the
Python's type bool, and the operators that
-
we can apply to Boolean values. Earlier,
we used Python's arithmetic operators like
-
multiplication and subtraction. And now,
we're going to use some of Python's
-
comparison operators. For example, let's
compare values three and four using the
-
less than operator. When this expression
is evaluated, we're going to get a true
-
value or false value back. The type of
value that we get is type bool. Let's
-
compare three with eight, asking if three
is greater than eight, and it's not. So,
-
the value that this expression evaluates
to is false. When we evaluate eight
-
greater than three, we get true. And if we
were to evaluate 3.5 greater than or equal
-
to 3.4, it's also true. Lets compare two
ends, seven with seven. Notice that the
-
operation that we're performing now is the
equality operation. And, we need to use
-
two equal signs, not one, to signify
equality. That's because the single equal
-
sign is already used for the assignment
operation. Seven is equal to seven. How
-
about seven and 7.0? I type into operand
with a type float operand. This is also
-
true. Let's assign a couple of variable
values, x gets seven, y gets eight. And
-
now, we can apply the same equality
operator to two variable operands. First,
-
we work up the value that x refers to,
which is seven. And y refers to eight. And
-
then, seven is compared with eight.
Another operator is the inequality
-
operator. We can check whether three is
not equal to four, and that's true. The
-
comparison operators take two values and
return a Boolean value, either true or
-
false. Python also has three logical
operators, which are operators that are
-
applied to Boolean values and yield
Boolean results. The first logical
-
operator that we'll use is the not
operator. And we'll begin by creating a
-
variable grade and assigning it the value
80. So, grade refers to 80. Now, let's
-
write up an expression that checks to see
whether the grade i s a passing grade. Is
-
grade greater than or equal to 50? And
that's true. In Canada, 50 is a pass.
-
We'll apply the not operator to that
expression now. So, we're going to check
-
to see whether grade is not greater than
or equal to 50. The order that this
-
expression is evaluated works from inside
out. So, the grade greater than or equal
-
to 50 part of the expression is evaluated
first and that gives the value true and
-
then the not operator is applied to true.
Something that is not true is false. And
-
that's the result that we get back. We can
apply this not operator two times in a
-
row, saying that this is asking whether
this is not, not true, which is equivalent
-
to just saying, is grade greater than or
equal to 50? So, rather than including two
-
nots in a row, eliminate double negation,
instead write the simpler version of that,
-
which is to just say, it's great, greater
than or equal to 50. Next, let's use the
-
and operator. First, we'll make another
variable named grade two. That refers to
-
the value 70. And now, we'll write an
expression involving both variables grade
-
and grade two. This expression, we'll
check to see whether both of these are
-
passing grades. So, is grade greater than
or equal to 50, and is grade two also
-
greater than or equal to 50? And evaluates
True, if both operands are true. So,
-
first, this expression is evaluated and it
is true, so then, this expression is
-
evaluated and it is also true, making this
entire result a true result. Let's change
-
the value of the variable grade for moment
and set it to 40. We'll rerun this Boolean
-
expression involving the and, and check to
see what we get. Because this first
-
operand is false, the Boolean expression
is false. And we don't even go on to check
-
the second operand's value. Now, let's set
grade back to 80, and this time, change
-
grade two to be a failing grade. When this
is expression is evaluated, first, this
-
part of the expression is evaluated. And
that's true so we move on to evaluating
-
this part of the expression which is
false. And so, the expression ev aluates
-
to false. To summarize, and again, only
evaluates to true if both of its operands
-
are true. Otherwise. it evaluates to
false. Finally, lets use the logical
-
operator or which also applies to two
operands. We'll start by assign grade and
-
grade two to passing grades. And now,
we'll write the same expression as before
-
replacing the and with an or. This
expression will evaluate to true if at
-
least one of the operands is true. So in
this case, we get true. Now, lets assign
-
to grade a failing grade and reevaluate
the expression. Python will first evaluate
-
the first part of this expression and
determine that it is false. So, it will go
-
on to evaluate the second part of the
expression, which is true, and because, at
-
least one operand is true, the expression
evaluates to true. If we set grade to a
-
passing grade, and grade two to a failing
grade. Then, when the expression is
-
evaluated, it works as follows. Because
grade is a passing grade, the, the
-
expression is evaluated to true at this
point, without even having to go on to
-
look as the second operand. So, to
summarize, a bool, the Boolean operator or
-
evaluates to true if at least one of its
operands is true. And it evaluates to
-
false otherwise. Now, let's combine the
operators into single expressions. I've
-
assigned grade and grade two passing
grades of 80 and 90, and I'd like to
-
evaluate this expression. I'm going to
apply not to grade greater than or equal
-
to 50, or grade two greater than or equal
to 50. And there are a couple of different
-
ways that we can interpret this
expression, depending on order of
-
precedence. The first would be to have the
or operator applied first, and I'll use
-
parentheses to signal that, followed by
the not operator. The second would be to
-
actually have the not operator apply first
to the first part of the expression. And
-
then, the or operator apply second. So,
not first, followed by or, or, or first
-
followed by not. Let's evaluate the
expression and see what happens. [typing
-
sound] The value that the expression
evaluates to is true. An d let's use the
-
parentheses to see which of the two
operators applied first. We'll begin by
-
putting the parentheses around the or part
of the expression, ensuring that or
-
applies before not. And when we do that,
the result is false. So, that's not what
-
happened when we left off the parentheses,
parentheses. And that means that that's
-
not the order of precedence. Instead, the
order of precedence is that not is applied
-
first, as I can show here, followed by or.
The order of precedence for logical
-
operators is not, and, and then or. And
when we're working with multiple logical
-
operators within an expression, we can use
parentheses to ensure that the operations
-
apply in the order we'd like without
having to worry about what the order of
-
precedence is. Sometimes, we'll use
parentheses to make an expression more
-
readable. For example, in this expression,
the arithmetic operators have higher
-
precedence than the Boolean operators or
logical operators, so these parentheses
-
are unnecessary but we include them for
readability. Instead, we could have left
-
off the parentheses and had the following,
where some, I find a little harder to read
-
and understand.