WEBVTT

0
00:00.110 --> 00:04.670
Now, up to this point you've seen us use if statements,

1
00:04.700 --> 00:10.730
else statements, elif statements, multiple if statements as well as nested if statements,

2
00:10.730 --> 00:19.340
but the thing that we haven't been able to do thus far is to check for multiple conditions in the same

3
00:19.340 --> 00:20.600
line of code.

4
00:21.020 --> 00:28.310
So how would we be able to combine different conditions and say, well, is the pizza large, and the

5
00:28.310 --> 00:33.770
user wants pepperoni, and extra cheese all in the same line of code.

6
00:33.830 --> 00:38.030
Well, to do this, we would need to learn about the logical operators.

7
00:38.030 --> 00:42.380
So there's three of them that are really useful: "and", "or", and "not".

8
00:42.920 --> 00:44.990
So let's take a look at the first one.

9
00:44.990 --> 00:52.190
When you combine two different conditions using an and operator they both have to be True both A and

10
00:52.190 --> 00:55.580
B for the entire line of code to be True.

11
00:56.060 --> 01:02.390
If just one of them is True, say A is True and B is False, or A is False and B is True, then the

12
01:02.390 --> 01:04.940
overall thing evaluates to False.

13
01:05.570 --> 01:08.990
Now let's try out some of that in actual code.

14
01:08.990 --> 01:14.600
So let's go ahead and go into the Python Console that we see, right here.

15
01:14.600 --> 01:18.740
And here we can write Python code from scratch,

16
01:18.740 --> 01:24.170
so completely independent of everything else and independent of our code files.

17
01:24.170 --> 01:26.780
So let's test out some Python code in here.

18
01:26.780 --> 01:30.470
Let's say that, a = 12.

19
01:30.470 --> 01:38.630
Right? Now, let's say that I wanted to check whether if is a &gt; 15?

20
01:38.990 --> 01:41.030
Well, that is going to be False.

21
01:41.030 --> 01:45.530
Now what about is a &gt; 10?

22
01:45.560 --> 01:46.910
Well, that's going to be True.

23
01:46.940 --> 01:57.950
Now, if I combined this using an "and" statement, I could say, well, is a &gt; 10 and a &lt; 13

24
01:57.950 --> 02:07.400
and hit Enter, then I would get, True, because both a is greater than ten and a is less

25
02:07.400 --> 02:09.020
than 13 are True.

26
02:09.050 --> 02:15.890
So in this case, when both conditions are true, then this entire line gets evaluated to True.

27
02:15.920 --> 02:19.400
But see what happens when just one of them is False.

28
02:19.400 --> 02:24.620
So is a &gt; 15 and a &lt; 13,

29
02:25.280 --> 02:29.030
a is greater than 15 is False, a is less than 13 is True,

30
02:29.030 --> 02:31.010
and now we get False

31
02:31.010 --> 02:31.460
.

32
02:31.610 --> 02:35.990
So that's what happens when you combine different conditions using and.

33
02:36.500 --> 02:41.780
So using the "and" logical operator it looks at two conditions,

34
02:41.780 --> 02:44.240
and to see if they are both True.

35
02:44.240 --> 02:49.010
And if they are both True it becomes True as the final outcome.

36
02:49.010 --> 02:54.830
But if either of these are False, so False and True becomes False.

37
02:54.830 --> 02:58.610
True and False also becomes False.

38
02:58.700 --> 03:03.020
And this is how the and logical operator works.

39
03:03.020 --> 03:10.010
Now if you only needed one of the conditions to be True, then you could use the "or" operator instead.

40
03:10.010 --> 03:17.450
So if C or D were True, or if they're both True, then it will evaluate to True.

41
03:17.870 --> 03:24.740
It's only when both C and D are False, does this statement actually become False.

42
03:25.370 --> 03:28.910
Now the final one is the "not" operator.

43
03:28.910 --> 03:33.500
And all that this does is it basically reverses a condition.

44
03:33.500 --> 03:36.680
So if the condition is False, then it becomes True.

45
03:36.680 --> 03:39.110
If it's True, then it becomes False.

46
03:40.070 --> 03:47.150
So coming back to our code, we can refresh and restart the Python Console and start with the same a = 12

47
03:47.150 --> 03:47.900
.

48
03:47.900 --> 03:54.860
And we can use the or statement by saying, well, is a &gt; 10.

49
03:54.860 --> 03:55.940
That's True,

50
03:56.180 --> 04:02.420
or is a &lt; 10, which is not True.

51
04:02.420 --> 04:07.400
So here we have a condition that is True and a condition that's not True,

52
04:07.400 --> 04:13.130
but because this time instead of using an and, we have an or, between these two conditions, then this

53
04:13.130 --> 04:15.110
will actually evaluate to True.

54
04:15.140 --> 04:22.340
So in this case, using the or True or True will become True.

55
04:22.430 --> 04:26.090
True or False will become True.

56
04:26.090 --> 04:34.790
False or True becomes true and it's only False or False when both of the conditions are False does it

57
04:34.790 --> 04:36.320
actually evaluate to False.

58
04:36.320 --> 04:39.260
So this is quite different from the "and" operator.

