1
00:00:05,550 --> 00:00:09,920
Now we had a problem in the last video, when we tried to use else in our comprehension.

2
00:00:10,130 --> 00:00:12,070
So let's start by reviewing the problem.

3
00:00:12,080 --> 00:00:18,050
Now what I've done is I've opened a new file here, condcomp1.py, or created it, and I've copied the code from

4
00:00:18,050 --> 00:00:25,020
our original condcomp.py that we used in the last video, and I've deleted all the code after the for loop

5
00:00:25,020 --> 00:00:26,640
and after we printed out the meals.

6
00:00:26,780 --> 00:00:28,850
So basically, starting from scratch there again.

7
00:00:29,240 --> 00:00:30,770
So if I run this file now,

8
00:00:34,040 --> 00:00:39,230
you can see that the for loop creates a list containing just the two meals that don't contain spam from

9
00:00:39,230 --> 00:00:41,990
the original list, and if a meal isn't included,

10
00:00:42,020 --> 00:00:45,370
we're getting the string 'a meal was skipped' instead.

11
00:00:45,470 --> 00:00:50,950
So the first part, getting a list of the meals that doesn't contain spam, is easy as a comprehension.

12
00:00:51,250 --> 00:00:53,330
So let's start that first, so on line

13
00:00:53,630 --> 00:01:01,440
twenty one we'll put meals equals, left and right square brackets, meal for meal in menu,

14
00:01:01,900 --> 00:01:07,180
if double quotes spam not in meal.

15
00:01:07,530 --> 00:01:12,250
We've got our right square bracket there, and we can print out meals.

16
00:01:12,530 --> 00:01:15,530
Let's just confirm that that works.

17
00:01:15,860 --> 00:01:17,650
And you can see that that works.

18
00:01:17,720 --> 00:01:21,380
So I'm getting the same two, basically the same two meals as the for loop,

19
00:01:21,500 --> 00:01:26,230
but of course we're not getting the 'meal was skipped' messages in that list. So that bit was easy,

20
00:01:26,310 --> 00:01:31,910
but at this point we're now stuck, because we've already seen in the previous video that we can't add an else

21
00:01:31,910 --> 00:01:33,200
clause to the filter.

22
00:01:33,500 --> 00:01:35,400
So how can we actually proceed here.

23
00:01:35,780 --> 00:01:41,180
Well the expression that we're using to build the list can be as complicated as we need. Now at the

24
00:01:41,180 --> 00:01:41,860
moment,

25
00:01:41,880 --> 00:01:44,420
we're just adding the individual items for menu -

26
00:01:44,420 --> 00:01:48,970
the expression part in the meal, in the variable meal, at the start of the comprehension.

27
00:01:49,190 --> 00:01:54,920
Now if we instead, replace the simple expressions with something a bit more complicated, we can then get our

28
00:01:54,920 --> 00:01:59,110
comprehension to produce exactly the same list as the for loop.

29
00:01:59,720 --> 00:02:03,920
So a conditional expression is pretty much what it sounds like.

30
00:02:03,920 --> 00:02:10,180
So it's a condition that we can assign to something or use anywhere else that an expression's needed.

31
00:02:10,190 --> 00:02:11,530
So let's see one in action.

32
00:02:11,570 --> 00:02:17,330
So we're going to add some code down here on line 24, x equals 12,

33
00:02:17,960 --> 00:02:24,170
and on the next line, expression equals double quotes, twelve with a capital T, although it doesn't really

34
00:02:24,170 --> 00:02:25,050
matter,

35
00:02:25,050 --> 00:02:32,920
space if x equals 12, two equals there. Now we've actually got an error on that line at the moment.

36
00:02:33,140 --> 00:02:36,230
So what we've typed is the first part of the expression with an error there.

37
00:02:36,560 --> 00:02:39,510
And if you think about it, hopefully it's obvious why we've got that error -

38
00:02:39,700 --> 00:02:40,960
this is on this line.

