1
00:00:00,180 --> 00:00:07,920
So far, we have discussed numbers and strings, but the reality in an actual program, we often want

2
00:00:07,920 --> 00:00:12,720
to collect our results and maybe store all of them in one variable.

3
00:00:13,380 --> 00:00:19,290
So of course, we could do this by writing something like, I don't know, a one is equal to one and

4
00:00:19,290 --> 00:00:21,060
then make another one.

5
00:00:21,810 --> 00:00:26,490
A two is equal to five and a three is equal to 10.

6
00:00:26,820 --> 00:00:29,220
But you can see this is really tedious.

7
00:00:29,220 --> 00:00:36,030
We have to continue and we have to define many of these variables, and we'll be a bit difficult to

8
00:00:36,030 --> 00:00:41,130
assess the individual variables because we always have to remember what we named them.

9
00:00:41,850 --> 00:00:44,940
So much better would be to just make a list.

10
00:00:45,270 --> 00:00:50,940
And this is, of course, possible with Python by, for example, defining a variable called List one,

11
00:00:51,450 --> 00:00:56,220
which will be these square brackets.

12
00:00:56,400 --> 00:00:59,290
And then you can just write to individual elements of the list.

13
00:00:59,310 --> 00:01:07,950
For example, these square numbers and what you can also do is you can combine several data types in

14
00:01:07,950 --> 00:01:08,730
a single list.

15
00:01:09,480 --> 00:01:15,840
For example, you have one, then a string, which is a and then another number.

16
00:01:17,130 --> 00:01:18,840
So now we have two lists.

17
00:01:19,710 --> 00:01:23,520
What we can do now is we can select individual elements of this list.

18
00:01:24,260 --> 00:01:29,010
We can, for example, start with list one and then one.

19
00:01:31,530 --> 00:01:37,110
So maybe you are a bit surprised now if you're new to Python, because the result is four, which is

20
00:01:37,110 --> 00:01:39,060
actually the second element of this list.

21
00:01:39,840 --> 00:01:45,300
And the reason for this is that these indices in Python start counting from zero.

22
00:01:46,080 --> 00:01:50,010
So if we write zero, then we actually mean the first element.

23
00:01:50,730 --> 00:01:53,580
If we write one, then we mean the second element.

24
00:01:54,420 --> 00:01:59,580
So this is different in other programming languages, and it can be a bit confusing, but you just have

25
00:01:59,580 --> 00:02:00,460
to get used to it.

26
00:02:00,480 --> 00:02:02,280
This is the way of Python works.

27
00:02:02,820 --> 00:02:10,440
So if we want to have to second element write one now, we can also select multiple elements of the

28
00:02:10,440 --> 00:02:11,340
list at once.

29
00:02:11,640 --> 00:02:13,890
For example, our writing list one.

30
00:02:14,760 --> 00:02:18,750
And then we do not write here only a single value, but two values.

31
00:02:19,380 --> 00:02:24,840
So for example, if we want to have the first two values, you could think that we would have to write

32
00:02:24,990 --> 00:02:26,280
zero to one.

33
00:02:27,150 --> 00:02:29,540
But here it's again a bit confusing, to be honest.

34
00:02:29,550 --> 00:02:37,260
We have to write a zero to two because this means something like start from the index zero, which is

35
00:02:37,260 --> 00:02:46,230
the first element and go until the index is smaller than two, which means only use zero and one and

36
00:02:46,230 --> 00:02:49,200
zero and one correspond to the first and second element.

37
00:02:49,980 --> 00:02:53,660
So I agree this is a bit confusing, but this is what it is.

38
00:02:53,670 --> 00:02:55,500
You have to get used to it, unfortunately.

39
00:02:56,460 --> 00:03:00,230
But once you realize it, it's really not that difficult.

40
00:03:00,630 --> 00:03:03,210
Just have to remember start counting at zero.

41
00:03:03,570 --> 00:03:10,230
And when you want to select several elements, then you have to go one number higher than you actually

42
00:03:10,230 --> 00:03:10,860
want to go.

43
00:03:12,450 --> 00:03:17,280
So the next thing that we can, of course, do is we can manipulate these lists because it would be

44
00:03:17,280 --> 00:03:24,120
very tedious to always redefine the whole list if we want to change something so we can just manipulate

45
00:03:24,120 --> 00:03:24,630
the list.

46
00:03:25,500 --> 00:03:28,950
For example, we could write this one.

47
00:03:29,130 --> 00:03:31,380
And then there's a function called Append.

48
00:03:31,830 --> 00:03:37,920
So we write list one dots append, and then we just write what we want to add to the list.

49
00:03:39,120 --> 00:03:43,410
And the cool thing is that this number is already added to the list.

50
00:03:43,470 --> 00:03:48,170
So we do not have to write something like this one equals list, one appends.

51
00:03:48,210 --> 00:03:54,570
But just this, this is already sufficient and you see the number three has been added to the list.

52
00:03:56,040 --> 00:03:58,560
We can, of course, also remove something from lists.

53
00:03:59,100 --> 00:04:01,680
For example, this time, let's take our lists two.

54
00:04:02,190 --> 00:04:03,840
And let's remove the eight.

55
00:04:04,440 --> 00:04:09,930
So we write list two dots to remove a.

