Python Tutorial 2: Understanding and Using Python Lists and Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number two in our incredible new tutorial series where you are going to learn to code in python or you are going to die trying what i'm gonna need you to do is pour yourself a nice strong cup of black coffee that's black coffee no sugar no sweeteners none needed and i'm gonna need you to get ready to learn some cool new stuff so what i will need you to do is come over and i need you to call up your most excellent python idol you can go down in your little windows search bar and search on idle we did this last week and click on idle python and we end up with this most excellent idle shell that we can start playing with python and it annoys me that it brings this thing in really tall so i've got a i've got to do a little windows management here to make sure that i end up with something that doesn't go off the bottom of the screen that is looking pretty darn good and let me i wish it would pop up the same size that i'd left it before but it does not but if i make it like that you are going to be able to see it without anything going off the bottom of the screen okay i also as you're getting your idle called up i as always want to give a shout out to you guys that are helping me out over at patreon it is your help and your encouragement that keeps these great lessons coming you guys that are not helping out yet take a look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion talking about my patreon account let's jump in and let's get ready to learn some cool new stuff okay so last week we learned how to print in python how to to print and format print formatted print in python we also learned our basic mathematical operators of add subtract multiply divide the most excellent mod function and then we also learned about how to raise something to a power we also learned a little bit about variable types we looked at string variables we looked at int variables integers and then we also looked at floats well what we're going to learn today is a little bit more advanced type of variable type and i want to go ahead and do this right at the start because this is one of those foundational things that we're going to need so as we start programming in python you really need to understand these data types and what we're going to do is we're going to learn about what's called arrays or list and the way you could think about this is is that if we just have the variable x you can think of it as a bucket the bucket is labeled x and then you can put a number in there and if you ever go and look at x it will return to you the number that was put in that bucket so like if you put four marbles in x and then you say print x it prints four well now if you put seven marbles in x and say print x it prints seven and so the seven would overwrite the four so whatever the last number you put in that's the one that's going to return it if you put in a 4 and then you put in a 7 it forgets the 4 and it remembers 7. so with these standard variable types you can have one number in the variable at a time now the problem with that is sometimes you have one concept let's say that you're going to keep track of grades you have one concept which is a student's grades but there's going to be more than one grade well one way to do that is you could like say you could say like grade 1 is equal to 97 you could say grade 2 is equal to 98 and you could say grade 3 is equal to 76 is equal to 76 and then you could say print and then you could print grade 2 and you would get your 98 back but the problem with this is is that when you're writing the program as a coder you don't know whether someone might have three grades or 50 grades and then also doing it this way it would require them to go in and change the program to change the grade since you need a more flexible data structure to hold the grades and so the way you could do that is you could use an array a python array or a python list and let me show you how that works in that case instead of having a different variable a different bucket for each number for each grade what we're going to create is a special type of variable which is called an array and that array is more like a file cabinet that has different slots and so i can call the variable grade but now instead of just a big bucket there is slots inside of it and there's the first grade the second grade the third grade the fourth grade and then i can call out whichever one i wanted and so the way that would work is i am going to call the variable grade and then i'll call it grades because there's going to be more than one and then i'm going to say it's equal to now in the bracket what i'm going to put is all my grades 97 okay 98 and then 76 and let's put in some more let's put in 54 let's put in 99 let's put in 85 and let's put in uh 67. all right now i've got to close that okay and now i press enter now i can say print and then i can print grades like that and then what does it print it prints the list of grades so you see i have one variable the variable is an array it is called grades and that grades contains the whole list of grades now that's kind of interesting but really what do you want you want to access you want to access individual grades because let's say you might want to print an individual grade or you might want to change an individual grade you've got to be able to address the grades individually so the way you do that is i'm going to print just to show you the the array or the variable is grades and now i can specify which one like the first one the second one the third one i can tell it to print the grade in a certain slot so inside of the bracket i need to tell it the slot position and so let's say i say print grades 2 okay grades in slot 2 and then i'm going to close that now before you do this look here at grades okay and tell me what number you think it is going to print tell me what number is it going to print now if you said 98 you would be wrong okay you would be wrong why because as a normal human being you start counting it one so this would be one and this would be two okay but computer programmers are not normal humans in any sense of the word and computer programmers and computer programs start counting at zero so what you gotta see is this is the zeroth slot this is the first slot and this is the second slot so when i said print grades of two it goes 0 1 2 and it prints 76 pretty cool huh and so if i wanted the 98 that is in the first slot so i would say print and then i would say grades and then that is in the first slot and then i close it up and boom it's like that i could also say something like x is equal to grades of zero plus grades of let's say zero one two three four five six grades of six okay and then that sets that why did i get that little oh i don't have a i guess i didn't oh i'm sorry i don't need to close that parentheses because i'm calculating x okay and then i'm going to say print x and then what is that 164 would be 97 plus 67 is 164. so you see you could start doing math on these things you can start doing math on them okay again if i don't put an index if i don't put an index i can just print and i can print grades and it will print that whole list of grades it will print the whole array or if i put grades of something it will print that index so this is zero one two this is the slot number three you notice you can type it in without putting the print and it will actually print it out little pro tip there for you to know okay now the array data structures can get even more sophisticated than this this is like if you're thinking about grades you could have one long list of grades right but there's other data sets that are by their very nature two dimensions where you would have rows and you would have columns okay what would be a data type that would have rows and columns well if you think it would be like a photograph because a photograph has got pixels a row of pixels another row of pixels another row of pixels another row of pixels and each pixel has a value and then that's how a photograph is made it is a two-dimensional array so let's try doing that okay the way i would do that is i could say picture and that would be equal to and i'm going to make it an array okay but now the first row needs to be an array so i am going to open up another bracket and so the first row is going to be 1 comma 2 comma 3 comma 4 and now it's 4 the picture is 4 pixels wide so i've got all four pixels of the first row i'm going to close that and now i've got to give it the second row so that would be 5 6 7 8 i'm going to close that and now i'm going to have the third row which is going to be 9 10 11 12. oh i use periods instead of commas that looks like an ip address doesn't it my bad okay now i close that row and now i've got to close the overall array okay and it liked it so now i could say print and i could print picture and then what it prints is it prints the array the first row is one two three four the second row is five six seven eight the third row is nine ten eleven twelve okay now what if i wanted to look at the first row well i would say print and then i would say picture and then what do i want i want to address it and i say the first row okay now what is that going to print it's going to print one two three four right you would be wrong because this is the zeroth row and this is the row one because again programming languages and programmers start counting at zero so the one row would be this one and that is five six seven eight if we wanted the actual first one it is the zeroth one so we would say print and then i would say picture of what picture of zero like that okay and that's one two three four similarly pick uh print and we're going to print pick picture and we are going to print picture and let's say this would be zero one two so this would be row two gotta close it like that and that is that last row which is the row number two zero one two and that's 9 10 11 12. okay so you see i can go grab that data one row at a time but a lot of times you would want to address this individual number how do i do that i put another index on it so i'm going to say print and then i'm going to say picture and what do i want i want row picture i want row 1 and then i want column two okay picture row one column two now this will return an individual number now i want you to tell me what number is this going to return i want to print picture row one column two what's it gonna return let's see okay it is seven because remember this is zero this is row zero this is row one and then once i'm in row one then i want element two this is element zero element one and element two pretty neat huh and that was indeed the number seven now you can actually make a three-dimensional array and you would do that by putting an extra set of outside brackets and then you would have brackets and you would sort of you would sort of nest these so you could have multi-dimensional arrays most of the time you'll have a one or two-dimensional array there's a small number of times you'll actually come back and have a three-dimensional array and you really never i can't really ever think of very many examples where you have an array with more than three dimensions but python can accommodate it you just sort of continue the pattern that we have here now you can also you don't have to just deal with numbers you can deal with strings so i could say fruits and fruits is equal to and then i could say apple apple and then end that string and then i could say banana and then i could say orange and then i could say grape okay then close that array ooh close that array and then print print fruits boom apple banana orange grape or i could index an individual fruit so i could say print and i want to print fruits and i want to print fruits of one tell me what that is going to print tell me what that is going to print it is going to print the most excellent banana because this is position zero and this is position one so i can do something i can do something like that now again python gives you enough flexibility to kill yourself and so i could say fruits and i could or let's say that i could put grades and grades are going to be equal to and i'm going to say 97 i'm going to say 34 and i'm going to say uh 23 and 9 99 and then i'm going to put bun and i'm going to put banana and then i'm going to put 100 okay now you see this is this is kind of crazy isn't it so you see it let me put a banana as a grade is that a smart thing to do though that's a very stupid thing to do but python gives you enough flexibility to be stupid so i can say print and i can say print grades of 0 and what am i going to get grades of 0 is a 97 as expected or i can say print and i could say grades of four like that and what is my fourth grade my fourth grade is banana now if you tried to add these numbers up and you had a string in your list it's going to crash and then you're going to have a lot of trouble understanding what it is now what i could also say is i could say x is equal to 97 i could say y is equal to 34. i could say z is equal to 23. i could say a is equal to 99. i could say b is equal to 100 and i could say c is equal to 101 and then i could say grades are equal to and then i could put x comma y comma z comma a comma b comma c c c now what do you see here okay what do you see here okay it locks it and now i'm going to print grades okay it prints the numbers because you have to see that this this sees it as a variable the variable x and this is very very different than if you had put it in like this it's very very different than if you'd put it in as strings okay so i could put it in like that and now if i say print grades it's going to print what it's going to print those strings so you've got to understand the difference between the variable x versus the string x okay so again we got to kind of keep track of our variable types now this is also kind of something crazy this is something crazy about python let me go back and get my fruits okay what if i copied that and i come back and put it back in and get my fruits fruits already back in there all right now i am going to print fruits of i'm going to print fruits of one what is that going to print what's that going to print you got it it's going to print banana but you remember up here when i had a two-dimensional array remember i made this two-dimensional array up here ah that was not what i wanted to do not what i wanted to do at all okay so here is this two dimensional array and then i said print picture one one well that took row one and then position one right this is row one position zero row one position one and so i can index an individual number what if i came here and said print and i said banana because this is just a one-dimensional array but what if i did something crazy like said print fruit and i'm gonna print fruit of one and i'm gonna put another index on here and i'm gonna say a second index of two so what do you think this is going to print if i print fruit fruits fruits plural fruits one and then two what do you think that's going to do any guesses python is kind of crazy it'll always just kind of figure something out so what is fruits of one that is banana and then what does it do with the two well it says he must want to bust that up so i'll bust it up so this is banana of zero this is banana of one and this is banana of two that is the letter n so you can actually index strings by using uh you know by kind of getting python to treat it as an array so that's just kind of like one of those crazy things that python will do okay now a lot of times if you wanted to input grades you don't know how many grades there are and you're going to have to the program's going to have to ask the user for the grades and so instead of like saying x you know instead of saying uh grade instead of saying grades of uh 2 is equal to let's see if i can do this grades of 2 is equal to 55. okay remember i already printed grades but now let me print grades again print grades again and when i prick print grades again oh okay you see it wouldn't change let me let me let me get my good grade array up here where is a reasonable grade array okay here it is okay let me get this one okay so you see that's my list of grades now now watch this i can say grades of 1 is equal to 25 okay and now if i print grades look at that now where i had a 98 i just changed it to 25 so you can go in and you can manipulate those things but a lot of times like if you made a program to track grades for a teacher you wouldn't want the teacher who's not a programmer to have to go in and edit the code you would want them to be able to input the grades and you don't know how many grades they're going to input you don't know how many grades they might have so a lot of times in your program you will just have to start by saying x is an empty array now you do this so that it knows that x is an array x is not a variable and so like if i just said x equals 7 then it's going to erase over that but i'm going to start with x is equal to an array okay so x is an array now what if i wanted to put a number in that array like what if i said x of 1 is equal to 97. okay when i do that i get an out of uh i get an out of range error the index is out of range because x is what empty and i said put in the first slot a you know in slot 1 and 97 there is no one slot because it's empty so it errors out and so if you start with an empty array how do you put numbers in it will you do it like this you can't do like a lot of programming language is would let you do something like this you can't do something like this in python so the way you would do it is you would have to put it in one at a time and so you would say x let's go and make sure print x and you see x is empty so what you have to do is you have to say x dot append and so you want to do the method append you're doing an append onto x and then what are you going to append now notice i'm not using the brackets because i'm not giving it an index i'm passing to the function append a number and that number is is 97 okay so i'm appending to x 97 and now i'm going to print x like that and there is a list with how many numbers in it one number it's the zeroth number is 97. well i could do that again x dot append and i could append the number 85 and now i say print x okay i could print x of x of 1 okay and it's 85 because it's the in the position 1. now do you see that when you're calling functions you typically use parentheses but if you're giving an uh if you're giving an index on a variable you will use the square brackets now why is append append is a function it is a function that python knows and therefore you're passing the number 97 to the append function which is attached to the variable x hopefully that makes sense but you got to kind of keep track of the difference between the square brackets and the old-fashioned parentheses and so that is some pretty nice stuff there okay guys this has been hopefully a good lesson for you what i need you guys to do is i need you guys to go around and kind of play with these things i don't have a formal homework assignment but i need you to go in and just on your own as a homework assignment play around with all of these things where you are completely comfortable with how to end up at the right index on these variables okay guys i really really hope much fun taking these lessons as i am making the lessons again really appreciate you guys for helping me out over at patreon the rest of you guys give me a thumbs up on this video it really helps me with the old youtube algorithms to get some thumbs up leave a comment down below i read every single comment and if you guys haven't already subscribe to the channel okay paul mcquarter with top techboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 29,859
Rating: undefined out of 5
Keywords:
Id: ndajyqo2QtA
Channel Id: undefined
Length: 27min 25sec (1645 seconds)
Published: Wed Mar 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.