5. Boolean variables in python
Oct 9, 2023
Boolean variables can be either True or False. These variables are very important in programming because we often have tasks or decisions to make which depend on whether different logical conditions are met. We need a way to define a True or False outcome. In this video we describe these variables and we also mention a brief word about the namesake of Boolean variables, George Boole.
Copyright image attribution:
https://en.wikipedia.org/wiki/George_Boole#/media/File:George_Boole_color.jpg, unkown author http://schools.keldysh.ru/sch444/museum/1_17-19.htm
In this video, we're going to talk about Boolean variables.
We said before that Boolean variables are used
when we need to store something that's either true or false.
So, for example, we'll use these when we're making comparisons.
Is this number greater than that? Is this equal to that?
Is this one less than that?
The answer it's going to return isn't going to be a number value,
it's going to be a true or false value.
So we need a Boolean-type variable to hold that information.
So let's come over to our spider and do some examples of this.
So first off, any value will be true for the most part, except for 0.
So if I take a number, x equals 5,
so I'm going to store the value of 5 in for x.
And then if I ask what type of variable that is,
let's go ahead and try that.
First we're going to run it and figure out
we're going to store that variable, so x is stored in as 5.
And now over here, we're going to ask it,
there's a function, Boolean, which returns,
if you put an argument in,
it will tell you whether or not it's true or false.
So let's go ahead and tell it to evaluate x.
And what we find is that it says it's true,
because we have a value of 5 there.
So what will make it be false?
Well, just like Boolean logic, if you're familiar with that,
1s and 0s, then what we have to do is store 0 in for x.
So what if we take this? What if we say y equals 0?
If we run that, like so,
and now we ask it to do the Boolean of y,
what we're going to find out is it is false.
So almost anything that you put in will be true.
0 will give you false,
and if you tell it that something is false,
it will give you false.
So let's say this.
We'll say that a is equal to true,
and we're going to say that b is equal to false.
When we run it now, we're going to get two new variables up here,
and these are type Boolean.
So for those, again, if we took and we did type,
and we did x from before,
it's going to tell us it's an integer type.
Or we could look up here in our Variable Explorer,
but if we do type and we now ask it for a or b,
we're going to see that it responds and tells us that it's a Boolean.
Well, what about words?
What if you ran a string through?
We had our string, dog equals scout.
What will happen if we ask it the Boolean value of a string?
Boolean of dog is going to be...
Ah, it gives us an error.
Oh, because we haven't defined it yet.
I didn't run the program over here.
So when I hit 5 first and now run it,
let's go ahead and try it again.
Now when I ask it for the Boolean of dog, it's true.
But if we take another string that's totally empty,
we'll say cat, and we don't give it a name.
It's currently empty.
So let's run that, and then let's come over here
and let's ask it for the Boolean of cat,
and it will return false.
So an empty string will return false.
Later we're going to learn about lists.
And so lists can be a list of items within an array,
and if those are an empty array and you ask it the Boolean value,
then it will just give you false.
And also if you give it the value of none.
None is a special value that we can give to something.
So we could say birds equals none.
So now birds will have a category.
Oops, I did the minus equals,
so it's trying to subtract from it.
That's not what we want.
Let's try that now.
So now up here, birds is a none-type object,
and so if we ask it for the Boolean of birds,
that variable which is a none-type object,
it will also come back false.
What else can we do?
How about this?
Any number will give you a true value,
even negative numbers, right?
So let's say we give it a negative number
and a decimal number, right?
So negative number, right?
And we're going to say that it's equal to negative,
I don't know, 6.7, okay?
So again, if we run that,
and then we ask it what the Boolean value will be,
neg number, it says it's true.
Even though it's some weird negative number,
it's still saying that it's true.
So now the last thing we should cover before we leave Booleans
is there's this idea of Boolean logic.
This is pretty cool stuff.
In fact, if you go to the internet,
Boolean, the word Boolean was named after this guy here,
George Boole, right?
Really interesting philosopher, mathematician, and logician.
Really clever guy.
He worked with logic.
He wrote this laws of thought book
which contained the concept behind Boolean algebra,
which is now foundational to a lot of computer science
and other fields.
And I just thought it was fascinating.
I was reading here about this guy,
and I read this about how he died in 1864.
He was a professor like me.
He's walking from his home to the university in the rain
at Litchfield College in Ballantymple.
He walked three miles,
and then he gave his lecture in his soaking wet clothes.
He got sick, and, you know,
she actually wrapped him in wet blankets
because that's what they believed was the way to cure it back then.
And so, sadly, this person died of pneumonia.
And here I am in my basement,
not doing as much to promote science.
But if he can, you know,
deliver his lecture after three miles in the rain,
even at the risk of, and ultimately, the loss of his life,
you know, I think that we can all be harder-working scientists too.
In any case, Boolean logic uses three keywords.
Its keywords are AND, OR, and NOT.
So let's see what those are.
We previously defined the Boolean variables A and B.
A was true, and B was false, right?
So now let's go ahead and use some of these Boolean logic variables,
Boolean logic operators, right?
So let's do this.
Let's do A AND B.
AND says the two conditions must be true in order for it to return true.
And since A and B are not the same,
it's going to return false, right?
So that's one possibility.
What about A OR B?
If we do A OR B,
then only one of them has to be true for it to return true.
So it's going to return true, right?
What about A NOT B, or NOT A?
NOT is going to be the opposite of whatever A is.
Since A was true, this is going to return...
Oops, we typed NOT B.
Let's try NOT A.
NOT A is going to be the opposite. It will be false.
NOT B, since B was false, it's going to return true.
So it flips it, right?
And then you can combine these together.
So let's make another variable.
Let's make C, which is also false, right?
So let's run that so we store C in our memory up here.
Alright, now we've got C stored up here.
Now let's try these scenarios.
First off, let's ask, is A equal to B?
And it's going to say false.
Then we can ask, is A NOT equal to B?
This is a comparator operation, which we'll talk about in the next video.
And it says true, A is not equal to B.
But how about this one?
Let's say A OR C AND B.
So what it's asking says, either A is true OR C AND B must be true.
And what do we know about that?
Well, we already know that A is true, so that should return true.
What about this one?
Let's do A AND B OR C.
Well, we know that C is not true, it's false.
And then A AND B, only one is true, not both.
And so neither condition is met, so it's going to turn back false.
So that's the idea behind Boolean logic.
We'll do a lot more Boolean logic when we get to if statements,
and in our future video when we talk about operators.
But that's what we've got for now.

