1
00:00:00,000 --> 00:00:04,990
Python has several sequential types that look like lists of things,

2
00:00:05,000 --> 00:00:06,990
and we're going to look at a few of them right now.

3
00:00:07,000 --> 00:00:16,990
Let's make a working copy of variables.py and we'll call this variables-lists.py.

4
00:00:17,000 --> 00:00:18,990
And we'll open that working copy.

5
00:00:19,000 --> 00:00:20,990
So let's create a variable.

6
00:00:21,000 --> 00:00:25,990
And we'll just call it x. And we'll assign it a list of values in

7
00:00:26,000 --> 00:00:28,990
parentheses (1, 2, 3).

8
00:00:29,000 --> 00:00:30,990
And we'll go ahead and we'll print(x).

9
00:00:31,000 --> 00:00:34,990
What print will do with this is it will try to print it in a way that Python

10
00:00:35,000 --> 00:00:38,990
could actually create one if it needed to.

11
00:00:39,000 --> 00:00:41,990
And so it has it in the parentheses just like that.

12
00:00:42,000 --> 00:00:44,990
So let's print the type of it, also.

13
00:00:45,000 --> 00:00:46,990
Save that and run it.

14
00:00:47,000 --> 00:00:48,990
So the class is tuple.

15
00:00:49,000 --> 00:00:51,990
So this is a tuple.

16
00:00:52,000 --> 00:00:53,990
And a tuple is created with parentheses.

17
00:00:54,000 --> 00:00:56,990
And a tuple is an immutable object.

18
00:00:57,000 --> 00:00:58,990
So I can't insert things.

19
00:00:59,000 --> 00:01:00,990
I can't append things. I can't delete things.

20
00:01:01,000 --> 00:01:02,990
Once I've created it, I've created it.

21
00:01:03,000 --> 00:01:04,990
And I can't change it.

22
00:01:05,000 --> 00:01:06,990
So this is useful for a lot of stuff.

23
00:01:07,000 --> 00:01:10,990
Perhaps you just to operate on a fixed list of things.

24
00:01:11,000 --> 00:01:15,990
In fact, most of time, when you need a list of something, you're probably not

25
00:01:16,000 --> 00:01:16,990
going to need an immutable list.

26
00:01:17,000 --> 00:01:21,990
And to use a tuple is going to be faster than to use immutable list.

27
00:01:22,000 --> 00:01:26,990
On the other hand, if we create this with the square brackets, then we get the list type.

28
00:01:27,000 --> 00:01:29,990
I'll save this and run it.

29
00:01:30,000 --> 00:01:31,990
And the list type is mutable.

30
00:01:32,000 --> 00:01:34,990
I can, in fact, add something to the end of it.

31
00:01:35,000 --> 00:01:39,990
I can say x.append and call it the number 5,

32
00:01:40,000 --> 00:01:41,990
and save that and run it,

33
00:01:42,000 --> 00:01:48,990
and now we have the number 5 at the end. Or I can say x.insert,

34
00:01:49,000 --> 00:01:55,990
and insert it at the beginning, which I use the index 0 for the beginning, and put a 7 there,

35
00:01:56,000 --> 00:01:57,990
and save that and run it,

36
00:01:58,000 --> 00:01:59,990
and now I have a 7 at the beginning.

37
00:02:00,000 --> 00:02:02,990
I could, of course, insert that at any point.

38
00:02:03,000 --> 00:02:07,990
If I say 2, use the number 2 there.

39
00:02:08,000 --> 00:02:11,990
So this is item 0 and this is item 1 and this would be item 2.

40
00:02:12,000 --> 00:02:13,990
And we can expect the 7 to show up in there.

41
00:02:14,000 --> 00:02:15,990
So I'll save it and I'll run it.

42
00:02:16,000 --> 00:02:18,990
And there we have the 7 in there.

43
00:02:19,000 --> 00:02:24,990
So you can actually modify the list that is mutable, but if I were to change

44
00:02:25,000 --> 00:02:30,990
this back to the tuple type, which is immutable, and save it and run it, then I get an error.

45
00:02:31,000 --> 00:02:36,990
That tuple object has no attribute append, and in fact it has no attribute insert

46
00:02:37,000 --> 00:02:38,990
either, because it's immutable.

47
00:02:39,000 --> 00:02:41,990
And it cannot be changed.

48
00:02:42,000 --> 00:02:43,990
So we'll go ahead and we'll take these out.

49
00:02:44,000 --> 00:02:47,990
And I'm going to show you one other sequence type that you've seen before, but

50
00:02:48,000 --> 00:02:50,990
you might not have thought it in this way.

51
00:02:51,000 --> 00:02:51,990
And that is the string.

52
00:02:52,000 --> 00:02:57,990
If I save this and run it,

53
00:02:58,000 --> 00:02:59,990
we'll see that we have a string.

54
00:03:00,000 --> 00:03:03,990
Just like with any sequential type, I can actually look at particular elements

55
00:03:04,000 --> 00:03:06,990
of the string by subscripting it to say 2.

56
00:03:07,000 --> 00:03:08,990
So that will be 0, 1, 2.

57
00:03:09,000 --> 00:03:12,990
I'll just get the r. Save that and run it.

58
00:03:13,000 --> 00:03:17,990
And there's the r. Or I can do what's called a slice.

59
00:03:18,000 --> 00:03:23,990
And we'll learn more about slices later on. I can say 2:4.

60
00:03:24,000 --> 00:03:28,990
And I'll get a slice of just the r and the i. And in slices, it's worth noting

61
00:03:29,000 --> 00:03:33,990
that even though I'm using the 2 index for the beginning, which would be 0, 1, 2,

62
00:03:34,000 --> 00:03:39,990
that's the r, that I'm using the 4 index for the end which would be 2, 3, 4, it would be the n.

63
00:03:40,000 --> 00:03:44,990
The way slices work in Python, they don't actually return that last element.

64
00:03:45,000 --> 00:03:47,990
And there are explanations in the documentation just to why that is.

65
00:03:48,000 --> 00:03:51,990
And we will get more into slices in more detail in a later chapter.

66
00:03:52,000 --> 00:03:56,990
It's also notable that all of these sequence types can be used as iterators.

67
00:03:57,000 --> 00:04:07,990
So if I go back to (1, 2, 3, 4, 5), I have an immutable tuple.

68
00:04:08,000 --> 00:04:16,990
And I'm going to for I in x: print(i).

69
00:04:17,000 --> 00:04:20,990
And so that will print each one on a separate line, each of the

70
00:04:21,000 --> 00:04:21,990
different elements.

71
00:04:22,000 --> 00:04:25,990
And in fact, the string also works like that.

72
00:04:26,000 --> 00:04:31,990
If I put a string in here and save and run, I get each of those individual

73
00:04:32,000 --> 00:04:33,990
letters from the string.

74
00:04:34,000 --> 00:04:38,990
So this is how you use sequences in Python.

75
00:04:39,000 --> 00:04:39,990
There are a number of them.

76
00:04:40,000 --> 00:04:41,990
These are the most common.

77
00:04:42,000 --> 00:04:43,990
The tuple is immutable.

78
00:04:44,000 --> 00:04:47,990
the list is mutable and is designated with the square brackets instead of the

79
00:04:48,000 --> 00:04:50,990
parenthesis for the tuple,

80
00:04:51,000 --> 00:05:01,000
and a string is also a sequence, an immutable sequence.

