Introduction To Lists In Python (Python Tutorial #4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey CS dojo in this video I'm gonna introduce you to list in Python the list is a type of data just like strings and integers that we've seen so far and it's used to store a list of things and it's similar to arrays in some other languages like Java so let's take a look at one example here to define a list you can just write a equals open square bracket 3 comma 10 comma minus 1 close square bracket and this line means define a new list with the elements 3 10 and minus 1 and assign this list to this variable a and just a quick note here that you can download this sample file by going to CS no 2 dot io / Python 4 and another quick reminder that you don't necessarily have to use Jupiter notebook to follow this course you could use for example per charm which is a good IDE for Python now let's run the cell and once we run the cell a should be defined so let's print a with print parentheses a and we see that we get a list with the elements 3 10 and minus 1 what if we wanted to add an item to this list you can do that with a dot append parentheses 1 this means append the item the number one to the list 8 so this dot notation is a pretty common one and you'll see more of that later in this course so this function append is basically a predefined function that you can use on the list datatype so again that's a dot append parentheses one let's execute this cell and once we print a you see that the number 1 has been added to this list so one interesting thing about the Python list which is different from some other languages like Java is that you can mix types in a single list so you could have a list containing numbers as well as a string let's see how that works by appending the string hello to the list a with a dot a pen parentheses double quotes hello let's print that with print a execute this cell and you see that this list now contains hello the string as well as the numbers a list could even contain another list so let's see how that works with ADA app in square brackets 1 comma 2 so this means create a new list with the elements 1 & 2 and then append it to this list let's see how that looks with print parentheses a and let's run this cell and now you see that we have a bunch of numbers and then a string which is hello and another list within the single list okay what if you wanted to delete an item from this list for example the last item to delete the last item you can do a dot pop parentheses so we're using the dot notation again here and pop is another function that's predefined for the list datatype and that's why we can just write a dot pop open parentheses close parentheses and to see the result you can of course print a and let's run the cell and you see that a now has the numbers and the string but not the list because that's popped or deleted let's delete the string as well this Hello string with a dot pop which still is the last item of the list of course and then let's print a and you see that it now has numbers only now there are many other predefined functions other than pop and a pen on the list data type but a pen and pop are two of the most common ones okay what if you wanted to retrieve a specific item from this list for example the first item the second item or the third item to do that you can just write a square brackets 0 and this means retrieve the item from the list a with the index 0 and in Python just like in many other languages the index starts at 0 so the first item in a list has the index 0 and the second item as the next one the third item has the index 2 and so on ok so we want to print this item just to make sure we got the right one but actually interpreter notebook you don't have to use the print statement every time you could just write a square bracket 0 here and when you run this cell you get the first item in the list but to keep it consistent and also to make sure it works in any other platform for example Park charm I'm gonna use the print statement every time okay what if you wanted to retrieve the fourth item this number one to do that you can just do a square brackets 3 so the fourth item has the index 3 and then let's print that and we get 1 and what if you wanted to change the content of this list so what if we wanted to change this number 3 to 100 to do that you can first select that item with a square bracket 0 that's the first item in the list and then you can assign a new number or any new value to it with a square bracket 0 equals 100 let's run this cell and then let's print a again with print parentheses a and you see that the first value of this list is 100 now instead of 3 okay now I'm gonna give you a quick exercise problem to practice what you've learned so far let's say you have a list with three strings inside so you have B equals square brackets double quotes banana comma double quotes Apple comma and then Microsoft can you swap the first value of this list whose index is of course 0 with the last item of this list before I show you my solution to this problem I'm going to show you a model you can use for thinking about a list in Python when you have B equals banana Apple Microsoft the way you can think about it is that B is going to be sort of like three boxes put together or one giant box one long box with three compartments and each of those compartments acts like a variable so each of those refers to a value in this case the first compartment refers to banana and the second element refers to Apple and the third element of course refers to the string Microsoft and I'm going to show you my solution first right temp equals B square brackets 0 so we're defining a new variable called temp for a temporary variable which is going to refer to what B square bracket 0 refers to which is this one and then you can write B square bracket 0 equals B square brackets 2 so we're saying the first compartment should now refer to whatever the third compartment refers to because like I said the index for the first item is 0 and the index for the second item is 1 and the index for the third item is 2 so after this line the first compartment or the first element will now refer to what the third compartment refers to which is Microsoft and once you have that you can write B square brackets 2 equals temp and the third compartment now will refer to what temp refers to which is banana as a result after these pieces of code we have the desired result so that's a list with the first compartment referring to the value Microsoft and the second element being Apple and the third element being banana the string ok let's make sure that method works in code first print the current lists with print B and we have banana Apple and Microsoft just like we saw will first define a temporary variable with temp equals B square bracket 0 and then assign the value of B square brackets 2 to be square brackets 0 and then assign what temp refers to to be square brackets 2 and then at the end we're going to print B okay that's correct we have Microsoft Apple banana now and actually there's a shortcut for this and that's B square brackets 0 comma B square brackets 2 equals B square brackets 2 comma B square brackets 0 let's see if that works by printing B and once we print be the first element and the last element should be swapped again so we should get the same list as the first one banana Apple and Microsoft and that's what we get so this is definitely a valid way to swap two variables or two elements within a list but don't worry too much about this particular method okay just a quick reminder you can go to CH dojo dot io / Python for to download the sample file and the best way to support me would be through my patreon page and you can find that at CH dojo dot IO / PT ok I'm working from CH dojo and I'll see you guys in the next video
Info
Channel: CS Dojo
Views: 911,929
Rating: 4.961062 out of 5
Keywords: python lists, python list, python 3 list, how to use python lists, how to use python list
Id: tw7ror9x32s
Channel Id: undefined
Length: 9min 55sec (595 seconds)
Published: Fri Jan 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.