Detailed Notes
We take a first look at arrays and slicing them as part of our if statements in this episode. We also touch on the pass command.
Helpful links: https://www.w3schools.com/python/numpy/numpy_array_slicing.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] well hello and welcome back we are continuing our season where we're working on python and certification essentially and what we're going to do today is we're going to sort of continue a little bit in the vein of what we were working on before so we've got a little if statements and we're going to keep these but we're going to play around a little bit more this time around so what we can do is uh first i want to work on some hooray some arrays uh and we may not we'll see how far we get into this so let's go just start here with an array so uh we're gonna do here is an array example so we're gonna do uh my array equals and we're just gonna do one two three four five six seven eight nine ten well let's do this let's do eight nine ten and actually we're going to call this morning array because we're gonna work with this for a little bit and this is going to be day array which will be 11 12 13 14 15 16 and 17. and then we're going to do evening array which is going to be 18 19 20 21 2 33 24 because i'm gonna play around with these a little bit so if we look at the let's move these it probably doesn't like the space there so let's just do this first we're gonna do print uh morning array and we'll just look at that first where is there we go ah let's do this let's get rid of some stuff here we're gonna do python three i think it's a ray there we go so array vector uh didn't see i don't think we have the input yeah so we get uh oh we did put the input first okay i'm sorry so if we do that so we can see that it prints out the array much as we had it so printing an array pretty straightforward now one of the things we can do is we can actually check for a value so instead of doing the morning here we can actually say if it is in morning array then oh let's do it uh something like eight or seven so here note that it's saying good morning because seven is in the morning array so we can actually you know building an array pretty simple we can check what's in it we can also do what's called slicing and so i could actually do a print morning array i'm just gonna do let's just print like the first three i think this is the slicing let's see if this works out yep and so it pretty and it's a so a slice i'm just saying i want to do the index uh the zero index up to three which is zero one two three but it's not inclusive as you see here so that's 0 1 2 and here it's not going to include so that's item 3. if i go to the whole thing is what zero one two three four five six seven eight nine so there's nine there if i go to 10 because there's not 10 items then it's going to go all the way out if i do 12. it's going to go all the way out so see when you're doing and when you're doing a slice it's not going to let you know if so if it's essentially if it runs off the list so if i were to do uh slicing this array 10 to 12 actually let's do like something crazy like 15 to 20. and then we're going to see that that's an empty array because there is i'm sorry make sure i don't so there it's an empty array it's getting no items back in the 15 to 20. so this is a great way to check an index and not have it you know throw on an issue or something like that because you know that if it comes back empty then we have we've gone out we have an invalid index within the array otherwise i could do if i do 20. in that case now notice i'm not doing a slice here i'm actually trying to grab this specific item but in this case i am going to get an index out of range so if i slice if i do a slice and i don't think it likes it i'm trying to remember if it'll like that we'll check that real quick oh it does so it's going to go out to this range this is as far as it'll go or i can start at this item and so you can see that if i don't have something on the front and let's do this let's actually keep a couple of these before i go two nuts with them so here's a couple things uh and let's put so there's there are three ways we can do this now these let's go actually let's do this this we're actually grabbing the index we need the indexed item within the array and so we will get an error if this is an invalid index if we put that colon in there then it's going to treat it as a slice whatever is to the left is where we start whatever is to the right is where we end if there's nothing there it takes it all the way to the end and if the start if there's nothing there that means it's going to start at the very first so this so zero so if we go to four and we go zero to four what we're gonna see is that those two are identical this is zero to four i'm sorry this one is a blank to four this one is a zero to four you can go either way with those so that allows you to within here you could actually and let's say let's do a all array let's take this well let's just do this um all right equals let's add these together morning array plus day array plus eve array and then print all right let's see what happens when we try to add those together notice that it appends them all together and it is in a pen so if i were if i change this to day so let's just do this then notice it's it appends it each of these isn't a pen so we can append a raise let me go back where i was and so now what we have is i can actually change around this i said if int value in morning array i can actually do if it is in all array morning is going to be 0 to 10 i think is what we did and i can say if that is in all array and so 12 is actually going to be 13. and then this one greater is going to be in all array and what did i say i said greater than 17. so i'm gonna do that so actually i can do it this way that's going to be less than 11 greater than 17. and then i'm going to do that so let's go play with that for a second um yeah we can keep the rest of those in there so now if i do that whoops i didn't like that okay so now if i do enter so if i do a sit a five it says good morning if i do uh 11 it's still good morning because i did it up to 11. oh because i don't have a zero my mistake i got to move that i'm sorry so if i go up to 10 so that is going to be 12. uh is that right yeah i think that's right so let me see and then that's going to be uh 16 on no because that's four so that should be right okay let's try this so here's our array and so if we do um let's do this so let's do 10 it's gonna be good morning because it's gonna say that it showed up here uh if we do 12 then it's going to blow up oh did i miss something there argument of type is not iterable oh i'm sorry because it's not it's going to have to be equals my mistake because i can't do an n for a single cell that makes sense my mistake and so now it's saying it's not so it is off okay so let's look at this so i can go out to 0 to here so that should be 13 and that starts at 15 and that goes out to 10. so let's try that again so if i do 10 it's still good morning which is right if i do 11 it's good evening so i'm not getting that one right oh because i ended up oh i'm sorry because this one is actually this isn't evening it up sorry sorry sorry sorry let's fix those up a little bit so now the fault will be day so now if i go in the morning it says good morning if i go at uh 10 am oh sorry still morning but if i go to 11 it's going to be the day if i go at noon let's do let's make sure i check that real quick let's print all right 13 because i think i need oh i'm not going the wrong direction i don't need 13 i need 11 i think is what i need so now there we go good noon i do um one o'clock oh i did one o'clock it's still during the day now if i do five o'clock just during the day because that's going to be the last one here because this actually starts at oh this starts at five o'clock for the evening so now five o'clock is 17 if i do 17. good evening which is 5 o'clock well if i do 4 o'clock then it's still during the day so now i can do this instead i could actually have which is great if i want to do things like checking for items within a weekend or like i can do here i can slice stuff up i can look for ranges of colors and things like that this really makes it easy for me to play around with ranges and arrays it is key to understand these as we are as we're looking at certification now one other thing i failed to mention before i wanted to touch on is when we're building out in particularly else's and ifs and when we get into while loops sometimes we want to build something out and so we're going to say i'm going to come back to it basically so i'm going to say here so i don't have night yet so let's say 16 to we'll do to 9 o'clock so which is what 21 which is actually 20. i'm sorry and then we're going to say something here so we're going to say if it's 20 or beyond uh let's say 21 and beyond then i'm gonna do something else now i'm what i mean to do at some point is i'm probably gonna say you know good night but i can say i don't really know what i'm gonna do here yet and so instead i'm going to do a pass and what that does is it just if i don't do anything let's do that first if i don't do anything then it blows up right away it doesn't it's not going to run at all because it needs something after that i need that indented block but if i do pass that basically says i'm going to come back to this later and now it allows me to run it and i'm going to be fine and now i'll actually get um so if i do something late at night i'm going to get actually nothing because it actually fell all the way through it came to here it said hey i hit this and it doesn't fall the hat to else instead it says hey i'm here i'm just going to do a pass on that i'm not going to do anything so you can use that to put placeholders in and still allow yourself to run through and you may want to do like a you know may want to do like a pass and then you know to do let's fill in this code you know something like that so you may do something like that at some point pass is not used a whole lot um but it's really probably especially in a a running system but it is a way for you to you know put some placeholders in and have something that sort of flows through and even to slice and dice your programs a little bit so outside of the fact that you need to you need to at least know that it exists for the certification point of view it is something that can prove helpful in the long run so that's your basics of arrays now they do have vectors within python but that's a separate package um i'll probably swing back around and use that at some point so although there is uh and we'll probably swing back around as we're getting into the while loop we're gonna throw some vector stuff in there's really there's not a whole lot of difference between it other than once you get in vectors you can do things that are a little bit different than the way arrays work but you can still do multi-dimensional arrays much like vectors so i could do um i can do some other i'm not going to stop i'm not going to step into that because i probably need more than a few minutes i don't want to go too long on this so i'm going to wrap this one up and actually hold this as just really originally you know i'm going to say whoops i'm going to say part 1 is array part 2 is vector so we'll sort of get back to that later and we'll call this one a day um this will as always it'll be out there in the github repository take a look you can check out some of these examples you can play around with this especially get comfortable with the with the rays with their indexing and what they're slicing because you will see some of that and it'll probably be most likely you know you're going to see things like on a test what this number is making sure that it is the right one so that you get the correct elements in the array i would play with it a bit and be really comfortable with those before i get into the certification that being said uh we'll let you get back to it so go out there have yourself a great day a great week and we will talk to you next time [Music] you
Transcript Segments
[Music]
well hello and welcome back
we are continuing our season where we're
working on python
and certification essentially and what
we're going to do
today is we're going to sort of continue
a little bit in the vein of what we were
working on before so we've got a little
if statements and we're going to keep
these but we're going to play around a
little bit more
this time around so
what we can do is uh
first i want to work on some hooray some
arrays uh and we may not we'll see how
far we get into this so
let's go just start here with an array
so
uh we're gonna do
here is an array example
so we're gonna do uh my array
equals and we're just gonna do one two
three four five six seven
eight nine
ten well let's do this let's do eight
nine
ten and actually we're going to call
this
morning array because we're gonna work
with this for a little bit
and this is going to be day array which
will be 11
12 13 14 15 16
and 17.
and then we're going to do evening array
which is going to be 18 19
20 21 2
33 24 because i'm gonna play around with
these a little bit
so if we look at the let's move these it
probably doesn't like the
space there
so let's just do this first we're gonna
do print
uh morning array
and we'll just look at that first
where is there we go ah let's do this
let's get rid of some stuff here we're
gonna do python three i think it's a ray
there we go
so array vector uh didn't see i don't
think we have the input
yeah so we get uh oh we did put the
input first okay i'm sorry so if we do
that
so we can see that it prints out the
array much as we had it
so printing an array pretty
straightforward
now one of the things we can do is we
can actually
check for a value so instead of doing
the morning here we can actually say if
it is in
morning array
then
oh let's do it uh something like eight
or seven so here note that it's saying
good morning because
seven is in the morning array so we can
actually
you know building an array pretty simple
we can
check what's in it we can also do
what's called slicing and so i could
actually do
a print morning array i'm just gonna do
let's just print like the first three
i think this is the slicing let's see if
this works out
yep and so it pretty and it's a
so a slice i'm just saying i want to do
the index uh the zero index up to
three which is zero one two three but
it's not inclusive as you see here so
that's 0 1
2 and here it's not going to include
so that's item 3. if i go to the whole
thing is what zero one two three
four five six seven eight nine so
there's nine there
if i go to 10
because there's not 10 items
then it's going to go all the way out
if i do 12.
it's going to go all the way out so see
when you're doing and
when you're doing a slice it's not going
to let you know
if so if it's essentially if it runs off
the list
so if i were to do uh slicing this array
10 to 12 actually let's do like
something crazy like 15 to 20.
and then we're going to see that that's
an empty array because there is i'm
sorry
make sure i don't so there it's an empty
array it's getting no items back
in the 15 to 20. so this is a great way
to check an index and not have it you
know throw on an
issue or something like that because you
know that if it comes back empty
then we have we've gone out we have an
invalid
index within the array otherwise
i could do if i do 20.
in that case now notice i'm not doing a
slice here
i'm actually trying to grab this
specific item
but in this case i am going to get an
index out of range
so if i slice
if i do a slice and i don't think it
likes it i'm trying to remember if it'll
like that we'll check that real quick oh
it does
so it's going to go out to this range
this is as far as it'll go
or i can start at this item
and so you can see that if i don't have
something on the front and let's do this
let's actually keep
a couple of these before i go two nuts
with them
so here's a couple things
uh and let's put so there's there are
three ways we can do this now these
let's go actually let's do this this
we're actually grabbing the index we
need the indexed
item within the array and so we will get
an error
if this is an invalid index
if we put that colon in there
then it's going to treat it as a slice
whatever is to the left is where we
start whatever is to the right
is where we end if there's nothing there
it takes it all the way to the end
and if the start if there's nothing
there that means it's going to start at
the very first
so this so zero so if we go to four
and we go zero to four what we're gonna
see
is that those two are identical this is
zero to four
i'm sorry this one is a blank to four
this one is a zero to four you can go
either way with those
so that allows you to
within here you could actually and let's
say
let's do a all array let's take this
well let's just do this um all right
equals let's
add these together morning
array plus day array
plus eve array
and then print all right let's see what
happens when we try to add those
together
notice that it appends them all together
and it is in a pen so if i were if i
change this to
day so let's just do this
then notice it's it appends it each of
these isn't a pen so we can append a
raise
let me go back where i was and so now
what we have
is i can actually change around this i
said
if int value in morning array i can
actually do
if it is in
all array morning is going to be
0 to 10 i think is what we did
and i can say if that is in
all array and so 12 is actually going to
be 13.
and then this one greater is going to be
in
all array
and what did i say i said greater than
17. so i'm gonna do that
so actually i can do it this way that's
going to be less than 11
greater than 17. and then i'm going to
do that so let's go play with that for a
second
um yeah we can keep the rest of those in
there
so now if i do that whoops i didn't like
that okay so now if i do enter so if i
do a sit a five
it says good morning if i do
uh 11 it's still good morning
because i did it up to 11. oh because i
don't have a zero
my mistake i got to move that i'm sorry
so if i go up to 10
so that is going to be 12.
uh is that right yeah i think that's
right so let me see and then that's
going to be
uh 16 on
no because that's four so that should be
right okay let's try this
so here's our array and so if we do
um let's do this so let's do 10
it's gonna be good morning because it's
gonna say that it showed up
here uh if we do 12
then it's going to blow up oh
did i miss something there
argument of type is not iterable
oh i'm sorry because it's not it's going
to have to be equals
my mistake because i can't do an n for a
single cell
that makes sense my mistake
and so now it's saying it's not so it is
off okay so let's look at this
so i can go out to 0 to
here so that should be 13
and that starts at 15 and that goes out
to 10. so let's try that again
so if i do 10 it's still good morning
which is right
if i do 11 it's good evening
so i'm not getting that one right
oh because i ended up oh i'm sorry
because this one is actually this isn't
evening it up sorry
sorry sorry sorry let's fix those up a
little bit
so now the fault will be day so now if i
go in the morning
it says good morning
if i go at uh 10 am
oh sorry still morning
but if i go to 11 it's going to be the
day
if i go at noon
let's do
let's make sure i check that real quick
let's print all right
13 because i think i need oh i'm not
going the wrong direction i don't need
13 i need 11
i think is what i need
so now there we go good noon i do
um one o'clock oh
i did one o'clock it's still during the
day
now if i do five o'clock just during the
day because that's going to be the last
one here
because this actually starts at oh
this starts at five o'clock for the
evening
so now five o'clock is 17 if i do 17.
good evening which is 5 o'clock well
if i do 4 o'clock
then it's still during the day so now i
can do this instead
i could actually have which is great if
i want to do things like
checking for items within a weekend or
like i can do here i can slice stuff up
i can look for
ranges of colors and things like that
this really makes it easy for me to play
around with
ranges and arrays it is key
to understand these as we are
as we're looking at certification now
one other thing i
failed to mention before i wanted to
touch on
is when we're building out in
particularly
else's and ifs and when we get into
while loops
sometimes we want to build something out
and so we're going to say i'm going to
come back to it basically so i'm going
to say here
so i don't have night yet so let's say
16 to
we'll do to 9 o'clock so which is what
21
which is actually 20. i'm sorry
and then we're going to say something
here so we're going to say if it's 20 or
beyond
uh let's say 21 and beyond then i'm
gonna do something else
now i'm what i mean to do at some point
is i'm probably gonna say you know good
night
but i can say i don't really know what
i'm gonna do here yet
and so instead i'm going to do a pass
and what that does is it just if i don't
do anything let's do that first if i
don't do anything
then it blows up right away it doesn't
it's not going to run at all because it
needs
something after that i need that
indented block
but if i do pass that basically says i'm
going to come back to this later
and now it allows me to run it
and i'm going to be fine and now i'll
actually get um
so if i do something late at night
i'm going to get actually nothing
because it actually fell all the way
through it came to here
it said hey i hit this and it doesn't
fall the hat
to else instead it says hey i'm here i'm
just going to do a pass on that i'm not
going to do anything
so you can use that to put placeholders
in and still allow yourself to run
through
and you may want to do like a you know
may want to do like a pass
and then you know to do
let's
fill in this code you know something
like that so you may do something like
that at some point
pass is not used a whole lot um but it's
really probably especially in a
a running system but it is a way for you
to
you know put some placeholders in and
have something that sort of
flows through and even to slice and dice
your programs a little bit so outside of
the fact that you need to
you need to at least know that it exists
for the certification point of view
it is something that can prove helpful
in the long run
so that's your basics of arrays now they
do have
vectors within python but that's a
separate package um i'll probably
swing back around and use that at some
point so
although there is uh and we'll probably
swing back around as we're getting into
the while loop we're gonna throw some
vector stuff in
there's really there's not a whole lot
of difference between it other than once
you get in vectors you can do
things that are a little bit different
than the way arrays work
but you can still do multi-dimensional
arrays much like vectors so i could do
um i can do some other i'm not going to
stop i'm not going to step into that
because i
probably need more than a few minutes i
don't want to go too long on this
so i'm going to wrap this one up and
actually hold this as just
really originally you know i'm going to
say
whoops i'm going to say part 1 is array
part 2 is vector so we'll sort of get
back to that later
and we'll call this one a day um this
will as always it'll be out there
in the github repository
take a look you can check out some of
these examples you can play around with
this especially get comfortable with the
with the rays with their indexing and
what they're slicing because you will
see some of that and it'll
probably be most likely you know you're
going to see things like on a test
what this number is making sure that it
is the right one so that you get the
correct
elements in the array i would play with
it a bit and be really
comfortable with those before i get into
the certification
that being said uh we'll let you get
back to it so go out there have yourself
a great day
a great week and we will talk to you
next time
[Music]
you