1
00:00:00,000 --> 00:00:03,990
Tuples and lists are the basic array types in Python.

2
00:00:04,000 --> 00:00:06,990
tuples are immutable and lists are mutable.

3
00:00:07,000 --> 00:00:11,990
tuples are created with the comma operator.

4
00:00:12,000 --> 00:00:17,990
Say t = 1, 2, 3, 4, 5 like that, and I get a tuple with those five elements in it.

5
00:00:18,000 --> 00:00:27,990
So, element number 0 is the beginning of the list and element 4 is the end of the list.

6
00:00:28,000 --> 00:00:32,990
Of course, you can always get the end of the list with -1, like that.

7
00:00:33,000 --> 00:00:40,990
I can take the length of the list, I can get the smallest element, I can get the

8
00:00:41,000 --> 00:00:45,990
largest element and so I have a tuple.

9
00:00:46,000 --> 00:00:48,990
There is some confusion about how you create a tuple in Python.

10
00:00:49,000 --> 00:00:52,990
Sometimes it looks like you use the parenthesis to create a tuple, because more

11
00:00:53,000 --> 00:01:00,990
often than not you'll see it done like this to create the tuple, but in fact,

12
00:01:01,000 --> 00:01:02,990
it's the comma operator.

13
00:01:03,000 --> 00:01:05,990
This is the important distinction because if you want to create a tuple with one

14
00:01:06,000 --> 00:01:09,990
element, that won't do it.

15
00:01:10,000 --> 00:01:13,990
That creates an integer.

16
00:01:14,000 --> 00:01:17,990
The comma is required. The parentheses are for binding, and when you have a lot

17
00:01:18,000 --> 00:01:20,990
of elements separated by commas, the parenthesis are convenient for that and

18
00:01:21,000 --> 00:01:22,990
that's why you'll often see it that way.

19
00:01:23,000 --> 00:01:30,990
But in fact, if you want a tuple with one element, you create it like this.

20
00:01:31,000 --> 00:01:36,990
And now, we have a tuple. type(t) is <class 'tuple'>.

21
00:01:37,000 --> 00:01:44,990
Lists are created with square brackets.

22
00:01:45,000 --> 00:01:51,990
Now I have a list. type of x is <class 'list'>.

23
00:01:52,000 --> 00:01:57,990
Lists work very much like tuples in that I can subscript it in the same way or

24
00:01:58,000 --> 00:02:05,990
I can look at the last one and I can check its length and I can get the largest

25
00:02:06,000 --> 00:02:10,990
element and the smallest element.

26
00:02:11,000 --> 00:02:13,990
The difference is, is that I can change things.

27
00:02:14,000 --> 00:02:23,990
If I take my tuple and let's put something bigger in the tuple.

28
00:02:24,000 --> 00:02:29,990
Now I have a tuple with 25 elements in it and that is a tuple.

29
00:02:30,000 --> 00:02:38,990
Let's say that I try to change one of those elements.

30
00:02:39,000 --> 00:02:42,990
I get this error, 'tuple' object does not support item assignment.

31
00:02:43,000 --> 00:02:46,990
Tuples are immutable, so I can't change things in them.

32
00:02:47,000 --> 00:02:54,990
On the other hand if I create a list, I am using the list constructor here to

33
00:02:55,000 --> 00:03:01,990
convert this range into a list and there is my list and type(x) is <class l'ist'>

34
00:03:02,000 --> 00:03:07,990
and I can say x10 = 42.

35
00:03:08,000 --> 00:03:13,990
Now, we see that 42 right there in the list.

36
00:03:14,000 --> 00:03:18,990
So, a list is mutable and a tuple is not mutable.

37
00:03:19,000 --> 00:03:21,990
So, why would you want to use a tuple when you can use a list and the list is

38
00:03:22,000 --> 00:03:24,990
more powerful and has more functionality?

39
00:03:25,000 --> 00:03:27,990
Tuples are there because more often than not, when you them you don't need

40
00:03:28,000 --> 00:03:28,990
to be changing them.

41
00:03:29,000 --> 00:03:30,990
It's a smaller class.

42
00:03:31,000 --> 00:03:36,990
It's simpler to implement, they are faster for some things, but most importantly

43
00:03:37,000 --> 00:03:38,990
they don't allow you to change stuff.

44
00:03:39,000 --> 00:03:43,990
So, if you have a circumstance, and most circumstances are like this, where you

45
00:03:44,000 --> 00:03:46,990
need an object that's always going to stay the same size, it's always going to

46
00:03:47,000 --> 00:03:48,990
have the same data in it,

47
00:03:49,000 --> 00:03:52,990
you want to use a tuple so that you can't accidentally it, and if you do, you'll

48
00:03:53,000 --> 00:03:55,990
get an exception and you'll be able to find your problem.

49
00:03:56,000 --> 00:04:01,990
So, use your tuples where you can, and where you absolutely need to be able to

50
00:04:02,000 --> 00:04:12,000
change your array, that's where you use a list.

