1
00:00:05,510 --> 00:00:10,930
Alright, so for the first challenge solution, we're going to use the conditional expression that gives

2
00:00:10,940 --> 00:00:14,840
the results we want for each of the values from 1 to 30.

3
00:00:14,840 --> 00:00:20,220
Now the line I'm about to type is a bit long, but bear with me as we start with it anyway.

4
00:00:20,230 --> 00:00:31,770
So I'm going to start with fizzbuzz equals, left and right square brackets, double quotes fizz buzz if x modulus 

5
00:00:31,770 --> 00:00:44,970
operator 15, two equal signs because it's equal to zero, else double quotes fizz, if x modulus three equal equal zero, else buzz double quotes,

6
00:00:45,920 --> 00:01:02,050
if x modulus 5 equals zero, else str x for x in range parentheses, one comma 31.

7
00:01:02,480 --> 00:01:07,600
So that line really is a bit long, and we should really split it up to stay consistent with PEP guidelines.

8
00:01:07,980 --> 00:01:12,890
If we have a look over to the right hand corner here, or right hand side rather, PEP 8 line too long, 125

9
00:01:12,950 --> 00:01:17,620
greater than 120 characters, so probably make sense to split it about there.

10
00:01:18,880 --> 00:01:20,420
And that makes it a bit more readable.

11
00:01:20,450 --> 00:01:24,710
Right so then we'll print on the next line, fizzbuzz.

12
00:01:25,380 --> 00:01:31,250
Now here before I actually run this, you might be wondering why I'm using the function string instead

13
00:01:31,250 --> 00:01:37,850
of just printing x, because we're printing the results and we won't be doing any further processing, which means

14
00:01:37,850 --> 00:01:38,990
that either would do here.

15
00:01:39,260 --> 00:01:44,860
But remember that a list comprehension produces a list and the lists are intended to be homogenous,

16
00:01:44,930 --> 00:01:48,860
in other words, they're intended to store items of the same type.

17
00:01:48,860 --> 00:01:54,720
Now that's not enforced by Python, and you can mix all sorts of types in a list and sometimes you will

18
00:01:54,720 --> 00:02:01,020
want to do that, but it can cause problems if you're not careful when you come to use the list.

19
00:02:01,100 --> 00:02:04,290
So let's actually run this just to confirm now that it does work,

20
00:02:05,370 --> 00:02:07,120
and you can see in the list there the results.

21
00:02:09,919 --> 00:02:13,050
It looks pretty good to me, it looks like it's working nicely.

22
00:02:13,290 --> 00:02:23,230
All the fifteens, threes and fives have got fizz, buzz or fizzbuzz, and we've got the numbers for the rest of them. OK.

23
00:02:23,230 --> 00:02:26,970
Now you'll often want to process the list in some way,

24
00:02:27,340 --> 00:02:32,000
so we'll use the centre method of strings now to display the results a bit differently.

25
00:02:32,080 --> 00:02:37,840
So what I'm going to do is comment out this initial for loop here, the one that you already had before the challenge,

26
00:02:38,950 --> 00:02:51,070
and instead what I'm going to do on line 21, is type for buzz in fizzbuzz, colon print parentheses buzz dot

27
00:02:51,070 --> 00:02:58,340
centre, parentheses 12 comma space two single quotes and the asterix.

28
00:02:59,350 --> 00:03:00,070
And if we run that,

29
00:03:03,610 --> 00:03:10,390
you can see now we've got the results printing out in a width of 12, padded by asterisks on each side. It makes

30
00:03:10,390 --> 00:03:14,400
it a bit easier to actually review the output, to make sure that it's working correctly.

31
00:03:16,390 --> 00:03:17,130
Now by the way,

32
00:03:17,140 --> 00:03:22,200
the reason I'm showing you that is because that works fine, only because we made sure that all 

33
00:03:22,200 --> 00:03:25,210
items in the list are of the same type - a string.

34
00:03:25,410 --> 00:03:27,280
Now if we didn't do that we'd get an error.

35
00:03:27,510 --> 00:03:33,790
So if we change the else over here, from string x to x, and run this again,

36
00:03:35,730 --> 00:03:36,900
we actually get an error.

37
00:03:37,350 --> 00:03:40,590
That's because ints of course don't have a centre function.

38
00:03:40,650 --> 00:03:43,020
It is a slightly contrived example.

39
00:03:43,020 --> 00:03:48,120
The point though, is you may want to do things with the items in your lists, and it's a lot easier if

40
00:03:48,120 --> 00:03:51,330
all items are the same type in that list.

41
00:03:51,330 --> 00:03:54,730
Now I'm not saying by any means that you should never mix types in a list.

42
00:03:54,800 --> 00:03:56,720
If it wasn't a useful thing to do,

43
00:03:56,850 --> 00:04:03,150
Python probably wouldn't let you do it. But if you can keep your lists homogenous, then you'll get fewer problems

44
00:04:03,180 --> 00:04:07,820
when you come to use them for things that you hadn't originally planned to use them for.

45
00:04:08,190 --> 00:04:16,110
Alright so let's close that run window down, and what I'll do is I'll undo those changes, and what I might do is actually

46
00:04:16,170 --> 00:04:16,740
end the video

47
00:04:16,750 --> 00:04:21,220
here now, because you've got Challenge 1 completed successfully. In the next video,

48
00:04:21,230 --> 00:04:26,210
what we're going to do is start tackling Challenge 2 - this one here. So I'll

49
00:04:26,280 --> 00:04:27,780
see you in the next video.

