- 00:00:01 what's up guys and girls and welcome to
- 00:00:03 the fourth video in my Python tutorial
- 00:00:05 series in this video we're gonna go over
- 00:00:08 lists and tuples and basically the list
- 00:00:11 is just what it sounds like you want to
- 00:00:13 store a collection of you know integers
- 00:00:15 or strings or boolean to use what is
- 00:00:18 called a list and then a tuple is a
- 00:00:20 slightly different version of that and
- 00:00:22 I'll go over the distinction near the
- 00:00:23 end of the video before I begin I want
- 00:00:26 to know how python is going for you guys
- 00:00:28 so if you don't mind leaving a comment
- 00:00:30 down below just let me know like you
- 00:00:32 know what's challenging for you with
- 00:00:33 Python or let me know something cool
- 00:00:35 that you've built so far with Python I
- 00:00:37 want to build more of a sense of the
- 00:00:39 community on these videos so if you
- 00:00:40 leave a comment down below that would be
- 00:00:42 awesome ok for this video we'll be kind
- 00:00:45 of thinking about grocery lists as our
- 00:00:47 kind of build examples off of so if we
- 00:00:50 wanted to just create an empty list to
- 00:00:52 begin I'm just gonna call I'm gonna name
- 00:00:55 a variable grocery list and if I want to
- 00:01:00 define a list I use these brackets so
- 00:01:04 this grocery list now is just an empty
- 00:01:06 list but let's just initialize a couple
- 00:01:10 items into our list so let's say I need
- 00:01:13 some I need some I need some more milk
- 00:01:16 definitely need some more milk
- 00:01:18 I'm liking avocados a lot of my toast
- 00:01:21 these days so we got to get some more
- 00:01:23 avocados let me think what else I need
- 00:01:27 some more of you got to think that I got
- 00:01:29 to get a couple hot pockets for the
- 00:01:30 daunting on-the-go meal so now we have a
- 00:01:35 list of three items we have milk
- 00:01:36 avocados hot pockets and I can print
- 00:01:41 this out by just doing print grocery
- 00:01:43 lists and as you can see below it it has
- 00:01:47 all this items so that's just like
- 00:01:50 initializing in a list with a couple of
- 00:01:51 items but you know what's really useful
- 00:01:53 with list is adding and removing items
- 00:01:56 are kind of being able to to dynamically
- 00:01:58 change our list so what we'll do next is
- 00:02:02 we'll start like messing around with
- 00:02:04 this list birthday
- 00:02:10 career day I'm going grocery shopping
- 00:02:16 uni anything yeah need some Oreos what
- 00:02:18 else I got you alright so let's say we
- 00:02:23 wanted to add Oreos to our grocery list
- 00:02:26 because I'm a nice guy and we're gonna
- 00:02:28 go some do some grocery shopping for my
- 00:02:30 roommate so we can add Oreos very easily
- 00:02:34 to this list by doing grocery list dot
- 00:02:38 append and then I can do Oreo so append
- 00:02:43 means like add on so now so I did this
- 00:02:46 and now if I print it out the grocery
- 00:02:49 list I have four items in the list so
- 00:02:55 even though I just defined it with these
- 00:02:56 three items when I do append Oreos we
- 00:03:03 now have that fourth item and so I could
- 00:03:05 keep doing this maybe I also wanted some
- 00:03:07 you know I have milk avocados Hot
- 00:03:10 Pockets Oreos if like I need to add
- 00:03:12 another like healthy option to this so
- 00:03:16 let's do some like chicken it's nice
- 00:03:19 chicken get some you got to think that
- 00:03:23 we need some eggs so I can just keep
- 00:03:25 appending to this so I'm going to append
- 00:03:27 eggs so now we have three things we
- 00:03:31 appended and we have the initial items
- 00:03:33 that we declared when we start in our
- 00:03:36 grocery list so if I run this we have
- 00:03:39 milk avocado hot pockets Oreos chicken
- 00:03:41 and eggs so we got a lot of things now
- 00:03:44 in our grocery list okay so now that we
- 00:03:51 have you know adding items let's do the
- 00:03:53 reverse let's say we wanted to remove
- 00:03:54 some items so I'm going to just start
- 00:03:58 I'm gonna instead of appending these I'm
- 00:04:00 just gonna add these to my initial list
- 00:04:02 so I'm gonna add Oreos chicken and eggs
- 00:04:10 and so let's just say now I run this and
- 00:04:14 it has everything still but I'm now not
- 00:04:16 appending and I'm just initializing the
- 00:04:18 list to have that so let's say I wanted
- 00:04:20 to remove
- 00:04:21 something let's say I realized that I
- 00:04:23 looked in my fridge and I already had
- 00:04:24 eggs so I wanted to remove eggs I can do
- 00:04:28 the command grocery list dot pop and
- 00:04:36 this will pop off the end of the grocery
- 00:04:40 list the last item I added so in this
- 00:04:42 case eggs is the last item so if I run
- 00:04:46 this eggs is no longer there because I
- 00:04:50 popped it off so pop is another built-in
- 00:04:52 list function so we have append we have
- 00:04:55 the reversal of a pen despot so okay so
- 00:04:59 we've gone over two basic functions with
- 00:05:02 lists now we get kind of into the more a
- 00:05:06 little bit more complex things so let's
- 00:05:09 say I have pop and let's say I realize
- 00:05:13 that I actually also have avocados
- 00:05:15 already my buddy brought over some
- 00:05:19 avocados from these avocado farm so I
- 00:05:22 already have avocados so if I simply
- 00:05:25 just called grocery list dot pop twice
- 00:05:30 that's not going to do what I wanted to
- 00:05:33 do right because it just pops off the
- 00:05:35 end so I don't have to pop a lot of
- 00:05:36 things off our grocery list to get two
- 00:05:38 avocados so this is where indexing comes
- 00:05:42 in so we can specify a specific position
- 00:05:46 in the list with an indexing to do this
- 00:05:50 imagine each item in our list has its
- 00:05:52 own box
- 00:05:53 and if we want to know like what the
- 00:05:55 position of these elements are we start
- 00:05:58 counting from zero upwards so milk would
- 00:06:01 be at the 0th index avocados would be
- 00:06:04 the first index hot pockets would be at
- 00:06:07 the second Oreos would be at the third
- 00:06:09 chicken at the fourth and eggs at the
- 00:06:12 fifth and the key thing to note here is
- 00:06:16 that it starts at zero it does not start
- 00:06:19 at one so even though you might
- 00:06:21 intuitively say Oh milk is the first
- 00:06:23 element in Python and a lot of the other
- 00:06:25 popular programming language which is
- 00:06:27 the element start at zero so zero is the
- 00:06:31 first index
- 00:06:32 so now if I go back to my Python program
- 00:06:35 I can actually use this this indexing to
- 00:06:40 help us out so we wanted to get rid of
- 00:06:43 I'm going to just comp these out real
- 00:06:44 quick so right now I print out grocery
- 00:06:46 lists and we have all these items I want
- 00:06:50 to get rid of eggs because we already
- 00:06:51 have some so that's why I do this first
- 00:06:54 pop and then you see eggs is gone but I
- 00:06:58 also wanted to get rid of avocados so
- 00:07:01 now what I can do is I can do a grocery
- 00:07:03 list pop but I could actually put in a
- 00:07:06 number input inside this pop function so
- 00:07:09 if I go back to that screen avocados is
- 00:07:14 the first avocados is the first index so
- 00:07:19 I can just type in pop one so now if I
- 00:07:24 run this you see that I have milk hot
- 00:07:28 pockets Oreos chicken no avocados no
- 00:07:31 eggs just like we want so in addition to
- 00:07:34 counting outwards from 0 to 5 we can
- 00:07:36 also count downwards so I can also start
- 00:07:39 at eggs and we say that this is the
- 00:07:41 negative first element let me make that
- 00:07:45 a little better at that one negative
- 00:07:48 first element then chicken we're
- 00:07:50 continually counting backwards the
- 00:07:51 nega-chin is the negative second element
- 00:07:53 Oreos of the negative third Hot Pockets
- 00:07:56 is the negative fourth avocado is the
- 00:07:59 negative fifth and milk would be the
- 00:08:01 negative sixth element and it's useful
- 00:08:04 to uses negative captain often times
- 00:08:06 because you know maybe we we don't know
- 00:08:09 how long a list is and we just know we
- 00:08:12 want to like get rid of the last element
- 00:08:14 or change something to the last element
- 00:08:17 so we've used negative one there okay so
- 00:08:20 noting that avocados is now for counting
- 00:08:22 negatively the negative 5th element
- 00:08:24 let's go back to our Python code and try
- 00:08:28 doing this pop a different way so right
- 00:08:31 now we have will just get comment out
- 00:08:34 these pops again we get our full list as
- 00:08:36 you can see here add back in that first
- 00:08:40 pop that gets rid of the eggs and then
- 00:08:42 we're going to now do a new
- 00:08:45 pop so instead of one I'm going to try
- 00:08:47 negative five negative five was that the
- 00:08:52 correct value I said oh okay so this is
- 00:09:03 a tricky little thing here so what just
- 00:09:06 happened when I try to do negative five
- 00:09:08 after the initial pop was once I popped
- 00:09:12 the first time so I'm going to print out
- 00:09:14 grocery list here and then I'll print it
- 00:09:23 again after the second pop so after the
- 00:09:25 first pop we lose eggs so if we counted
- 00:09:28 backwards so we're starting a chicken
- 00:09:30 chicken is now negative on Oreos this
- 00:09:32 negative Co hot pockets is negative
- 00:09:33 three
- 00:09:34 avocados is negative four in milk is
- 00:09:36 negative five so that's why in the
- 00:09:38 second one we actually removed milk
- 00:09:40 instead of avocados because the position
- 00:09:44 of that the position was no longer the
- 00:09:48 negative fifth for the avocados so you
- 00:09:49 got to be careful when we do this so one
- 00:09:51 way we could do this if we wanted to use
- 00:09:54 negative values and get rid of eggs and
- 00:09:56 avocados as I could do negative five
- 00:09:58 first and then I could pop off second
- 00:10:03 just nothing I just don't put anything
- 00:10:05 there so that I'll take off the last
- 00:10:07 element by default and as you can see
- 00:10:09 milk hot pockets Oreos and chicken so
- 00:10:12 that's what we wanted cool and also it's
- 00:10:15 worth noting that if I try to type in a
- 00:10:17 number like 100 in here we're gonna get
- 00:10:19 an error so you have to make sure you're
- 00:10:21 within the bounds of your list and if I
- 00:10:24 try to do negative 200 it would also be
- 00:10:27 error and also as I said before if you
- 00:10:32 look at the you know if you look at
- 00:10:35 dahshur sorry if you look at these
- 00:10:40 indexes 0 is the first element so
- 00:10:42 remember that like it's really important
- 00:10:44 to remember that lists start at 0 as the
- 00:10:48 first position so starts at 0 so if I
- 00:10:51 wanted to go ahead and you know specify
- 00:10:56 that I wanted to pop off the first
- 00:10:58 and twice I could do pop zero and as you
- 00:11:01 can see now only the last four things
- 00:11:03 are there also while we're talking about
- 00:11:06 indexing let's just show how you can get
- 00:11:09 access to specific element in that list
- 00:11:11 so another thing we can do is if I
- 00:11:14 wanted to just check to see what the
- 00:11:16 second element was in my list so when I
- 00:11:19 say second I mean zero first second so
- 00:11:23 the Hot Pockets I could do grocery I go
- 00:11:27 to print grocery list and then I use
- 00:11:33 brackets here so if I you do the
- 00:11:37 variable of the list and then bracket
- 00:11:39 and I insert a number inside here like
- 00:11:42 two that is now getting the second index
- 00:11:47 of the grocery list and the second index
- 00:11:50 is hot pockets because we go zero one
- 00:11:53 two so I print that out I get hot pocket
- 00:11:56 it's cool you do the same thing with the
- 00:11:58 negative index thing I can do negative
- 00:12:00 two and this is negative one X is
- 00:12:03 negative one chicken would be negative
- 00:12:06 two so this will print out chicken so
- 00:12:09 that's pretty very very useful if you
- 00:12:12 need to like just know when a specific
- 00:12:14 element is like imagine instead of a
- 00:12:16 grocery list this was like a position
- 00:12:19 that we are building a game and this was
- 00:12:21 a position so this was like X comma Y so
- 00:12:24 I could do a hundred common like three
- 00:12:28 hundred and if I wanted to know the X
- 00:12:32 there you know the y coordinate so I
- 00:12:34 wanted to know three hundred I would
- 00:12:35 just do position and then one so this
- 00:12:44 would be the 0th position so 300 would
- 00:12:46 be the first position as you can see we
- 00:12:50 got 300 down there also useful also very
- 00:12:56 useful is in addition to indexing just
- 00:12:58 one element another thing we can do is
- 00:13:00 we could let's say let's say I just
- 00:13:02 wanted to get the middle four elements
- 00:13:07 of our array so I could do we have six
- 00:13:11 elements
- 00:13:11 total in this list so what I could do to
- 00:13:15 get the middle four elements is it's
- 00:13:17 pretty similar to just getting a single
- 00:13:19 element you do a grocery list and then
- 00:13:23 you start it you type in the number that
- 00:13:25 you want to start at so if I wanted to
- 00:13:26 get the middle four I would want to
- 00:13:27 start at index 1 which is avocados and
- 00:13:30 then I do : and I continue for however
- 00:13:35 long I want to go until so this is 0 1 2
- 00:13:39 3 4 5 so I want to include 4 so when
- 00:13:44 we're doing this the the number you put
- 00:13:47 in at the end is the first number we
- 00:13:50 don't want to include it so in this case
- 00:13:51 the first number we don't want included
- 00:13:53 is 5 so that's eggs so now if I print
- 00:13:56 out grocery list 1 2 5 I get just the
- 00:14:08 middle four elements so that's also very
- 00:14:10 useful and a couple little neat little
- 00:14:13 things about this is if I didn't include
- 00:14:15 a number at the start it assumes we're
- 00:14:17 starting it the zeroth element so if I
- 00:14:19 do this we get the first five similarly
- 00:14:24 if I did one and I didn't include the
- 00:14:26 last element you would know to go until
- 00:14:30 eggs and you can just play around with
- 00:14:35 this however you need to in your own
- 00:14:37 programs that you're right
- 00:14:38 then one final thing before I move into
- 00:14:41 some of the more complex functions you
- 00:14:42 can do with lists is we can get the
- 00:14:45 length of a grocery list or any list by
- 00:14:48 just typing in Lane le n grocery lists
- 00:14:53 so I do that nothing prints right now
- 00:14:56 because I didn't tell it to print but if
- 00:14:58 I then print length of the grocery list
- 00:15:04 make sure you sign it with the proper
- 00:15:06 amount of parentheses we get 6 which is
- 00:15:10 exactly how many elements there are in
- 00:15:12 here so that looks good ok let's go over
- 00:15:15 some other useful functions with lists
- 00:15:17 so a first is maybe we wanted to insert
- 00:15:20 an item in a specific location so that
- 00:15:25 say I wanted to insert something like
- 00:15:30 wow it's it's hard for me to think of a
- 00:15:32 hit food item right now but let's say I
- 00:15:34 needed some silverware so I wanted some
- 00:15:37 plastic plastic knifes I don't know well
- 00:15:41 actually I'll just type in silverware so
- 00:15:43 I wanted to add silverware inside of
- 00:15:45 here so and I wanted it for some reason
- 00:15:48 I wanted that to be in the top three
- 00:15:50 because you know if I don't get these
- 00:15:52 items it's fun you know it's not the end
- 00:15:55 of the world but if I don't have
- 00:15:56 silverware like how do I eat my avocados
- 00:15:59 so if we wanted to insert in a specific
- 00:16:02 location in the list we can do grocery
- 00:16:04 list dot insert that's another recognize
- 00:16:09 function and if you want to see all the
- 00:16:12 recognized functions I recommend you go
- 00:16:14 to the official Python documentation so
- 00:16:17 I'll drag that in real quick so Python
- 00:16:21 org slash like tutorial slash data
- 00:16:24 structures and I'm on lists right now so
- 00:16:26 I'm looking at the insert function right
- 00:16:28 now so insert I comma X and if you see
- 00:16:34 that insert an item at a given position
- 00:16:35 the first argument is the index the
- 00:16:38 element before which to insert so a 0 X
- 00:16:41 inserts at the front of the list this
- 00:16:43 cool so I'll close out of that
- 00:16:47 so inserts so I wanted it right before
- 00:16:52 Hot Pockets which is the second element
- 00:16:54 because milk is the 0th so I do
- 00:16:59 silverware
- 00:17:00 I do a shoot index first so two comma
- 00:17:04 silverware and right now I'm just
- 00:17:09 printing the length of a grocery list
- 00:17:10 and as you can see it did increase by
- 00:17:12 one but I actually want to print out the
- 00:17:14 grocery list ah not type there don't
- 00:17:16 take their grocery list and as you can
- 00:17:22 see silverware is where I want it to be
- 00:17:24 cool cool cool and I feel like I had one
- 00:17:27 other thing to say regarding this okay
- 00:17:30 actually the one thing I wanted to
- 00:17:31 mention is I probably know there's a
- 00:17:34 couple times throughout the video but
- 00:17:35 the reason we're able to directly modify
- 00:17:38 grocery list is because it's known as
- 00:17:41 this mutable data structure in Python
- 00:17:44 mutable means changeable so we can
- 00:17:47 directly alter and dynamically change
- 00:17:50 the the list with commands like insert
- 00:17:54 that's the key difference between lists
- 00:17:58 and tuples and I'll cover the tuples a
- 00:18:00 little bit more at the end still but
- 00:18:02 yeah grocery list or lists in Python are
- 00:18:05 mutable so you can change them you can
- 00:18:07 depend to them you can pop things off
- 00:18:09 you're allowed to change it your delao
- 00:18:11 to change the variables that are inside
- 00:18:13 the list and the list can grow as big as
- 00:18:16 you want and you can shrink it as much
- 00:18:18 to do one you can have an empty list
- 00:18:22 okay we've done insert I'm able to leave
- 00:18:25 this up insert another thing you might
- 00:18:29 want to do is you know what if you
- 00:18:31 didn't know the index of a specific item
- 00:18:33 so we can also find the index or we can
- 00:18:38 find the index by doing the function the
- 00:18:42 function grocery list dot index and
- 00:18:48 let's say you know I knew I included
- 00:18:50 Oreos in there somewhere but I don't
- 00:18:52 remember the index of it I can do index
- 00:18:54 of Oreos and you have to be careful I
- 00:18:56 think yeah you'll have to spell this
- 00:18:58 exactly as it is in your code
- 00:19:01 so now if I printed this so nothing's
- 00:19:09 gonna print from this come in because it
- 00:19:11 doesn't actually change the list at all
- 00:19:14 but I can print this out right here
- 00:19:17 print grocery list index of Oreos and
- 00:19:21 also it's going to print out the first
- 00:19:24 occurrence of Oreos so if you had
- 00:19:25 multiple Oreos in your list it would
- 00:19:27 only get the first thing if you want to
- 00:19:31 get inside a specific range there's
- 00:19:33 that's in the documentation that I just
- 00:19:35 showed you so for and that's exactly
- 00:19:37 right 0 1 2 3 oh shoot what heaven oh I
- 00:19:41 inserted silver waited too so that's why
- 00:19:43 it became 4 we ran this command that's
- 00:19:47 index you can also just directly remove
- 00:19:50 something from a list by doing
- 00:19:52 move so if I wanted to actually I'll
- 00:19:56 undo that if I wanted to remove a
- 00:19:59 specific name of an item I would do
- 00:20:05 grocery list dot remove or is and if I
- 00:20:13 print now print out the grocery list
- 00:20:15 you'll see that oreos is gone so that's
- 00:20:19 another function so we've done insert
- 00:20:21 index remove we have from the start of
- 00:20:23 the video append and pop we've covered
- 00:20:27 all the different indexing of the list
- 00:20:31 too so you can get like the second and
- 00:20:32 third element next we'll do some we'll
- 00:20:34 switch this up a bit and I'm going to
- 00:20:36 delete this grocery list so all of these
- 00:20:39 functions I'm just going to delete right
- 00:20:40 now you can just go back to this point
- 00:20:43 in the video if you forget them but
- 00:20:45 we're just going to delete our grocery
- 00:20:46 list and instead we're gonna do a list
- 00:20:49 of grades so 134 72 so there's a lot of
- 00:20:57 functions that help are helpful for you
- 00:20:59 with grades as well as with with strings
- 00:21:05 so our grocery list use strings this is
- 00:21:08 going to use integers 93 84 87 87 100
- 00:21:18 100 100 so one person did really bad
- 00:21:22 here within 34 a lot of people did
- 00:21:24 really good with a hundred it's company
- 00:21:26 people got 87 s so here are some useful
- 00:21:28 commands for a list of numbers so the
- 00:21:33 first one is maybe we were scoring this
- 00:21:37 this test we were a teacher and we were
- 00:21:38 grading this test it wouldn't be helpful
- 00:21:41 for me to put that in in numerical order
- 00:21:44 so I can do grades dot sort and then if
- 00:21:48 I print out the grades so it begins so
- 00:21:52 I'll print this out twice I'll print it
- 00:21:54 before I sort it and after so this is
- 00:21:56 all inside the documentation so as you
- 00:21:59 can see the first time I printed it it
- 00:22:01 was exactly how I typed it in the second
- 00:22:04 time after I sorted
- 00:22:05 it looks like this so it's in this nice
- 00:22:09 order in addition to sorting we can also
- 00:22:12 do grades dot reverse so if I wanted to
- 00:22:16 go from big to small and then I print
- 00:22:18 this so now we have three different
- 00:22:22 things here we have way I initially
- 00:22:25 typed it in we sorted it with the sort
- 00:22:27 function and then we reverse the sort
- 00:22:32 with the reverse function reverse order
- 00:22:40 sort and you can in the sort function
- 00:22:43 you can like to find how you want it to
- 00:22:44 be sorted look at the documentation x'
- 00:22:47 for that sort in increasing order and so
- 00:22:57 one other thing that is very useful is
- 00:22:59 we can do a count of each score so if i
- 00:23:03 wanted to know how many hundreds got in
- 00:23:05 and let's say maybe we had ten thousand
- 00:23:08 scores in our list the annoying to have
- 00:23:10 to manually look through all of those
- 00:23:11 numbers and the ten thousand so if i
- 00:23:16 wanted to just get the amount of a
- 00:23:18 certain value i could do a grades dot
- 00:23:21 count of 100 and if i print that out
- 00:23:33 oh my gosh what happened if I print that
- 00:23:38 out you see four that's exactly right
- 00:23:41 and that's just like play around so play
- 00:23:43 around with these things so now I could
- 00:23:44 just pop off a couple scores and guess
- 00:23:50 how many hundreds they're going to be
- 00:23:52 after this so after we popped off the
- 00:24:02 last two values we eliminated to
- 00:24:04 hundreds and now I return a different
- 00:24:06 count of 100 which is two I could also
- 00:24:09 print out the count of the 87's which
- 00:24:12 also will be two so that's the count
- 00:24:18 function so we've done in total in this
- 00:24:21 video we've done a pend we've done pop
- 00:24:23 we've done insert we've done remove
- 00:24:25 we've done index we've done count we've
- 00:24:30 done sort we've done reverse so a bunch
- 00:24:32 of different functions you can look at
- 00:24:34 the documentation I'll put a link in the
- 00:24:35 description to the documentation but a
- 00:24:38 lot of fun things we can do with lists
- 00:24:40 okay let's end this video talking about
- 00:24:42 tuples and then also we'll combine
- 00:24:44 everything we've learned in all these
- 00:24:46 videos for like one last little exercise
- 00:24:49 so tuples they're very similar to lists
- 00:24:54 the way we would write a tuple is
- 00:24:56 instead of using brackets like we do for
- 00:24:58 the list we use to write a tuple we
- 00:25:01 write will say values equals we use
- 00:25:05 parentheses so for a tuple we use
- 00:25:07 parentheses twelve twelve twelve let's
- 00:25:11 say we have a tuple of three values and
- 00:25:16 so here's the difference between lists
- 00:25:20 and tuples twelve twelve twelve maybe
- 00:25:24 I'll do like one two three instead of
- 00:25:26 twelve twelve twelve just so we can kind
- 00:25:28 of have a little bit of separation
- 00:25:30 between the things two three so the way
- 00:25:37 I declared the values up top is a list
- 00:25:41 and the way I declared it down below is
- 00:25:43 a tuple
- 00:25:47 I like it a little bigger so you can see
- 00:25:49 doing more clear and I'll actually name
- 00:25:51 this values list and this as values
- 00:25:56 tuple just to separate them now I have
- 00:25:59 them reset each other okay so we have
- 00:26:02 these two things we have the list up top
- 00:26:04 and you have the tuple down below so the
- 00:26:08 first thing about the list is I've said
- 00:26:11 this before in the video it's mutable so
- 00:26:14 all the commands we were doing we're
- 00:26:16 doing append pop remove etc you can do
- 00:26:23 because this type of lit the list
- 00:26:25 structure allows you to just alter it
- 00:26:29 any way you see fit the tuple on the
- 00:26:33 other hand is called immutable and
- 00:26:37 basically that means that it's a set
- 00:26:41 object once you set the object the way
- 00:26:44 you want it the values stored and the
- 00:26:48 the structure doesn't change so it's it
- 00:26:52 does not change so for items like so
- 00:26:56 think for things that you don't really
- 00:26:57 want to like add to and remove from that
- 00:26:59 you know they're set in stone there's
- 00:27:01 these three values and that's what you
- 00:27:03 want the value is to stay you don't want
- 00:27:05 this lit this tuple that change around
- 00:27:07 at all you would use yeah you'd use the
- 00:27:11 tuple to store those and okay so I mean
- 00:27:16 one one cool thing about the tuple one
- 00:27:19 reason one way I would probably use this
- 00:27:21 is inside of a function so I'm just
- 00:27:23 gonna leave this list real quick so
- 00:27:27 let's say we had a function called
- 00:27:29 update and that took in an x coordinate
- 00:27:33 a y-coordinate and a Z coordinate right
- 00:27:40 I could have a certain rule so maybe
- 00:27:44 like x equals x plus five like there's
- 00:27:50 some rule you have for how you want
- 00:27:52 these things update maybe you're drawing
- 00:27:54 the line and each time you have a new
- 00:27:56 point you need it to update a certain
- 00:27:58 way
- 00:27:59 like mirrors of slope y equals y minus 3
- 00:28:02 and Z equals Z Z plus 10 so we have this
- 00:28:10 update this is our update function and
- 00:28:15 what I'll do that uses a tuple is I'll
- 00:28:17 just return the three new values as a
- 00:28:20 tuple because I'm not going to change
- 00:28:21 until I call update again but in that
- 00:28:24 case we have deconstructed the tuple so
- 00:28:27 I'll return a tuple of X comma Y comma Z
- 00:28:31 and now if I call update we're going to
- 00:28:35 print out update print update of let's
- 00:28:41 say 10 10 10 and we can easily see that
- 00:28:45 change that we should expect so we
- 00:28:48 expect to see X now 15 Y is 7 and this
- 00:28:54 is 20 so if I run this yeah 15 7 20 and
- 00:28:58 it comes back as a tuple one nice thing
- 00:29:01 about tuples is this it has this kind of
- 00:29:04 feature that you can unpack it it's
- 00:29:05 called so so if we ran this update
- 00:29:08 function I could reset x y&z to be these
- 00:29:13 new values by doing the following I can
- 00:29:16 just write X comma Y comma Z equals
- 00:29:22 update and Python knows that oh there's
- 00:29:27 gonna be three values in this update
- 00:29:28 function that it returns assign the
- 00:29:31 first value to X the second value to Y
- 00:29:33 and the third value to Z and now if I
- 00:29:40 print out X it knows it's the new value
- 00:29:44 so we passed in 10 here but I reset it
- 00:29:47 here using the tuple Louie unpacked it
- 00:29:50 and now we get 15 so just something
- 00:29:53 useful to use a tooth before I feel like
- 00:29:55 when I'm storing and like you know
- 00:29:57 messing around with a lot of numbers are
- 00:29:59 a lot of names I'm usually using a list
- 00:30:01 because I usually want to be able to
- 00:30:03 remove
- 00:30:03 things and bad things so ok let's go
- 00:30:07 back to lists really quickly I just
- 00:30:09 realized I missed one thing that I
- 00:30:10 wanted to say and that is that if we had
- 00:30:14 a list of grades so we're moving on from
- 00:30:16 tuples tuples I said all I really wanted
- 00:30:21 to have got two pools maybe I had some
- 00:30:22 grades again 95 93 80 and 82 82 right so
- 00:30:31 we had just have four grades here if I
- 00:30:33 wanted to maybe one of these students
- 00:30:35 here in one of these four students comes
- 00:30:37 up to me and says Keith you graded this
- 00:30:41 in properly I want some points back so
- 00:30:45 let's say the guy that got an 80 you
- 00:30:46 know I messed up when I was creating and
- 00:30:48 he should have gotten a 90 so in Python
- 00:30:52 I can make this change directly to the
- 00:30:54 list by first indexing the spot that I
- 00:30:58 want to change so this is the zeroth
- 00:31:01 element this is the first element this
- 00:31:03 is the second element so I would do
- 00:31:07 second element because it's the 80 we
- 00:31:09 want to change and I can actually just
- 00:31:11 directly assign it to a new value so now
- 00:31:13 I do grades 2 equals 90 I print the
- 00:31:17 grades beforehand and then I print the
- 00:31:19 grades afterwards you'll see the first
- 00:31:25 time it was an 80 second time it's been
- 00:31:27 altered so it's now 90 so this is a
- 00:31:30 useful thing to know and you can do that
- 00:31:33 with multiple grades too so let's say
- 00:31:35 the first person and the second or in
- 00:31:39 the second person so I use a 3 here
- 00:31:41 because the first person I don't want to
- 00:31:45 include is the third person here the 0 1
- 00:31:48 2 third person so let's say the first
- 00:31:52 person actually got 100 and the second
- 00:31:55 person still got that 90 so I could pass
- 00:31:57 in 190 and now I print out that so you
- 00:32:04 see now I passed in a list it updated
- 00:32:07 these two elements in the list perfectly
- 00:32:12 okay to end this video let's combine
- 00:32:14 everything we've learned so far in all
- 00:32:16 of these video
- 00:32:17 and watch we learned one more useful
- 00:32:19 command for lists so I just want a list
- 00:32:24 let's say we have a list of names so we
- 00:32:28 have names like I don't know Ben Jerry
- 00:32:35 that's kind of funny ice cream I'm
- 00:32:38 getting hungry I think Bob Joe
- 00:32:44 Xavier Tyranitar shockwave yes we're
- 00:32:56 just having fun right now and Keith my
- 00:32:59 name and how about one more led Pegasus
- 00:33:02 so all very common names you know you
- 00:33:05 got Ben Jerry Bob joke Xavier Tyranitar
- 00:33:08 previous Keith and Pegasus and I want my
- 00:33:13 function to tell me whether or not Keith
- 00:33:16 is included so a specific name is
- 00:33:19 included in my names list so I'm going
- 00:33:23 to just call this function check for
- 00:33:28 name and I'm gonna be able to pass in
- 00:33:32 whatever name I want to check in this
- 00:33:34 life so it doesn't have to be Keith it
- 00:33:36 can be any of these names I put in this
- 00:33:37 list or or maybe your name or anything
- 00:33:39 so follow along and do this with me so
- 00:33:43 name so we're passing in a name whatever
- 00:33:45 name we want to check will be held by
- 00:33:48 this name variable so what I can do
- 00:33:53 and then we'll also pass in the list of
- 00:33:56 names so we have a name that we're
- 00:33:58 checking for in the list of names to
- 00:34:00 check so what I can do is I can say okay
- 00:34:04 I want to know if Keith is in this
- 00:34:06 function so if I could do if name and
- 00:34:10 there's actually this function called or
- 00:34:13 not I guess a function but there's a
- 00:34:15 method that's just I n so in so this
- 00:34:19 allows us to actually check for an
- 00:34:21 element inside of a list so if name in
- 00:34:24 names I can return true so the need
- 00:34:30 name does exist else I can return false
- 00:34:39 so now if I run check for name with
- 00:34:47 let's say Keith and our list of names
- 00:34:50 that we defined up here this should
- 00:34:54 print out true and it did if I tried to
- 00:35:03 capitalize my name it would print out
- 00:35:08 false if I threw in just like a random
- 00:35:12 name like John Cena it would print out
- 00:35:18 false and it include John Cena if I try
- 00:35:22 to search for Jerry it'd be true so this
- 00:35:30 is a function that allows us to check
- 00:35:33 for names and also I can actually
- 00:35:35 simplify this function I kind of wrote
- 00:35:37 it a little bit improperly just to see
- 00:35:39 if you guys would catch this instead of
- 00:35:41 doing if name and names return true else
- 00:35:44 return false I can actually just
- 00:35:45 directly I don't even need to use this
- 00:35:49 if statement I can just directly return
- 00:35:51 name and names and then maybe that
- 00:35:54 wouldn't be intuitive right off the bat
- 00:35:56 but this actually still works so Jerry's
- 00:35:59 in there Bob should return true xaviar
- 00:36:08 should return true Joey
- 00:36:13 Jill with 12 E's for to return false and
- 00:36:18 one final thing I can also like pop off
- 00:36:21 my name stop pop or remove I'll remove
- 00:36:25 my name because I forget what index it
- 00:36:26 is Keith and then if I check for my name
- 00:36:31 it should say false now yep and it did
- 00:36:35 if I deleted this line
- 00:36:40 true okay that's all I got for this
- 00:36:43 video
- 00:36:44 I recommend playing around and trying to
- 00:36:46 like test out what you've learned in
- 00:36:47 this video go to that coding back
- 00:36:49 website I showed in the previous video
- 00:36:51 that's a good place to practice all
- 00:36:53 sorts of exercises I'll be posting the
- 00:36:57 next tutorial video by this time next
- 00:37:00 week in that next video we'll cover for
- 00:37:03 loops and while loops which do a lot of
- 00:37:06 building off of the list that we covered
- 00:37:09 today also in the next video I'll
- 00:37:12 introduce a game that you can build so
- 00:37:14 it'll be a separate little video but we
- 00:37:16 can program our first like fully
- 00:37:18 function fully functional animated game
- 00:37:21 using the skills that we've learned in
- 00:37:23 these first five actual tutorials
- 00:37:25 including the next one I'm going to be
- 00:37:27 posting so 5 including that next one
- 00:37:29 alright if you liked this video make
- 00:37:32 sure to throw it a big thumbs up and
- 00:37:34 subscribe so you don't miss any of the
- 00:37:37 future tutorials thanks again for
- 00:37:40 watching peace