39
00:02:41,290 --> 00:02:47,400
Now expression will be equal to the string 12 in double quotes if x equals 12,

40
00:02:47,660 --> 00:02:49,940
but what value will it have otherwise.

41
00:02:49,970 --> 00:02:53,600
So when using a conditional expression there must be an else

42
00:02:53,600 --> 00:02:54,240
part.

43
00:02:54,320 --> 00:02:57,910
So let's revise that, and add on the end of it else

44
00:02:58,040 --> 00:03:07,460
double quotes unknown, then on the next line we can do print expression. If we run this now, there shouldn't

45
00:03:07,460 --> 00:03:09,130
be any surprises.

46
00:03:09,290 --> 00:03:14,550
And you can see that we're getting the output 12, because you can clearly see that x is defined as 12,

47
00:03:14,780 --> 00:03:17,560
and the if clause there is basically true,

48
00:03:17,630 --> 00:03:24,350
so therefore we're getting the result 12. And just to really confirm that, lets change it to something else, to 15, run it

49
00:03:24,350 --> 00:03:27,160
again, and we've got unknown.

50
00:03:27,190 --> 00:03:29,470
So that's working like we think it should.

51
00:03:29,470 --> 00:03:31,730
So now that we know what conditional expressions are,

52
00:03:32,080 --> 00:03:35,060
let's see how one can help us with what we're trying to achieve here.

53
00:03:35,350 --> 00:03:40,810
So what we're trying to do, is we want an expression that evaluates to the meal if the meal doesn't

54
00:03:40,810 --> 00:03:44,400
contain spam, otherwise it evaluates to the string

55
00:03:44,410 --> 00:03:45,890
a meal was skipped.

56
00:03:46,030 --> 00:03:50,560
So how do we do that? Well we go back and, lets, we go back and close the run window, and we're going to modify line

57
00:03:50,580 --> 00:03:55,880
21, so it should be now meal equals, and we've got double quotes meal.

58
00:03:56,190 --> 00:04:00,420
And what I'm going to do is, just to make it easy, I'm going to type it, so you can see me type the whole thing in

59
00:04:00,420 --> 00:04:11,530
again. So, meal equals and we've got our double quotes meal, if double quotes spam not in meal, else a meal was skipped,

60
00:04:14,480 --> 00:04:19,800
for meal in menu, and we'll leave the print meals there.

61
00:04:20,029 --> 00:04:25,730
So lets actually run this, confirm that it works, and you can see that we're basically getting the identical

62
00:04:26,180 --> 00:04:31,270
list to what was outputted for us on the previous lines, with a meal skipped,

63
00:04:31,360 --> 00:04:36,740
eggs, sausages, bacon etc. So we're basically getting everything. We're getting the if and the else condition being

64
00:04:36,740 --> 00:04:37,840
evaluated.

65
00:04:37,930 --> 00:04:45,140
And just in case this isn't clear, what we've done is remove the filter and we've put the condition into

66
00:04:45,140 --> 00:04:48,870
a conditional expression in the comprehension.

67
00:04:49,010 --> 00:04:53,890
So in this slide you can see that this is just like our first list comprehension,

68
00:04:54,020 --> 00:04:57,940
but now the expression part's a bit more complex. Now

69
00:04:57,980 --> 00:05:00,280
the expression can be as complex as you need,

70
00:05:00,530 --> 00:05:08,300
although you can also use a function to avoid excessively complex code. Alright so back to IntelliJ again.

71
00:05:08,990 --> 00:05:12,770
So conditional expressions can be more complicated than this.

72
00:05:12,830 --> 00:05:18,890
Now whenever there's an else, it can be followed by another if, whenever there's an if part that must

73
00:05:18,890 --> 00:05:20,380
be followed by an else.

74
00:05:20,390 --> 00:05:25,460
So what I'm going to do is comment out all the code other than the definition. I'm just going to just close the run

