Okay, let's talk about Booleans.
Booleans, in my opinion, are the easiest data type.
They're easier than strings, which are really just sentences.
They're easier than lists or even tuples, which you can't
really do a whole lot with.
A Boolean is either a True or False answer, and it actually just
has to be the word True or the word False.
And in Python it actually has to be a capital T and a capital
F. That's important.
So let's say I've got a variable called 'is_30', because that's
my age right now. I'm 30.
I can set that to True with the capital T.
Then I can say 'is_30', comes back as True.
The 'type(is_30)' is a bool, it's a Boolean.
So bool is short for boolean.
We can also try another one.
Let's say are you in space?
So 'is_in_space'.
I am currently not in space.
I am definitely on Earth right now.
And what I can do is print the 'type(is_in_space)', and that's
going to come back as a Boolean.
And as a second print parameter I can just put in here what
it actually is, 'is_in_space', and this will come back as False.
There it is, class bool and False.
Now let's take a look at this as an object.
We talked a while ago that anytime you see class, this is an object
So let's use this 'is_in_space.', and hit tab.
And then you can see really not a whole lot of options in
here. We can get the 'real' number, we can get a 'numerator',
we can convert it to byte,
we can get the bit_length if we wanted to.
But generally speaking, this is not super useful.
This is actually supposed to be a function. So
anytime you see this, it says 'function', and then whatever
that is supposed to be, just put parentheses at the end.
The 'bit_length' of this is 0.
And let's do 'is_30', because this is going to be an interesting thing.
'is_30.bit_length' is 1.
And this is where the idea of ones and zeros come up in
computer and computer science.
So anything that's True is equal to 1 and anything that's
False is equal to 0.
That's where we get ones and zeros from, is the 'bit_length'.
So False has no length at all, and True actually has one.
So you got ones and zeros.
Yes or no, true or false, black and white.
That's really all a Boolean is.
Now let's take a look at some typecasting.
I want to get this idea out there a little earlier than just
waiting for that video to show up.
So we can actually do some typecasting here.
We can say of 'course = "Python for Everybody".
Now we know that 'course' is a string, and if we do this 'type
(course)', it says it's a str, but we can typecast things
as a Boolean with the 'bool()' function, 'bool()'.
Let's throw this in here.
'course'. 'course = True'.
What if we have an empty string, though?
So let's say we ask someone for their 'name', and that 'name'
came back as empty.
Let's say they just answered with absolutely nothing
for some reason. We can do 'bool(name)', and it's False.
So in Python, and this is good to know right now, in Python,
when you have an empty string, it's False.
When you have a string with anything in it, it is True.
You can even do this.
Just throw in an empty list, '[]' if you want to.
An empty list is False, but a list with even just one item
in it is True.
You can do the same thing with a tuple.
So let's just make sure that this understands it's a tuple.
That's why I put that extra comma in there, and I had one
too many things. There we go.
That one's True.
But if we have an empty tuple without a comma because there's
no items in there, it's False.
Again, we can do the same thing with a dictionary.
If there is nothing in a dictionary, you could even consider this a set.
It's False.
But as soon as you put something in there, anything, it's
True. So behind the scenes, Python is able to evaluate any
data type as either True or False,
it can turn it into a Boolean.
Now, this is really important, because down the road, we're
going to be learning about comparison operators.
And a program is always looking for something to be True.
If it's True, take an action.
If it's False, take a different action.
But for the time being, all you need to know is that Booleans
really are True and False.
And that's it.
That's all there is to a Boolean.
Nice and simple.