Python for Absolute Beginners Transcripts
Chapter: Code that interacts with users
Lecture: Concept: Truthiness
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We saw the values that go into
0:02
the if statement or elif statement
0:04
or even the While statement have
0:06
to evaluate to True or False.
0:08
So let's talk about this idea of truthyness.
0:11
That maybe sounds like a weird word
0:13
that doesn't quite make sense
0:15
but the idea is that different types of data
0:17
can say for certain values I want you as far
0:20
as the execution goes as far
0:22
as the conditional blocks to go
0:24
we treat this is True and this is False.
0:27
So for example if you have just have the word False
0:29
well then obviously that's False
0:30
that is what the definition of False
0:32
is in the programming language.
0:33
But if you have a list and it has no data in it
0:36
well then that's considered False
0:38
but if it has like one, one, and seven.
0:41
It has some data in it then it's True.
0:44
if you have something called a dictionary
0:45
or a set those are the same kind of rules.
0:47
if you have a string well empty strings are False
0:51
but non empty strings like "Hello"
0:53
that's a True statement.
0:55
So this is a little bit of a weird idea at first
0:57
the idea is instead of trying to come up with some
0:59
kind of test like is the length of the number
1:02
of items in the list equal to 0, yes or no?
1:05
You can just say if the list
1:07
so it simplifies these tests.
1:09
Also for numbers if there's 0 that's False
1:12
otherwise they're True
1:13
and then none is a way to say this
1:16
variable has no value at the moment.
1:18
That's also False.
1:20
if it's not one of these things in the list
1:22
then it's True.
1:23
So we'll see an example of this in just a second
1:25
but this is leveraged all over Python
1:28
if you've got numbers you've got collections
1:30
you've got strings, very often the way you test
1:33
whether they're empty or not
1:34
or their have decent values in them or not
1:36
is to just do if the variable, carry on.