1 00:00:00,000 --> 00:00:04,000 We can now use variables with different data types. 2 00:00:04,001 --> 00:00:10,000 But what if you want, for example, to keep a collection of 100 temperatures? 3 00:00:10,001 --> 00:00:13,610 Are you going to create 100 variables named 4 00:00:13,622 --> 00:00:18,000 "temperature1", "temperature2", "temperature3", etc? 5 00:00:18,001 --> 00:00:22,028 Well, this would be very impractical, super long to 6 00:00:22,040 --> 00:00:26,000 create and prone to a lot of errors and headaches. 7 00:00:26,001 --> 00:00:32,000 So instead, you are going to use lists. A list is simply a collection of data. 8 00:00:32,001 --> 00:00:38,000 Or in other words, a collection of variables which are related to each other. 9 00:00:38,001 --> 00:00:42,000 So let's create a list so you can see what I'm talking about. 10 00:00:42,001 --> 00:00:46,000 So let's name it, we want temperature list, so let's name it "temperature". 11 00:00:46,001 --> 00:00:50,000 List. Okay, make the name meaningful. 12 00:00:50,001 --> 00:00:53,000 Don't try to make it too complicated, okay? 13 00:00:53,001 --> 00:00:56,172 Aim for simplicity. You want a list of temperatures, 14 00:00:56,184 --> 00:00:59,000 this is going to be called "temperature list". 15 00:00:59,001 --> 00:01:01,000 Then you are going to open and close brackets. 16 00:01:01,001 --> 00:01:06,000 And you are going to put the values you want inside this list. 17 00:01:06,001 --> 00:01:09,000 So for example, I'm going to put 12.3, okay? 18 00:01:09,001 --> 00:01:17,000 23.0, and then let's put a negative number also, minus 12.8. 19 00:01:18,000 --> 00:01:24,000 And then another negative number, minus 5.9. 20 00:01:24,001 --> 00:01:26,000 So here I have a list, I'm going to press enter. 21 00:01:26,001 --> 00:01:31,000 I have a list of four different float numbers. 22 00:01:31,001 --> 00:01:36,000 If I just do "temperature list", I can see the list. 23 00:01:36,001 --> 00:01:40,000 So the list is evaluated here and printed on the Python shell. 24 00:01:40,001 --> 00:01:44,000 Now what if I want to retrieve a specific element of the list? 25 00:01:45,000 --> 00:01:49,000 In this case, I'm going to use "temperature list" 26 00:01:49,001 --> 00:01:56,000 and also using directly brackets here and put the index of the element. 27 00:01:56,001 --> 00:01:57,000 Let's start with one. 28 00:01:57,001 --> 00:02:00,905 Okay, you can see actually with one, what I get 29 00:02:00,917 --> 00:02:05,000 is not the first element, but the second element. 30 00:02:05,001 --> 00:02:11,000 Why is that? Because in programming, usually we start to count at zero. 31 00:02:11,001 --> 00:02:17,000 So you can use the up arrow key to come back to the different instructions you have. 32 00:02:17,001 --> 00:02:19,000 Okay, so you don't need to write "temperature list" again. 33 00:02:19,001 --> 00:02:22,000 And then I'm going to put zero instead. 34 00:02:22,001 --> 00:02:25,000 And zero gives me, you can see the first index here. 35 00:02:25,001 --> 00:02:28,000 So this will be zero, one, two, and three. 36 00:02:28,001 --> 00:02:30,000 So let's get each element. 37 00:02:30,001 --> 00:02:36,000 So we have zero, we have one, let's get two, which is the third element. 38 00:02:36,001 --> 00:02:39,000 And let's get three, which is the last element of the list. 39 00:02:40,000 --> 00:02:42,000 Now, let's say I put four. 40 00:02:42,001 --> 00:02:49,000 What is going to happen is that I have an error, a list index out of range error. 41 00:02:49,001 --> 00:02:52,000 Okay, so that's the common error is that you have a list 42 00:02:52,001 --> 00:02:56,000 and you try to access an element which is outside of the list. 43 00:02:56,001 --> 00:02:58,000 Okay, so just remember when you have a list, 44 00:02:58,001 --> 00:03:05,000 the first element starts at zero for the index and then one, two, three, etc. 45 00:03:05,001 --> 00:03:12,000 A nice function you can use to actually get the length of a list is simply the "len", 46 00:03:12,001 --> 00:03:17,000 so L-E-N function, which is a built-in function inside Python. 47 00:03:17,001 --> 00:03:19,000 So you just put "len", you open the parentheses, 48 00:03:19,001 --> 00:03:23,000 and you can put the temperature list. 49 00:03:23,001 --> 00:03:28,000 You can put the name of the list, close the parentheses, and then you get four. 50 00:03:28,001 --> 00:03:30,000 So this gives you the number of elements in the list. 51 00:03:30,001 --> 00:03:33,000 You can see one, two, three, four. 52 00:03:33,001 --> 00:03:38,000 And so this information is very useful because you can see that the last index, 53 00:03:38,001 --> 00:03:43,000 okay, here is three, okay, zero, one, two, three. 54 00:03:43,001 --> 00:03:48,000 So the last index will simply be the length minus one. 55 00:03:48,001 --> 00:03:52,000 And this can help you avoid getting the index error in your program. 56 00:03:52,001 --> 00:03:56,000 Okay, so now we have seen how to get elements from the list. 57 00:03:56,001 --> 00:03:58,000 Now how you can modify an element. 58 00:03:58,001 --> 00:04:04,000 When you simply do "temperature list", you also put, so 59 00:04:04,001 --> 00:04:06,000 let's say zero, I'm going to modify the first element. 60 00:04:06,001 --> 00:04:11,000 So the same as for getting the element, but then you write equal, 61 00:04:11,001 --> 00:04:14,000 and then you can change the value, let's say 2.4. 62 00:04:14,001 --> 00:04:18,000 So now if I try to get the value, you can see this is 2.4. 63 00:04:18,001 --> 00:04:23,000 And if I print the list, the first value is now 2.4. 64 00:04:23,001 --> 00:04:24,966 What you can also do is, if you want to add 65 00:04:24,978 --> 00:04:27,000 another element here at the end of the list, 66 00:04:27,001 --> 00:04:31,000 okay, add a new element, you can simply do "temperature", 67 00:04:31,001 --> 00:04:35,000 so write the name of the list, "temperature list". 68 00:04:35,001 --> 00:04:42,000 And then use the append function like this, "temperature list.append". 69 00:04:42,001 --> 00:04:45,000 And let's say I'm going to add 10.7. 70 00:04:45,001 --> 00:04:47,000 So this is going to add a new element. 71 00:04:47,001 --> 00:04:50,000 Now I go back to "temperature list". 72 00:04:50,001 --> 00:04:54,000 And you can see I have five elements, okay. 73 00:04:54,001 --> 00:04:56,000 And the last one is 10.7. 74 00:04:56,001 --> 00:05:01,000 If I do "len" with "temperature list", now I have five. 75 00:05:01,001 --> 00:05:08,000 So now I will be able to actually, if I do "temperature 76 00:05:08,001 --> 00:05:11,000 list" 4, like this, I will not get an error like here, okay. 77 00:05:11,001 --> 00:05:16,000 I will get the last element because the length of the list is 5. 78 00:05:16,001 --> 00:05:18,000 Okay, so that's pretty much it about lists, 79 00:05:18,001 --> 00:05:20,000 and we are going to continue to use them later on. 80 00:05:20,001 --> 00:05:25,000 So here you have created a list with float numbers. 81 00:05:25,001 --> 00:05:28,000 But now that you can create a list with integer numbers, 82 00:05:28,001 --> 00:05:34,000 you can also create a list containing booleans, and also a list containing strings. 83 00:05:34,001 --> 00:05:39,000 And one recommendation I'm going to give you is to use, 84 00:05:39,001 --> 00:05:43,000 to stick to the same data type for all elements of a list. 85 00:05:43,001 --> 00:05:47,000 Okay, if you mix, let's say you use integer and booleans, 86 00:05:47,001 --> 00:05:52,000 inside the same list, well, you are probably going to have errors in the future. 87 00:05:52,001 --> 00:05:54,000 And also this can kind of break the logic, okay. 88 00:05:54,001 --> 00:05:56,000 If you want a list of numbers, 89 00:05:56,001 --> 00:05:59,000 you are not going to put a string inside the list of numbers. 90 00:05:59,001 --> 00:06:03,000 So stick to one data type for one list.