75
00:05:25,460 --> 00:05:26,510
window down.

76
00:05:26,780 --> 00:05:33,620
So basically, comment out all this code from line 13, right down to and including line 26.

77
00:05:33,740 --> 00:05:41,210
I'm going to start again on line 28, and let's try this; for meal in menu,

78
00:05:41,600 --> 00:05:44,570
colon, then on the indented line, on the next line,

79
00:05:44,660 --> 00:05:54,100
print parentheses meal comma space double quotes, contains chicken, double quote,

80
00:05:54,180 --> 00:06:08,110
if double quote chicken double quote in meal, else contains bacon, if double quotes bacon in meal,

81
00:06:09,120 --> 00:06:13,970
else contains egg.

82
00:06:14,230 --> 00:06:21,080
Then we've got our right parentheses. So lets actually try running that to get an indication of what each meal

83
00:06:21,080 --> 00:06:26,250
contains.

84
00:06:26,480 --> 00:06:30,580
So note here that the order is important when creating the conditional expression.

85
00:06:30,810 --> 00:06:34,550
So the first two meals don't have a message that they contain egg,

86
00:06:34,850 --> 00:06:38,140
and the reason for that is in the expression which we can't quite see

87
00:06:38,140 --> 00:06:39,320
now - that's better.

88
00:06:39,460 --> 00:06:43,330
We're actually checking for bacon before checking for egg. Now

89
00:06:43,360 --> 00:06:45,550
only if there's no chicken and no bacon

90
00:06:45,620 --> 00:06:48,180
do we get that 'contains egg' message.

91
00:06:48,300 --> 00:06:53,280
Now that example was to show how to include several conditions in an expression.

92
00:06:53,310 --> 00:06:57,350
Now you really wouldn't perform the test that way and it's also wrong.

93
00:06:57,380 --> 00:07:03,350
Now we can see that, by changing chicken to sausage. So I'll go through to our expression

94
00:07:03,370 --> 00:07:04,380
and change that to sausage.

95
00:07:10,960 --> 00:07:12,100
OK and we'll run that,

96
00:07:15,090 --> 00:07:21,550
and you can see that it's actually showing our chicken and chips meal down the bottom, as containing egg.

97
00:07:21,640 --> 00:07:27,170
Now containing egg is the default and it's going to be printed if none of the other conditions match.

98
00:07:27,170 --> 00:07:31,150
Now as I said, you probably wouldn't perform this particular test in that way.

99
00:07:31,420 --> 00:07:33,490
Now exactly how you would write the code

100
00:07:33,490 --> 00:07:37,530
depends on what you're trying to do, and the requirements here are a bit vague.

101
00:07:37,600 --> 00:07:41,080
It was just really an example of a more complex conditional expression.

102
00:07:41,700 --> 00:07:45,180
So you'd probably want to test for membership in a list or a set instead.

103
00:07:45,430 --> 00:07:50,710
So first we can create a set of all the items that our meals can contain, and I'll actually print a blank line

104
00:07:50,710 --> 00:07:55,250
to separate this output from the preceeding results, so lets go ahead and do that. Close down the run window

105
00:07:55,290 --> 00:07:56,420
for now.

106
00:07:57,300 --> 00:07:59,780
So I'll just do a print first, an empty line.

107
00:08:00,200 --> 00:08:07,930
So we'll start with items equals set parentheses, then for meal in menu on the next line, colon, for

108
00:08:07,930 --> 00:08:14,950
item in meal colon, items dot add parentheses item.

109
00:08:15,200 --> 00:08:24,400
Then we're going to go back and print items print, just to make another space there, so print parentheses.

110
00:08:25,290 --> 00:08:26,560
So I'll run the program.

111
00:08:26,980 --> 00:08:29,750
And I'll just put a space there to keep Python happy,

112
00:08:29,770 --> 00:08:31,660
run.

113
00:08:31,690 --> 00:08:35,570
So we've now got a set of all items that could appear in the meals.

