Instructor: Welcome back.
In this video, I wanna talk
about the idea of truthy and falsy.
What do they mean?
And you'll hear this a lot in Python programming.
You see, up until now, you've used booleans here as values.
So it was really easy.
If true and true, then run this code.
But Python can do other things as well.
For example, if this was five and this was, let's say hello,
what do you think will happen here in our program?
Well, if I click run,
you are old enough to drive
and you have a license, is that what you expected?
And this is because what Python does
underneath the hood is actually convert these
to booleans for you.
So it's almost as if
it is doing this, remember type conversion.
We're converting the type
to boolean
as soon as we get to this if statement
because Python is saying, Hey
I want true or false, I don't care
what is old is
I don't care what is licensed is just gimme true or false.
So I know whether to do this or to go to the else block.
Now, if I was to just print,
let's do this here.
Let's do print
boolean
hello.
And then finally print boolean
five.
Let's see what we get.
We get true, true.
And this is what we call a truthy value in Python.
That is, it's a truthy value because if we run the boolean
type conversion on it, it evaluates into true.
What's a falsy value?
Well, if I do zero, and if I do, let's say an empty string
and I click run,
those are falsy.
That is, it's not actually false, but it is falsy because
if we run boolean on it to Python, it's going to say, Oh
this is false and zero.
That's false.
And that kind of makes sense, right?
And if you actually go to Stack Overflow, if you've never
seen Stack Overflow before, if you ever have programming
questions and you're Googling them, usually the top five
answers we'll contain either one or two
Stack Overflow answers.
And basically what happens is people ask questions like,
What is truthy and falsy and Python?
And you get answers.
So you can see over here that it has four answers
and you usually look for the check mark, which means
that this is a verified answer.
It means that, hey, this question owner has accepted it
as the best answer.
So the person who asked this question just said, Yep
this is the right answer.
This is the best one.
But you also have other people voting.
So you can see that this answer received 46 marks.
And although it was marked
as the correct answer by the user, maybe somebody else came
along later on and gave an even better answer.
So now we have 123 up votes.
And you see that this answer is a lot better.
All values are considered truthy except for the following.
So you can actually see here that in Python
these values are considered falsy.
You can see zero here, you can see an empty string here
and then there's a few other ones.
Empty set.
You have a empty dictionary, empty list, empty tuple.
You have decimals, you have things like fractions.
You have 0.0.
False is that.
And also none is falsy.
So that if I type in none here,
that's false too.
Now, you don't need to memorize these
because most of the time it, it makes sense, right?
But it helps us do conditional logic very nicely.
For example, let's remove this.
And let's say that we have a user
and this user has to have a password and a username.
If let's say the username is
Johnny
and the password is 1, 2, 3.
Well, instead of me saying,
Hey, if this user has password and username
and we're not checking if the username exists
or password exists, we just wanna make sure,
hey did this person fill out the form
and did they try to submit a username and password?
I can just simply say password and username
and I don't have to worry
about converting this to a boolean or anything like that.
Because if password and username exists
then try to log them in.
And you see that it reads a lot nicer
because each one of these get evaluated
into a truthy and falsy value.
It's a nice little feature to keep Python,
well, nice and simple, and reading like English.
I'll see you in the next video.
Bye bye.