Welcome back.
In this video, I want to talk about the idea of Truthy and falsey.
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 Valdés.
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 where converting the type.
To Bolian, 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 all this, I don't care what is license's, just give me 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 Bolian Hello.
And then finally print Bolian 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 Bolian type conversion on it, it evaluates into true.
What's a falsey value?
Well, if I do zero and if I do, let's say, an empty string and I click run.
Those are falsie, that is it's not actually false, but it is false because if we run bullion 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 will contain either
one or two stack overflow answers.
And basically what happens is people ask questions like what is truth and falsity in Python?
And you get answers.
So you can see over here that it has four answers and you usually look for the checkmark, which means
that this is a verified answer and means that, hey, this question owner has accepted it as the best
answer.
So the person who asked this question just said, yeah, 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 one hundred and twenty three upvotes 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 falsey, you can see zero
here.
You can see an empty string here and then there's a few other ones empty set.
You have an empty dictionary, empty list, empty tuple.
You have decimals.
You have things like fractions.
You have zero point zero falsies.
That and also none is falsey.
So that if I type in none here.
That's false to.
Now, you don't need to memorize this 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 one, two, three.
Well, instead of me saying, hey, if this user has a password and username and we're not checking
if the username exists or password exists, we just want to 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 bullion or anything like that, because if password
and username exist, then try to lock them in and you see that it reads a lot nicer because each one
of these get evaluated into a truthy and falsey value.
It's a nice little feature to keep Python well, nice and simple and reading like English.
I'll see in the next video by.