Detailed Notes
We cover the second part of two tutorials on the list functions as we look toward Python certification
Useful Links: https://www.w3schools.com/python/python_ref_list.asp https://www.geeksforgeeks.org/python-string-rfind/ https://www.w3schools.com/python/ref_func_sorted.asp
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] hello and welcome back we're continuing our season our series where we talk about python certification in this episode we are continuing uh from our prior episode if you remember we were working on pardon me while i adjust my mic just a little bit there we go maybe okay sorry three two one and so last time we talked about strings uh string functions and we just got not really started we got a pretty good way into them but now we're going to continue and we're going to start with the sort command so the first thing we do with this one is because we've got so many in this example if you sort of see we've got a lot of stuff going on so i'm going to jump to the end here and actually let me just do this just to help us with our eyeballs as we go through this um we'll do something like that and so we're gonna we've got a new list we're gonna work with because this is gonna be uh needed for some of the items we're gonna work on here so our example now is gonna be red green blue red green blue white black so it's just a bunch of strings they just happen to be color names so first thing we do is sort on a very simple sort uh did i say that and so we come down here and we sort that here's the original list and now we can see that we sort it now it sorts the items are black blue blue green green red red white and so it is alphabetically sorting everything bla comes before blue and so sor there's some there are a few features we can do a sort that we're not really gonna mess with too much uh just because um i don't think you're gonna see them uh but essentially you can do some sort based on a couple things that well the one we will show basically you can do a custom sort before i jump into that let me do where's that list i'm gonna do so as an example for sort this is going to be for reverse which as you will see is somewhat similar and it's important to note that the sort and reverse actually impact the list itself the output for these is nothing but it does change the list itself so now if we look at list reverse um yeah i'll do it this way first so if you look at list reverse it's easiest here because we can see here's what it was after we sorted it alphabetically and now if you look it went white red red green green blue blue black exactly the reverse of what it did so all reverse does is it swaps the items or it reverses the order so that one's pretty easy uh now sort get a little more advanced example because you probably will come across it in this situation what i do is i go in and i'm just going to create this little function all it does is you give it a value and it's going to turn the length of the value and then let's see i can come down here and let me where did it go let me go back to let's just do this just so i can save the state of it and then right before i get into that i'm going to say examples equals example 2 which is basically so i can just reset to this this top setting [Music] and now what i'm going to do this time is i'm going to sort but i'm going to give it a key and that key is this length function that i defined so what it's going to do is it's going to call this function for every item and it's going to return the link so now we're going to see when we sort based on that key and it should be fairly obvious that we see that now it sorts the items of link 3 length 4 length five and it just goes through and it just sorts them that way so there are ways that you can do custom sorts that you can do some custom adjustments to these things and um and get something actually you know fairly complicated particularly you would use this if you're doing so it's not like a simple list like a string or numbers but maybe if you were had a string or sorry a list of objects you know or classes or some chickens would be objects that you were working with and they may have their own essentially from another language you may hear about it as a comparator of some sort that says uh maybe i'm sorting by id values or some calculated number something like that so sort can get pretty cool uh sorted is let's just go here [Music] let's do this and this so sorted is actually a little bit different approach if we wanted to do so here example.sort gives us uh does not return anything but it does change the uh the array itself and so let's see so here so i did that let's go back and reset and so if we do let's do this so let's do first we're going to do the sorted example and then we're just going to print example and if you look at that now you're going to see here um let me put something here to make this a little easier let's do it this way and we'll do sorted list [Music] so initially we do it if we send it sorted see so if we actually kick out the output of sorted then it is now that sorted list much as we saw up here but if we actually grab the list it goes back to what we originally what our example is here so it's not the same anymore which we did sort that at some point apparently what do we do to example two well somewhere i got that sorted anyways so now you see that's not actually i'm not sure how i messed this one up but well let's see so we got in a weird little state so let's just do this just to be safe there we go let's move that up so i think i messed it up there we go okay somewhere i must have screwed up my example two somewhere all right so sorted actually gives you back a sorted list sort sorts the list itself so it's sort of important to know particularly as you're playing around with things there let's do extend so extend we had an example way back here here we go if you remember when we did append what it did is if we added um let's do this let's grab that one again because i'm going to do these so this is going to be for our pinned back here me disappend i'm sorry extend plus i didn't even type that right so i do extend i'm going to look at these two so if i did colors in a pen and then i wanted to print colors so first i want to do a pen and then i'm going to do the same thing i'm going to reset it and i'm going to do extend and this should be the easiest way to see the difference in these two so if you see here this first one where i do the extend i'm sorry when i do the append it takes that array and just puts a it adds an object which is the array it adds that to the end but when i do extend it assumes there's going to be a list of items and it just builds it into the array so you see where the brackets start to identify that this is an item within our array and over here there is not that it takes all of those individual items and extends them in this effectively is your way to concatenate a pair of a pair of like you know lists or arrays or things like that is that you can just come in and you know start doing them from an extend and it will uh paste those two together for you essentially so next i want to do is uh length let's uh just keep it simple if i do the length of i'm sorry colors dot and then oh it's the line of colors uh one two three we're going to see that that last thing is it's going to give us a link i think we've done this before we may not have um but basically it's going to count here so we've got red green blue white black if we had this guy instead oh we can probably do that so if we do example and actually let's make sure i didn't screw that up somewhere so let's come down here into example that's going to be one two three four five six seven eight yep there we go there's our eight that's pretty straightforward and then the one of the last ones i want to do is uh sort of go back because i think we covered in and not in but we can do if let's do if red in colors i'm just going to do print it's in there and actually let's pick what's that [Music] and let's do if purple not in colors it's not in there and so this is just a simple logic and we'll see it's in there it's not in there so here we can actually look for red now if we change it to let's say just an e then you're going to find that it's not in there well it's in there doesn't show up it's looking for this exact item so in and not in are going to be based on the item uh items within the array so it's going to see if we can find them now if we were to do well for example red's in there twice but no it doesn't doesn't care it's just it either is or it is not now last thing i wanted to do was uh one other that's a string so if i do let's do my my string because i sort of missed this earlier and want to make sure i swing back around to it so if my string equals lazy dog jumps over log and i want to do a find so this is a find example where i'm going to put that somewhere there we go [Music] so when i do find and i'm going to do our find and so if i do let's do my string dot oops fine and let me find um something let's see i'm not sure yet what i'm gonna do so let's do um let's do oh [Music] and so between find and are fine it's going to be looking for o within here let's see what it finds it does six and it oops that's not in our fine and it does that's there we go 6 and 21. and so oh good and here's a good example because we do have three o's in here so if you find 0 1 2 3 4 5 6. so find and that's what happens gives you the first occurrence so if you want to do sometimes you see this is like a pos or position or something like that function the r fine actually starts at the end and finds the last occurrence which is zero one two three four five six seven eight nine 10 11 12 13 14 15 16 17 18 19 20 21 which is what we got so we get our 21 down there and it skips over this one fine gets the first one are fine get you the last one and i think i'm gonna wrap that up for this time around because i threw the i did throw those strings in and actually i think i'm gonna probably move these because i was just as i was looking through this i did swing back around so i'm going to throw those in fine throw that in here and that'll work so that'll be updated in the code repository and that covers uh really covers a lot of what we've got in the world of arrays i would um i would go back through these a little bit and just play around with them so you you have an understanding of what they are in particular the um the things that deal with indexes of some sort uh that tell you you know like index and fine or not but index and um length and uh those kinds of things just to see what's there uh you may want to look into the i don't think you need anything complicated in the sorted like i did you know probably just to know that you can do a key uh you may want to play around with that just from you know feeling comfortable when we get to that certification point and that will this that'll do it for uh this one i don't think we need to go very further in it we'll get a good break here in in topics we'll switch over to something else next time around uh but until then go out there have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
hello and welcome back
we're continuing our season our series
where we talk about python certification
in this episode we are continuing uh
from our prior episode if you remember
we were working on pardon me while i
adjust my mic just a little bit there we
go maybe okay sorry
three two one
and so last time we talked about strings
uh string functions
and we just got not really started we
got a pretty good way into them but now
we're going to
continue and we're going to start with
the sort command so the first thing we
do
with this one is because we've got so
many in this example if you sort of see
we've got a lot of stuff going on so i'm
going to jump to the end here and
actually let me just do this
just to help us with our eyeballs as we
go
through this um
we'll do something like that
and so we're gonna we've got a new list
we're gonna work with because this is
gonna be
uh needed for some of the items we're
gonna work on here so our example now is
gonna be red green blue
red green blue white black so it's just
a bunch of strings they just happen to
be
color names so first thing we do is sort
on a very simple sort uh did i say that
and so we come down here and we sort
that here's the original
list and now we can see that we sort it
now it sorts the items are black blue
blue green green red red white
and so it is alphabetically sorting
everything
bla comes before blue
and so sor there's some there are a few
features we can do a sort that we're not
really gonna
mess with too much uh just because
um i don't think you're gonna see them
uh but essentially you can do some sort
based on a couple things that
well the one we will show basically you
can do a custom sort
before i jump into that let me do
where's that
list i'm gonna do
so as an example for sort this is going
to be
for reverse which as you will see
is somewhat similar and it's important
to note that
the sort and reverse actually
impact the list itself
the output for these is nothing but it
does change
the list itself so now if we look at
list reverse
um yeah i'll do it this way first
so if you look at list reverse it's
easiest here because we can see here's
what it was after we sorted it
alphabetically
and now if you look it went white red
red green green blue blue black
exactly the reverse of what it did so
all reverse does is it swaps the items
or it reverses the order
so that one's pretty easy uh now sort
get a little more advanced example
because you probably will come across it
in this situation what i do is i go in
and i'm just going to create this little
function
all it does is you give it a value and
it's going to turn the length of the
value
and then let's see
i can come down here and let me
where did it go let me go back to
let's just do this just so i can save
the state of it
and then right before i get into that
i'm going to say examples equals example
2 which is basically so i can just reset
to this
this top setting
[Music]
and now what i'm going to do this time
is i'm going to sort but i'm going to
give it a key
and that key is this length function
that i defined so what it's going to do
is it's going to call this function
for every item and it's going to return
the link so now we're going to see
when we sort based on that key
and it should be fairly obvious that we
see that now
it sorts the items of link 3
length 4 length five
and it just goes through and it just
sorts them that way so there are ways
that you can do
custom sorts that you can do some custom
adjustments to these things
and um and get something actually you
know fairly complicated particularly you
would use this if you're doing so it's
not like a simple list like a string or
numbers but maybe if you were
had a string or sorry a list of objects
you know or classes or some chickens
would be objects that you were working
with
and they may have their own essentially
from another language you may hear about
it as a comparator of some sort that
says
uh maybe i'm sorting by id values or
some calculated number
something like that so sort can get
pretty cool uh sorted
is let's just go here
[Music]
let's do this
and this
so sorted is actually
a little bit different approach if we
wanted to do so here example.sort
gives us uh does not return anything but
it does change
the uh the array itself
and so let's see so here so i did that
let's go back and reset
and so if we do
let's do this
so let's do first we're going to do
the sorted example
and then we're just going to print
example
and if you look at that now you're going
to see
here um let me
put something here to make this a little
easier
let's do it this way and we'll do sorted
list
[Music]
so initially we do it if we send it
sorted
see so if we actually kick out the
output of sorted
then it is now that sorted list much as
we saw up here
but if we actually grab the list it goes
back to what we originally
what our example is here so it's not
the same anymore which we did sort that
at some point
apparently
what do we do to example two
well somewhere i got that sorted anyways
so now you see that's not actually i'm
not sure how i messed
this one up but well let's see so we got
in a weird little state so let's just do
this
just to be safe
there we go let's move that up
so i think i messed it up there we go
okay
somewhere i must have screwed up my
example two somewhere all right so
sorted
actually gives you back a sorted list
sort sorts the list itself
so it's sort of important to know
particularly as you're playing around
with things there
let's do extend so extend we had an
example
way back here here we go
if you remember when we did append
what it did is if we added um
let's do this let's grab that one again
because i'm going to do these so this is
going to be for our pinned
back here
me disappend
i'm sorry extend plus i didn't even
type that right so i do extend
i'm going to look at these two so if i
did colors in a pen and then i wanted to
print colors
so first i want to do a pen and then i'm
going to do the same thing i'm going to
reset it
and i'm going to do extend and this
should be the easiest way to see the
difference
in these two so if you see here this
first one
where i do the extend i'm sorry when i
do the append
it takes that array and just puts a it
adds an object which is the array it
adds that to the end
but when i do extend
it assumes there's going to be a list of
items and it just builds it into the
array so you see where the
brackets start to identify that this is
an item within our array
and over here there is not that it takes
all of those individual items
and extends them in
this effectively is your way to
concatenate a pair of
a pair of like you know lists or arrays
or things like that
is that you can just come in and you
know start doing them from an extend and
it will
uh paste those two together for you
essentially so next i want to do
is uh length let's uh just keep it
simple
if i do the
length of i'm sorry
colors dot and then
oh it's the line of colors uh one two
three we're going to see that
that last thing is it's going to give us
a link i think we've done this before
we may not have um but basically it's
going to count
here so we've got red green blue white
black if we had this guy
instead oh we can probably do that
so if we do example and actually let's
make sure i
didn't screw that up somewhere
so let's come down here into example
that's going to be one two three four
five six seven
eight yep there we go there's our eight
that's pretty straightforward and then
the one of the last ones i want to do
is uh sort of go back because i think we
covered
in and not in but we can do if
let's do if red
in colors
i'm just going to do print
it's in there
and actually let's pick what's that
[Music]
and let's do if purple
not in colors it's not in there
and so this is just a simple logic
and we'll see it's in there it's not in
there so here
we can actually look for red now if we
change it to
let's say just an e then you're going to
find that it's not in there
well it's in there doesn't show up it's
looking for this exact item so in
and not in are going to be based on the
item uh items within the array so it's
going to see if we can find them
now if we were to do well for example
red's in there twice but no
it doesn't doesn't care it's just it
either is
or it is not now last thing i wanted to
do
was uh one other that's a string so if i
do
let's do my my string because i sort of
missed this earlier
and want to make sure i swing back
around to it so if my string equals
lazy dog jumps over log
and i want to do a find
so this is a find example where i'm
going to put that somewhere
there we go
[Music]
so when i do find and i'm going to do
our find
and so if i do
let's do my string dot
oops
fine and let me find
um something let's see i'm not sure yet
what i'm gonna do
so let's do um let's do oh
[Music]
and so between find and are fine it's
going to be looking for o within here
let's see what it finds
it does six
and it oops that's not in our fine and
it does that's
there we go 6 and 21. and so
oh good and here's a good example
because we do have three o's in here so
if you find
0 1 2 3 4 5 6.
so find and that's what happens gives
you the first occurrence
so if you want to do sometimes you see
this is like a pos or position or
something like that function
the r fine actually starts at the end
and finds the last occurrence which is
zero one two three
four five six seven eight nine 10 11 12
13 14 15 16 17 18 19 20 21
which is what we got so we get our 21
down there
and it skips over this one fine gets the
first one
are fine get you the last one
and i think i'm gonna wrap that up for
this time around because i threw the i
did throw those strings in and actually
i think i'm gonna probably move these
because i was just as i was looking
through this
i did swing back around so i'm going to
throw those in
fine throw that in here
and that'll work
so that'll be updated in the code
repository
and that covers uh really covers a lot
of what we've got in the world of
arrays i would um
i would go back through these a little
bit and just play around with them
so you you have an understanding of what
they are in particular
the um the things that deal with indexes
of some sort
uh that tell you you know like index and
fine or not
but index and um
length and uh those kinds of things just
to see
what's there uh you may want to look
into the i don't think you need anything
complicated in the sorted
like i did you know probably just to
know that you can do a key
uh you may want to play around with that
just from you know feeling comfortable
when we get to that certification point
and that will this that'll do it for uh
this one i don't think we need to go
very further in it we'll
get a good break here in in topics we'll
switch over to something else next time
around
uh but until then go out there have
yourself a great day a great week
and we will talk to you next time
you