Welcome back. It's time for us to learn about a new Python type.
So far, we've seen integers,
which are good for storing round numbers, floats,
which are good for storing decimal numbers, strings,
which are good for storing sequences of characters like words,
in lists and tuples,
which are good for storing sequences of other items.
In this lesson, we're going to learn about a new Python type called Booleans.
Booleans are good for storing truth values and that
is whether something is true or false.
In fact, there are only two possible values for Booleans, true and false.
This is in contrast with pretty much every other type.
So, in integer might be negative 10,
five or one of any infinite number of values,
but Booleans are always either true or false.
As a result, we almost never write a Boolean literal,
instead we typically write expressions that compute values of Booleans.
So again, Booleans can only have one of two values, true or false.
We can write a Boolean literal by saying capital
F false for false or capital T true for true.
So, if I print out the value of false and you can see that I get false,
if I print out the value capital T true, then I get true.
Now, just to be sure that these are Booleans and not strings or any other type,
then I can say print out the type of
the expression truth and then print out the type of the expression false.
So, I should expect both of these to print out something like Boolean,
so when I run my code and I can see that type of true is class.
This is short for Boolean and type of false is also bool short for Boolean.
One thing to note about Boolean literals is that they're entirely different from strings.
So, before I print out the type of this expression capital T true and then
I print out the type of this expression capital T true but in quotation marks,
then I should see that this is a Boolean and this is a string.
It's a string whose contents just so happened to be a Boolean expression true,
but to Python, this is just a sequence of
characters starting with capital T and then R-U-E.
So, when I run my code I can see that the type of
this expression is a Boolean and the type of this expression is a string.
So again, we almost never actually write Boolean
literal expressions like capital T true or capital F false.
Instead we write expressions whose values evaluate to be a Boolean.
So, we're going to start with one kind of
Boolean expression called a comparison operator.
A comparison operator compares one thing on the left and then one thing on the right.
So for example, the first comparison operator
that will learn is that is equal to operator.
So, the value of this overall expression is going to be true,
if the thing on the left is equal to the thing on the right,
but it's going to be false otherwise.
So, let's see this in action.
So on line one, I print out is the value of five equal to the value of five,
so that thing on the left is five,
that thing on the right is five and so I should expect the value of
this overall expression to be true because five is equal to five.
Here on line two,
a print out is the value of five equal to the value of six,
so I should expect the value of this overall expression to be false.
Let's see if that's the case.
There are other comparison operators that we can use.
So, in addition to equals equals, there's the opposite,
which is not equal,
we write not equal with an exclamation point equals.
So, that's true if L is not equal to.
So, if the value
of L is not equal to the value of
R then the value of this expression is going to be true.
So, if I write is five not equal to six,
then I should expect this to be true and I can see when I run my code that it is.
In addition to equals equals and not equal to,
there's less than, less than or equal to,
greater than, and greater than or equal to.
In all of these expressions expect one thing on the left and one thing on the right.
So, let's write out a few more Boolean expressions.
So, I can say print out is the value of the expression three less than the value of four.
I should expect that to be true.
I can print out is the value of four less than or equal to the value of four,
I expect this to be true because four is equal to four.
I can print out is the value of five greater than the value of six,
I should expect this to be false and if I print out is the value
of seven greater than or equal to the value of eight,
I should expect this to be false as well So,
you can see that these are the most common types of comparison operators that we have,
equal to, not equal to, less than,
less than or equal to,
greater than, greater than or equal to.
They compare the thing on the left with the thing on
the right and the value of the overall expression is going to be a Boolean,
again, either true or false.
So, let's answer a multiple choice question.
Which of the following is a Boolean expression?
Select all that apply.
So, the value of this expression true is a Boolean literal expression,
so I should say this is a Boolean.
The value of this expression,
three equals equals four is true if three is equal to four,
but because those two aren't equal,
it's going to be false,
but this is still a Boolean expression,
so I'm going to say yes, this is a Boolean expression.
This addition between three and four has the value seven that's not a Boolean,
that's an integer, so this is not a Boolean expression.
The value of this expression,
three plus four equals equals seven is
a Boolean expression and the value of this expression is the string false and so,
this is not a Boolean expression.
So, I'm going to say A,
B, and D are all Boolean expressions.
One thing that I want to note really quickly with this equals equals
Boolean expression is that this is entirely
different than saying something like X equals three.
So, if I say X equals three,
this is an assignment,
that assigns X to three,
if I say X equals equals three,
then this is a comparison and the value of
this overall expression is going to be a Boolean and if X is equal to three,
it's value's going to be true,
if X is not equal to three,
then it's going to be false.
But again, the way that we distinguish between assignment
and comparison is assignment uses one equals,
comparison uses two equals.
That's all for now, until next time.