56
00:04:10,110 --> 00:04:12,720
And then I check what it will be.

57
00:04:12,720 --> 00:04:16,950
The result around this and this will give us the results.

58
00:04:18,300 --> 00:04:23,910
The only thing you need to be careful about is that when you run these cells multiple times, for example,

59
00:04:23,910 --> 00:04:30,390
by accident, you run this cell twice and then you have already added the number three twice at the

60
00:04:30,390 --> 00:04:31,110
end of the list.

61
00:04:32,790 --> 00:04:41,910
What we can also do is we can sort the lists by using the command sort and once again, this running

62
00:04:41,910 --> 00:04:50,610
this command will already manipulate the list and sort has some optional arguments that we are not going

63
00:04:50,610 --> 00:04:51,060
to use.

64
00:04:51,060 --> 00:04:54,580
For example, you could select that you want to store that number of First Order.

65
00:04:55,110 --> 00:04:58,770
But we want to we don't want to use any of the optional arguments.

66
00:04:58,770 --> 00:04:59,720
So you just write.

67
00:05:00,020 --> 00:05:07,510
It open bracket close and then it will work, but you need to remember that these brackets here and

68
00:05:07,550 --> 00:05:12,500
then the result will be not one four nine three, but one three four nine.

69
00:05:14,340 --> 00:05:14,790
All right.

70
00:05:15,000 --> 00:05:22,020
So one more thing that we can do when we want to manipulate our lists is to change a single value of

71
00:05:22,020 --> 00:05:22,440
the list.

72
00:05:23,130 --> 00:05:27,330
And what I want to do now is I want to change the number nine to number 10.

73
00:05:27,900 --> 00:05:29,550
So I just write list one.

74
00:05:30,270 --> 00:05:35,880
I want to select the fourth element, which will mean the index has to be three.

75
00:05:36,030 --> 00:05:45,990
Since we're starting counting from zero and this should be 10, then the result will be one three four

76
00:05:46,140 --> 00:05:46,530
10.

77
00:05:48,660 --> 00:05:53,820
Multidimensional lists are also possible, so we can create a new list list three.

78
00:05:54,390 --> 00:06:00,690
And here I just tried to square brackets one to three as an example.

79
00:06:01,140 --> 00:06:07,050
And then here another list which we could use here to square numbers one four nine.

80
00:06:07,650 --> 00:06:13,710
So you see, this is a list consisting of two lists, and now I can run this.

81
00:06:14,610 --> 00:06:17,880
And of course, now I can select individual values of the list.

82
00:06:18,000 --> 00:06:23,790
For example, I could write, I want to take only the zeros element or else that the first element,

83
00:06:23,790 --> 00:06:25,140
which would be this list here.

84
00:06:27,060 --> 00:06:31,770
And then I could also say from this list, I want to take the first element.

85
00:06:33,120 --> 00:06:38,190
So we get the one, this one here, that's all possible.

86
00:06:39,390 --> 00:06:44,760
So now I want to show you why lists are not always perfect because there are some problems with these,

87
00:06:44,760 --> 00:06:47,580
especially if you want to do mathematical operations.

88
00:06:48,690 --> 00:06:59,970
So if you remember all of this one, we, for example, could have the problem or we want to multiply

89
00:07:00,000 --> 00:07:02,340
all the values in the list by a number.

90
00:07:02,970 --> 00:07:09,660
For example, we could then write list times too, and we would think that this would give us two six,

91
00:07:09,660 --> 00:07:10,590
eight 20.

92
00:07:11,190 --> 00:07:13,830
However, if we run this, it doesn't work.

93
00:07:14,220 --> 00:07:18,510
It gives us twice the list and they are merged to a single list.

94
00:07:19,920 --> 00:07:22,080
And the same happens if we

95
00:07:24,690 --> 00:07:25,670
are not the same.

96
00:07:26,010 --> 00:07:28,590
A similar problem happens when we want to add a number.

97
00:07:29,760 --> 00:07:37,760
So here we even get an error because plus like four strings means concatenate, so basically merge lists.

98
00:07:37,800 --> 00:07:43,590
So we tried to merge here this list with a number which is not working because one of these properties

99
00:07:43,620 --> 00:07:46,860
is the list and the other one is just a number.

100
00:07:47,880 --> 00:07:51,720
So it would work if we would write something like this.

101
00:07:54,940 --> 00:07:58,810
But then we would just add another element to our list.

102
00:07:59,260 --> 00:08:02,460
So this is the same as using the append commands.

103
00:08:02,890 --> 00:08:06,340
And this is not really what I had in mind here by adding a number.

104
00:08:07,450 --> 00:08:14,530
So the thing is, lists are really nice to quickly store some, some some variables, some numbers,

105
00:08:14,530 --> 00:08:22,390
some strings, but it's very difficult to do mathematical operations with them and also storing things

106
00:08:22,390 --> 00:08:24,760
in lists requires a whole lot of memory.

107
00:08:25,420 --> 00:08:33,850
So most of the times in our for our purposes lists are not really ideal only if we have a very few elements

108
00:08:33,850 --> 00:08:38,830
that we do not want to do any mathematical calculations with them.

109
00:08:39,760 --> 00:08:45,610
So what we are going to use instead are arrays and these are we'll discuss in the next lecture.

