1
00:00:04,569 --> 00:00:08,870
Alright, so let's now create the first
Python file for this project. We're going

2
00:00:08,870 --> 00:00:20,720
to call it lists_intro.py. In Python, a list is enclosed in square brackets.

3
00:00:20,720 --> 00:00:23,960
Back in the previous section, we
saw a slide with a list of computer

4
00:00:23,960 --> 00:00:28,020
parts, so let's type that in again. So
computer_parts

5
00:00:28,020 --> 00:00:32,500
is equal to left and right
square brackets, then in double quotes

6
00:00:32,500 --> 00:00:37,370
each item, so it can be computer in the
first one in double quotes, comma monitor in

7
00:00:37,370 --> 00:00:43,700
double quotes comma keyboard
in double quotes with a comma.

8
00:00:43,700 --> 00:00:49,900
Then mouse in double quotes with a comma, and
finally mouse mat in double quotes and

9
00:00:49,910 --> 00:00:54,200
then we've got the right square bracket as
well. Now you'll often see list literals,

10
00:00:54,200 --> 00:00:58,010
which these, are split up, so that each
item is on a new line. I'll be talking

11
00:00:58,010 --> 00:01:00,980
about this later, when we've got a more complex list.

12
00:01:00,980 --> 00:01:05,560
For now what I'm going to do, is press ENTER after each of the commas, to split the list up,

13
00:01:12,100 --> 00:01:16,399
and also, the last right
square bracket, I'll put onto a

14
00:01:16,400 --> 00:01:22,740
separate line as well. Alright, so that's
a very simple list. So computer_parts

15
00:01:22,740 --> 00:01:25,070
is a list of strings here - the names of some common computer

16
00:01:25,070 --> 00:01:28,640
parts for a computer. I mentioned
previously, that square brackets are used

17
00:01:28,640 --> 00:01:33,229
in three places in Python and this is
the third use. We've seen them, previously, being

18
00:01:33,229 --> 00:01:38,270
used to index a sequence and for slices.
They're also used to create lists, as we're

19
00:01:38,270 --> 00:01:42,220
doing here. So inside the square brackets,
we've got the items in the list,

20
00:01:42,220 --> 00:01:46,670
separated by commas. Alright, so that's a
very simple list, but because it is a

21
00:01:46,670 --> 00:01:50,539
sequence type, we can iterate over it,
using a for loop. So let's go ahead and

22
00:01:50,539 --> 00:01:58,100
do that. So I'm going to type for part in
computer_parts: and the next

23
00:01:58,100 --> 00:02:05,899
line indented, print parentheses part.
Run the program, and you can see we've got

24
00:02:05,899 --> 00:02:12,830
each item of our list displayed in the
output pane. So we can refer to

25
00:02:12,830 --> 00:02:17,120
individual items in the list, using their
index position as well. It works exactly

26
00:02:17,120 --> 00:02:19,400
the same as we've seen with strings.

27
00:02:19,400 --> 00:02:24,080
The third item in the list is keyboard, so
that's at index position 2, remembering

28
00:02:24,080 --> 00:02:29,000
that indexing starts at 0. So let's have
a look at that: print left and right

29
00:02:29,000 --> 00:02:35,220
parentheses here, to make a space between lines, and then on the next line, print computer_parts

30
00:02:35,220 --> 00:02:38,540
left square bracket 2
right square bracket and right

31
00:02:38,540 --> 00:02:45,320
parenthesis. So if you run that, you can
see we get the keyword, or the word

32
00:02:45,320 --> 00:02:49,700
keyboard, printed out. Now you can also
slice a list, and that works

33
00:02:49,700 --> 00:02:53,180
exactly the same as we've seen for strings. What
I'll do is print out a couple of

34
00:02:53,180 --> 00:02:56,650
examples to revise what we've already done.

35
00:02:59,460 --> 00:03:05,400
So print parenthesis computer_parts left square bracket

36
00:03:05,400 --> 00:03:10,840
0:3 right square bracket then right
parenthesis, and on line 15, print

37
00:03:10,840 --> 00:03:17,180
parentheses computer_parts
left parenthesis minus 1. We've got our

38
00:03:17,180 --> 00:03:25,540
right square bracket and right parenthesis. So if you run the program now,

39
00:03:25,540 --> 00:03:30,580
you see that the first slice - that's
the code there on line 14 -

40
00:03:30,580 --> 00:03:34,720
that's produced the first 3 items from the list,
in this case, computer, monitor and

41
00:03:34,730 --> 00:03:39,080
keyboard. Note that the output here is in
square brackets, and that indicates that

42
00:03:39,080 --> 00:03:44,990
slicing a list produces another list. Now
line 15 is a Python idiom, and returns

43
00:03:44,990 --> 00:03:49,430
the last item in the sequence - the last
item in the list, in this case - which was

44
00:03:49,430 --> 00:03:56,240
mouse mat. And we can confirm that by
looking at our setup on line 1. Alright, so

45
00:03:56,240 --> 00:04:00,200
that's all been revision so far. It's not
surprising that indexing and slicing

46
00:04:00,200 --> 00:04:03,620
works the same with a list as it did with
a string, and that's because they're both

47
00:04:03,620 --> 00:04:07,980
sequence types. But there's one big
difference between strings and lists:

48
00:04:07,980 --> 00:04:12,500
strings are immutable, which means they
can't be changed. Lists, on the other hand,

49
00:04:12,500 --> 00:04:16,100
are mutable. In other words, you can
change the contents of a list.

50
00:04:16,100 --> 00:04:20,320
I'll explain those terms in more detail, in
the next two videos, and then we'll look

51
00:04:20,329 --> 00:04:25,510
at ways to change a list's contents. See you
in the next video.

