1
00:00:04,590 --> 00:00:09,870
Let's talk about boolean expressions,
True or False. So a boolean value is a

2
00:00:09,870 --> 00:00:16,960
value that can either be True or False.
There are only two values that a boolean can have.

3
00:00:16,960 --> 00:00:21,060
Python defines two constants,
True and False. Note that they're written

4
00:00:21,060 --> 00:00:25,830
with a capital letter. All names in
Python are case-sensitive, so True as a

5
00:00:25,830 --> 00:00:32,430
capital T and False has a capital F. A
boolean expression is an expression that

6
00:00:32,430 --> 00:00:37,290
evaluates to either True or False. We've
seen a few boolean expressions, so far in

7
00:00:37,290 --> 00:00:41,260
the course - things like age greater than
equal to 16, for example.

8
00:00:41,260 --> 00:00:45,320
Boolean expressions aren't restricted to just
testing numbers. We could also test a

9
00:00:45,330 --> 00:00:49,380
string value. Name is equal to Tim, so
that's a boolean expression that will

10
00:00:49,380 --> 00:00:54,140
evaluate to True if the value of name
equals the string Tim.

11
00:00:54,140 --> 00:00:58,640
It will evaluate to False if name has any other value.

12
00:00:58,640 --> 00:01:04,000
Boolean expressions can be very complex. For example, you might have a condition such as: if day is equal to

13
00:01:04,000 --> 00:01:08,680
Saturday, in double quotes, and
temperature greater than 27 and not raining.

14
00:01:08,680 --> 00:01:11,780
So you can see in that example,
we're checking to see whether day is

15
00:01:11,780 --> 00:01:15,800
equal to Saturday and whether
temperature is greater than 27, and we're

16
00:01:15,800 --> 00:01:20,810
also using and not here. Now in this
example, raining is a boolean variable.

17
00:01:20,810 --> 00:01:26,540
It has the value True or False. Not is used
to reverse the value.

18
00:01:26,540 --> 00:01:33,780
So we can use not to reverse a boolean value: not True is
False and not False is True.

19
00:01:33,780 --> 00:01:37,500
Alright, so back to the code now, so let's actually
create a new Python file.

20
00:01:37,500 --> 00:01:46,200
I'm going to call this one truefalse.

21
00:01:46,200 --> 00:01:49,820
Alright, we're gonna
start typing some code: day is equal to,

22
00:01:49,820 --> 00:01:57,680
in double quotes, Monday with a capital M.
Line 2, temperature is equal to 30.

23
00:01:57,680 --> 00:02:03,860
Next line, raining is equal to True, remembering
we need to use a capital T

24
00:02:03,860 --> 00:02:06,940
when using a boolean variable, as
we've done there. Then we'll have a

25
00:02:06,950 --> 00:02:12,890
look at that condition, so if day is equal to,
so it's two equal signs, Saturday

26
00:02:12,890 --> 00:02:18,780
in double quotes with a capital S, and
temperature is greater than 27

27
00:02:18,780 --> 00:02:28,940
and not raining colon, print parentheses double
quotes Go swimming.

28
00:02:28,940 --> 00:02:33,060
And the next line else :
then on the line 8, indented,

29
00:02:33,060 --> 00:02:40,040
print parenthesis double quotes and within the
double quotes, learn Python.

30
00:02:40,040 --> 00:02:44,959
Now didn't mention it when I created this file,
truefalse.py, but be careful with

31
00:02:44,959 --> 00:02:48,230
the names you choose for your Python
file names. You're starting to learn

32
00:02:48,230 --> 00:02:52,250
quite a few Python names and keywords
now, so it's getting easier to avoid

33
00:02:52,250 --> 00:02:58,140
using them as the name of your file. Now
if you'd called the file true.py with a capital T,

34
00:02:58,140 --> 00:03:00,680
then that wouldn't
be a good idea. You might not get an

35
00:03:00,680 --> 00:03:04,340
error straight away but it could cause
problems eventually. So just keep it in

36
00:03:04,340 --> 00:03:09,709
mind and try not to use Python names and keywords. And I do suggest experimenting

37
00:03:09,709 --> 00:03:13,040
with different boolean expressions to
get used to how they work, as a good idea.

38
00:03:13,040 --> 00:03:17,100
We're obviously doing that here with the code that we saw on the slides.

39
00:03:17,100 --> 00:03:20,420
It's essential to attach the
names to values before you use them in

40
00:03:20,420 --> 00:03:24,820
your boolean expressions.
If you don't attach date or value, for example,

41
00:03:24,820 --> 00:03:29,700
then you'd get an error on line
5. The same for temperature and raining.

42
00:03:29,700 --> 00:03:34,540
Alright, so let's run the program, and we
should see Learn Python,

43
00:03:34,540 --> 00:03:38,780
and you can see we're seeing that down the bottom there. To make sure this is working because we

44
00:03:38,780 --> 00:03:45,620
know that temperature is 30 and raining
set to True, let's change the day to Saturday,

45
00:03:45,620 --> 00:03:48,920
remembering I need to type it exactly how I've typed Saturday on line 5,

46
00:03:48,920 --> 00:03:55,300
in this case, with a capital S. Run that,
and in fact, we're still learning Python. Why?

47
00:03:55,300 --> 00:04:00,100
Well that's because it's raining. We set
raining to True on line 3.

48
00:04:00,100 --> 00:04:05,800
All three parts of the condition, on line 5, must
evaluate to True in order for the entire

