1
00:00:04,910 --> 00:00:09,180
Continuing with tuples_intro.py,
it would be useful to store

2
00:00:09,180 --> 00:00:13,799
all our albums in a single list. Now if
we're going to do that, we must enclose

3
00:00:13,799 --> 00:00:18,780
each one in parentheses. I suggested
always using parentheses around your

4
00:00:18,780 --> 00:00:24,640
tuples, and you can see that I didn't do
that in the five tuples on lines 1 to 5.

5
00:00:24,640 --> 00:00:28,640
What we'll do first is create the
list without any parentheses, to show you

6
00:00:28,650 --> 00:00:33,360
why they're necessary. So I'm going to go
ahead and change this to; welcome's

7
00:00:33,360 --> 00:00:39,120
gonna be changed to albums. I'm
going to put our left square bracket there.

8
00:00:39,120 --> 00:00:47,820
I'm going to put a comma at the end of
each line. We're going to put our right

9
00:00:47,820 --> 00:00:52,980
square bracket at the start of line six and
then I'm going to delete the variable

10
00:00:52,980 --> 00:01:01,700
definitions at the start - each one of these, like so.

11
00:01:01,700 --> 00:01:04,640
I'm going to delete all the code
after that closing brace, the right

12
00:01:04,650 --> 00:01:08,910
square bracket. That's only going to, that
was referring to variables that we don't

13
00:01:08,910 --> 00:01:12,660
have anymore and was showing an
error. Alright, so what we've got now

14
00:01:12,660 --> 00:01:18,630
looks like a list containing five albums.
It doesn't conform to the Python style

15
00:01:18,630 --> 00:01:22,680
guide, so what we should do is reformat
the code before going any further. So I'm

16
00:01:22,680 --> 00:01:27,860
gonna come up to code and select Reformat Code.
You can use the shortcut if you prefer.

17
00:01:27,860 --> 00:01:31,860
Now that I've done that, it looks even
more like a list containing five albums,

18
00:01:31,860 --> 00:01:39,820
but in fact, it's not. What it is, is a list
containing 15 items, 10 strings and 5 ints.

19
00:01:39,820 --> 00:01:42,810
Just adding line breaks
doesn't make each group of entries into

20
00:01:42,810 --> 00:01:48,510
a tuple. We need to use parentheses to do
that. Now we can check, by printing out

21
00:01:48,510 --> 00:01:51,340
how many items are on the list. So let's do that.

22
00:01:51,340 --> 00:01:59,100
On line 8,  I'm going to type print
parentheses len parentheses albums.

23
00:01:59,100 --> 00:02:03,720
Run the program and the answer will be 15, as
you can see on the screen there. When we

24
00:02:03,720 --> 00:02:06,990
use parentheses around the individual
album details, we will get a list

25
00:02:06,990 --> 00:02:11,380
containing five entries. So let's have a go at
doing that. So we're gonna each one of

26
00:02:11,380 --> 00:02:16,180
these groups here, up to but not including the
furthermost right comma,

27
00:02:16,180 --> 00:02:19,520
we're gonna surround in parentheses.

28
00:02:36,200 --> 00:02:40,920
Alright, like so. So now that I've done
that, at the end of each line, the comma

29
00:02:40,920 --> 00:02:44,880
separates the tuple from the next one.
So do make sure you put the comma

30
00:02:44,880 --> 00:02:49,170
after the closing parenthesis, as I did, or
you'll get an error. So run the program -

31
00:02:49,170 --> 00:02:55,060
this time we should see the answer 5,
instead of 15, and you can see that's the case.

32
00:02:55,060 --> 00:03:00,360
Our albums list now contains five
tuples, and each of those tuples contains

33
00:03:00,360 --> 00:03:05,120
three items. Now if we want to print out
a catalogue of all our albums,

34
00:03:05,120 --> 00:03:10,240
all we need to do is iterate over the
list. Let's have a go at doing that.

35
00:03:10,240 --> 00:03:20,040
So on line 10, I'm going to type for album
in albums : and on the next line, print

36
00:03:20,040 --> 00:03:28,880
parentheses double quotes Album : Then
we want left and right curly braces comma

37
00:03:28,880 --> 00:03:35,480
Artist : left and right curly braces again
comma Year :

38
00:03:35,480 --> 00:03:39,660
left and right curly braces, closing double
quote. On the next line, I'm going to

39
00:03:39,660 --> 00:03:52,980
type dot format and it's going to be, in
parentheses, album 0 album 1, in square

40
00:03:52,980 --> 00:04:01,440
brackets again, and finally, album 2 in
square brackets, and we've got our two

41
00:04:01,440 --> 00:04:07,049
right parentheses as well. And I split
line 11 up, just to make it easier to see

42
00:04:07,049 --> 00:04:12,540
on the screen. You don't need to do that.
Run the program, and we should see our

43
00:04:12,540 --> 00:04:16,168
tuples printed out - our albums, I should
say - from the tuples, and we can see them

44
00:04:16,168 --> 00:04:21,860
on screen there. Now this would be tidier -
the code that I just wrote - if we unpacked

45
00:04:21,860 --> 00:04:26,620
the tuple and used sensibly named
variables for the album name,

46
00:04:26,620 --> 00:04:31,980
artist and year. So in other words,
changing it so we had something like this;

47
00:04:31,980 --> 00:04:39,600
we're going to make this name
change there, to name comma artist, comma year.

48
00:04:39,600 --> 00:04:41,100
Now you can see the errors on the screen

49
00:04:41,100 --> 00:04:45,300
because we haven't unpacked the tuples
yet. I know I haven't unpacked them either.

50
00:04:45,300 --> 00:04:49,140
I'll be doing that in the next
lecture. Before that, your challenge is to

51
00:04:49,140 --> 00:04:53,430
fix the errors in the most efficient way
you can think of. So the challenge is to

52
00:04:53,430 --> 00:04:58,590
fix the errors in our code, by unpacking
the tuples into the variables, that we're

53
00:04:58,590 --> 00:05:03,330
using on line 12. Now, there are two ways that
you could do that. Try to do it by only

54
00:05:03,330 --> 00:05:10,080
changing one line of code. In any event,
I'll go over my solution in the next video.