59
04:39.290 --> 04:47.840
Now finally with the "not" operator, it only works when the condition that's being checked is False.

60
04:47.840 --> 04:52.550
So effectively it flips the True to the False and False to True.

61
04:52.580 --> 04:53.960
Let me show you what I mean.

62
04:53.960 --> 05:00.470
So we know that a is 12, so is a &lt; 0?

63
05:00.470 --> 05:01.940
Well it's not because it's 12.

64
05:01.940 --> 05:03.590
It's definitely not less than zero.

65
05:03.590 --> 05:08.120
But by putting the not in front of it, it reverses the condition.

66
05:08.120 --> 05:10.010
So a &lt; 0,

67
05:10.040 --> 05:14.870
this will evaluate to False, but putting the not in front of it it becomes True.

68
05:14.870 --> 05:20.660
So not False is True, and not True is False.

69
05:20.690 --> 05:26.990
Coming back to our roller coaster ticketing, let's say that the roller coaster company decided that

70
05:26.990 --> 05:32.060
for everybody who is having a midlife crisis, they would give them free tickets.

71
05:32.060 --> 05:39.500
And according to Wikipedia, midlife crises typically occur when you are 45 to 55 years old.

72
05:39.800 --> 05:43.940
Let's see if we can incorporate this into our code.

73
05:44.000 --> 05:51.650
So let's say that in addition to these existing price categories, what if you had to add a separate

74
05:51.650 --> 05:59.930
price category for those people who are aged between 45 and 55, and those people get to ride for free?

75
06:00.800 --> 06:05.960
Do you think you would be able to change the code using what you've learned about logical operators,

76
06:05.960 --> 06:09.680
in order to incorporate this addition to our program?

77
06:09.830 --> 06:11.720
Pause the video and give that a go.

78
06:15.080 --> 06:18.470
So we've currently got three conditions:

79
06:18.710 --> 06:20.300
age less than 12,

80
06:20.330 --> 06:22.640
age between 12 and 18,

81
06:22.640 --> 06:24.710
and finally everybody else.

82
06:24.740 --> 06:32.240
Now instead of just finishing up there, let's go ahead and add another elif.

83
06:32.240 --> 06:35.270
And here we're going to combine two conditions.

84
06:35.270 --> 06:47.510
We're going to say if the age &gt;= 45 and age &lt;= 55,

85
06:47.540 --> 06:51.110
then we get to catch that midlife crisis window.

86
06:51.890 --> 06:57.650
Well, in this case, we're going to print something like...we're going to say everything's going to be

87
06:57.650 --> 07:00.080
okay, have a free ride on us.

88
07:01.580 --> 07:10.370
And in fact, we don't need to modify the bill in any way because we know that with if, elif, and else

89
07:10.370 --> 07:19.640
statements is that once this condition matches, then everything that's inside this block.

90
07:19.640 --> 07:25.880
So everything that's indented inside this elif is going to be carried out, namely printing this out,

91
07:25.880 --> 07:33.140
and then it's going to skip the rest of this if else block and continue on.

92
07:33.800 --> 07:39.260
Now, if they want a photo, they still have to pay $3, but at least their ticket is free.

93
07:39.260 --> 07:44.690
And we've been able to do this because we know about the "and" logical operator.

94
07:45.410 --> 07:52.460
Now, for the keen eyed amongst you, you might have noticed with this condition check, we have a warning

95
07:52.460 --> 07:53.630
underneath it.

96
07:53.750 --> 08:01.460
And this says that there's actually a simpler way of writing this expression, and it tells us that

97
08:01.460 --> 08:10.760
we can actually simplify it. Now because we're checking two conditions and we have the "and" logical operator

98
08:10.760 --> 08:11.750
in between,

99
08:11.750 --> 08:15.860
sometimes you might want to make this code less wordy.

100
08:15.860 --> 08:22.910
So if you hover over this warning and you click on Simplified chained comparison, it will give you a

101
08:22.910 --> 08:31.430
simpler way of doing this comparison using two of these checks, either side of the age,

102
08:31.430 --> 08:36.440
and it does exactly the same thing, but in less words of code.

103
08:36.440 --> 08:46.490
But here is the big but, looking at this, is this easier to understand and logic through versus this?

104
08:46.490 --> 08:53.330
Because to somebody who's just starting Python programming, I think this is easier to understand

105
08:53.330 --> 08:56.840
what's going on. Is age greater or equal to 45,

106
08:56.840 --> 09:00.290
and is age less than or equal to 55?

107
09:00.920 --> 09:07.310
So even though there is this warning in here, I'm going to leave it in this format because a lot of

108
09:07.310 --> 09:13.100
you are just starting out learning how to code, and this is a much easier way of reasoning about what's

109
09:13.100 --> 09:15.140
going on in this line of code.

110
09:15.140 --> 09:20.810
So the thing with warnings are that they're really helpful once you get going as a programmer,

111
09:20.810 --> 09:25.130
but in the learning stage, I wouldn't worry too much about all of it.

112
09:25.130 --> 09:30.470
Look at it, read it, see if you understand what it's trying to tell you, and then you decide which

113
09:30.470 --> 09:32.120
version you prefer.