49
00:04:05,800 --> 00:04:11,500
condition to be true. The last part of
the condition on line 5 is: and not raining.

50
00:04:11,500 --> 00:04:16,459
So raining is true, as we can see on line 3. That means that not raining is false.

51
00:04:16,459 --> 00:04:22,060
So what we can do here is change
the and, here in line 5, to an or,

52
00:04:22,060 --> 00:04:27,560
and once we do that and run the program again, this time we get the output, Go swimming.

53
00:04:27,560 --> 00:04:31,260
When you're experimenting, remember which
strings have to match exactly.

54
00:04:31,260 --> 00:04:36,960
On line 1, we've got Saturday with a capital S, which matches Saturday in the condition on line 5.

55
00:04:36,960 --> 00:04:40,560
If you'd spelled one of them
with a lowercase s, so I had another typo,

56
00:04:40,560 --> 00:04:44,700
they wouldn't compare equal and your
program wouldn't work as expected.

57
00:04:44,700 --> 00:04:49,280
Before I finish the video, take a closer look
at line 5 in our code.

58
00:04:49,280 --> 00:04:53,980
Can you tell by reading that line, what the true intention, or what I wanted that line to do?

59
00:04:53,980 --> 00:04:58,700
It's ambiguous to read, so we know how
Python will interpret it because the

60
00:04:58,700 --> 00:05:01,790
precedents of the operators is
well-documented, and you can find the

61
00:05:01,790 --> 00:05:06,000
table in the documentation in a link in
the resources section of this video.

62
00:05:06,000 --> 00:05:11,320
But I can also bring it up on screen, which
I'll do now.

63
00:05:11,320 --> 00:05:16,820
So there's the table. Now we discussed operator precedence for the arithmetic operators in the previous section,

64
00:05:16,820 --> 00:05:20,600
but boolean, or conditional
operators also have precedence and you

65
00:05:20,600 --> 00:05:25,280
can see the or and not there, talking
about boolean. So we can see here that and

66
00:05:25,280 --> 00:05:30,380
has a higher precedence than or. Now
that's why we go swimming even though it's raining,

67
00:05:30,380 --> 00:05:35,600
looking back at the code again.
Raining is True, as you can see on line 3.

68
00:05:35,600 --> 00:05:39,340
So is that a bug, or is that what
I intended? The point of this is that

69
00:05:39,340 --> 00:05:43,300
that line of code doesn't really give
you any indication of what I intended.

70
00:05:43,300 --> 00:05:48,140
Even if they aren't necessary, we should
use parentheses to make it clear.

71
00:05:48,140 --> 00:05:52,280
So let's change the day to Friday to see what happens.

72
00:05:52,580 --> 00:06:00,820
Capital F there. We run the program. We
can see Learn Python, outputted at the bottom of the screen.

73
00:06:00,820 --> 00:06:05,480
The condition day
is equal to Saturday, evaluates to False.

74
00:06:05,480 --> 00:06:09,300
The condition temperature greater than
27 evaluates to True,

75
00:06:09,300 --> 00:06:15,780
but False and True is False, as we established in previous videos. Not raining is also False,

76
00:06:15,780 --> 00:06:19,870
which means the code in the else
block is executed. So what would happen

77
00:06:19,870 --> 00:06:26,440
here if we changed raining to False.
Let's do that.

78
00:06:26,440 --> 00:06:28,640
Run the code.

79
00:06:28,640 --> 00:06:33,760
This time we go swimming. As I said, you don't have any
indication if that's what was intended

80
00:06:33,760 --> 00:06:37,640
Maybe I want to go swimming on hot
Saturdays or on any other day

81
00:06:37,640 --> 00:06:41,960
that's not raining. So let's add some
parentheses here, to make it clear

82
00:06:41,960 --> 00:06:45,880
how this code is being interpreted. So I'm
going to put a parenthesis in front,

83
00:06:45,880 --> 00:06:51,180
a left parenthesis in front of day, and the
other right parenthesis after

84
00:06:51,180 --> 00:06:57,320
temperature greater than 27, over here.
Now this is how the code's interpreted at the moment,

85
00:06:57,320 --> 00:07:01,990
because and has a higher
precedence than or. So even if that's

86
00:07:01,990 --> 00:07:05,280
what I meant,
then I should still use the parentheses.

87
00:07:05,280 --> 00:07:09,180
They're not needed by Python but they
make the code much easier to read.

88
00:07:09,180 --> 00:07:13,080
Looking at this code now, it's now clear
that we'll go swimming on any day

89
00:07:13,080 --> 00:07:17,800
when it's not raining, as well as on hot
Saturdays, whether it's raining or not.

90
00:07:17,800 --> 00:07:23,520
As a general rule, always use parentheses when you mix and and or in the same condition.

91
00:07:23,520 --> 00:07:26,900
Doing it makes it clear what you intended. More importantly,

92
00:07:26,900 --> 00:07:29,520
it makes it less likely that you'll
introduce a bug.

93
00:07:29,520 --> 00:07:32,820
When you use parentheses, you don't have to worry about remembering

94
00:07:32,820 --> 00:07:35,980
whether and has a higher or lower  precedence than or.

95
00:07:35,980 --> 00:07:40,200
Alright, so try different values for those three variables,
and then have a go at creating your own

96
00:07:40,200 --> 00:07:44,760
boolean expressions. In the next video,
we're gonna have a quick look at some truthy values.

97
00:07:44,760 --> 00:07:47,920
See you in the next video.