114
00:08:35,710 --> 00:08:40,990
So now that we've got that, what we can do instead is loop through the meals and print the first matching

115
00:08:40,990 --> 00:08:43,740
item that each meal contains. So lets do that.

116
00:08:47,900 --> 00:08:53,130
So on line 44, meal in menu colon, for item in menu,

117
00:08:53,320 --> 00:08:56,900
Sorry, for item in items I should say.

118
00:08:56,910 --> 00:09:08,620
If item in meal colon print parentheses left and right curly braces in double quotes, contains, and

119
00:09:08,650 --> 00:09:09,780
another left and right curly

120
00:09:09,820 --> 00:09:14,730
braces double quote dot format, meal

121
00:09:14,830 --> 00:09:21,880
comma space item, two right parentheses and then we'll do a break. Alright so now we can loop through

122
00:09:21,880 --> 00:09:28,890
the meals and print the first matching item that each meal contains. So lets try running that, and you

123
00:09:28,890 --> 00:09:34,410
can see we've got a much better result this time. So conditional expressions can be very useful but don't

124
00:09:34,410 --> 00:09:36,320
try to use one just because you can.

125
00:09:36,360 --> 00:09:39,750
They really should only be used to solve specific problems.

126
00:09:39,930 --> 00:09:41,340
So as another example,

127
00:09:41,340 --> 00:09:43,810
let's get the computer to play fizzbuzz.

128
00:09:43,830 --> 00:09:45,310
Now that's a very simple game.

129
00:09:45,480 --> 00:09:51,010
If a number is divisible by three you say fizz instead of the number, if it's divisible by five you say

130
00:09:51,030 --> 00:09:56,100
buzz, and if it's divisible by both 3 and 5 you say fizzbuzz -

131
00:09:56,130 --> 00:09:59,580
so if it's divisible by 15, in other words. Otherwise you say the number.

132
00:09:59,850 --> 00:10:05,940
So let's have a look how to code that expression, and we'll put that into a loop to get a range of results,

133
00:10:06,010 --> 00:10:07,440
or so we get a range of results.

134
00:10:07,530 --> 00:10:16,790
So I'm going to start typing here, for x in range, parentheses, we go from 1 to 31 colon. So

135
00:10:16,850 --> 00:10:28,470
fizzbuzz equals fizz buzz in double quotes, space if x modulus, so the percent sign, 15 is

136
00:10:28,470 --> 00:10:30,160
equal to zero, two equals,

137
00:10:30,390 --> 00:10:44,730
else double quotes fizz if x modulus again percent three equals zero, else double quotes buzz if x

138
00:10:45,480 --> 00:10:50,500
modulus five - that's the percent sign again - equals zero, else,

139
00:10:50,600 --> 00:11:01,780
str x. Then on the next line we're going to print fizzbuzz. So remember that x modulus 15 will be

140
00:11:01,860 --> 00:11:09,110
0 if X is divisible by 15 and the same for x modulus 3 and x modulus 5.

141
00:11:09,130 --> 00:11:13,030
Now the order's important here. We have to check 15 fist,

142
00:11:13,210 --> 00:11:15,880
otherwise one of the other conditions will be true.

143
00:11:16,220 --> 00:11:17,070
Now let's run the program

144
00:11:20,050 --> 00:11:21,580
and confirm that it actually works.

145
00:11:26,640 --> 00:11:32,040
So you can see we get the number or buzz word printed out for values from 1 to 30 inclusive,

146
00:11:33,420 --> 00:11:34,470
and that looks pretty good to me.

147
00:11:36,940 --> 00:11:40,110
Alright, so I'm going to finish the video here. In the next one,

148
00:11:40,160 --> 00:11:44,840
we've got some challenges to practice what we've done so far, and you can probably guess what the first one's

149
00:11:44,860 --> 00:11:45,690
going to be.

150
00:11:45,690 --> 00:11:47,050
So I'll see you in the next video.

