1
00:00:04,360 --> 00:00:09,520
Let's discuss truthy values. So I want to
actually discuss something that you'll

2
00:00:09,520 --> 00:00:12,840
see being done when you're looking at
code on the internet, for example,

3
00:00:12,840 --> 00:00:16,780
and I'm going to keep this short and I'm
including it here for completeness.

4
00:00:16,780 --> 00:00:20,320
Don't worry if you don't understand this. It'll
be there in the back of your mind and

5
00:00:20,320 --> 00:00:27,040
it'll make sense when we come to use it.
So I'll start by referring to the Python documentation.

6
00:00:27,040 --> 00:00:28,980
I'm linking the resources
section in this video. We're going to

7
00:00:28,989 --> 00:00:33,340
paste it into here. So it starts by
saying that an object can be tested for

8
00:00:33,340 --> 00:00:38,109
truth value for use in an if or while
condition. We'll see an example of that

9
00:00:38,109 --> 00:00:42,300
in a moment. Now just below that, you can
see there's two bullets there,

10
00:00:42,300 --> 00:00:47,739
indicating values that will evaluate to
False. So it mentions two constants - none

11
00:00:47,740 --> 00:00:53,300
and False. We haven't seen none yet but
obviously, False is False. Zero is also

12
00:00:53,300 --> 00:00:57,460
considered False, whether that's an
int zero or a floating point zero, it's

13
00:00:57,460 --> 00:01:03,660
still False. And finally, empty sequences
and collections are also considered False.

14
00:01:03,660 --> 00:01:07,760
So what does all this mean? We're
going to go back to the code

15
00:01:07,760 --> 00:01:11,580
and we're going to stay with our truefalse.py file
from the previous video. We're going to

16
00:01:11,580 --> 00:01:14,500
delete all the code now.

17
00:01:14,500 --> 00:01:16,990
If you've been experimenting, and I hope you have, you

18
00:01:16,990 --> 00:01:19,930
may want to keep your code. In that
case, just create a new python file for

19
00:01:19,930 --> 00:01:22,360
these examples.
Alright so let's have a look at things

20
00:01:22,360 --> 00:01:27,370
that evaluate to False when used in a
boolean expression. The documentation

21
00:01:27,370 --> 00:01:33,130
said that zero was treated as False,
so let's type some code: if 0 colon,

22
00:01:33,130 --> 00:01:39,280
on the next line, print parentheses and True in double quotes. We'll add an else there, else

23
00:01:39,280 --> 00:01:47,360
colon print parentheses double quotes
False.

24
00:01:47,360 --> 00:01:52,780
Now we don't actually have to execute this code because
my IDE's telling me, if I hover over line 2 there,

25
00:01:52,780 --> 00:01:59,770
this code is unreachable. 0 won't evaluate to True,
so line 2 can't be executed.

26
00:01:59,770 --> 00:02:06,880
We'll run it anyway and it prints False.
Alright so zero is evaluated to False

27
00:02:06,880 --> 00:02:13,040
when it appears in a boolean expression.
Empty sequences also evaluate to False.

28
00:02:13,040 --> 00:02:16,760
We've only seen one sequence type so far,
the string type.

29
00:02:16,760 --> 00:02:19,920
So if an empty string evaluates to False,
we can do something like the following

30
00:02:19,920 --> 00:02:26,920
I'm going to type in. So on line six, name equals
input parentheses double quotes

31
00:02:26,920 --> 00:02:33,700
please enter your name, and we'll put a colon there
and we want a space, and closing double quote and right

32
00:02:33,700 --> 00:02:39,280
parenthesis. Then we'll put if name colon
print on the next line parentheses

33
00:02:39,280 --> 00:02:46,160
double quotes. We're going to type Hello
Hello comma then left and right curly braces,

34
00:02:46,160 --> 00:02:51,139
then closing double quote dot format
left parenthesis name, then we've got the two

35
00:02:51,140 --> 00:03:03,140
right parentheses there, else colon
print Are you the man with no name?

36
00:03:03,140 --> 00:03:09,640
Alright, so both of those if statements on line 1
and line 7 look a bit strange.

37
00:03:09,640 --> 00:03:13,489
Python expects a boolean expression
after if, which is why it tries to

38
00:03:13,489 --> 00:03:18,590
evaluate 0 and name as boolean values.
We've already seen that 0 evaluates to

39
00:03:18,590 --> 00:03:24,380
False. Let's run the program and see what
happens on line 7.

40
00:03:24,380 --> 00:03:31,040
Alright, so I'm going to start by entering my name Tim. Because Tim isn't an empty string, it evaluates as

41
00:03:31,040 --> 00:03:36,320
True on line 7 and we get the message
Hello Tim. Run the program again.

42
00:03:36,320 --> 00:03:40,120
This time I'm just going to press enter here
without typing anything. So that then

43
00:03:40,129 --> 00:03:44,329
results in name being an empty string
which evaluates as False and that's why

44
00:03:44,329 --> 00:03:48,340
we get the message Are you the man with no
name? So that's actually quite useful.

45
00:03:48,340 --> 00:03:51,820
We've got a quick way now to check if
the user types something when we ask

46
00:03:51,820 --> 00:03:55,940
them for input. You'll see things like
that being done, but I suggest you avoid

47
00:03:55,940 --> 00:04:00,040
doing it until you're really comfortable
with Python. So until then what I do,

48
00:04:00,049 --> 00:04:03,620
I suggest you do, is write line 7 a
different way, so I want to comment that

49
00:04:03,620 --> 00:04:10,159
out so you've still got it there, and instead
I'm going to type it as if name is not

50
00:04:10,159 --> 00:04:14,930
equal, and two double quotes with a colon.
So now what we're doing on line 8 is

51
00:04:14,930 --> 00:04:19,009
explicitly comparing name to an empty
string. It's more obvious what we're

52
00:04:19,009 --> 00:04:22,660
doing there and that's the way I suggest
you write your conditions to begin with.

53
00:04:22,660 --> 00:04:25,240
In the next video we'll finish
discussing boolean expressions by

54
00:04:25,240 --> 00:04:29,140
looking at two more operators we  can
use; in and not in.

55
00:04:29,140 --> 00:04:31,980
See you in the next video.

