📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Pass, Arrays and Slicing

2021-07-01 •Youtube

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
0.46

[Music]

26.24

well hello and welcome back

27.84

we are continuing our season where we're

30.24

working on python

32.64

and certification essentially and what

35.36

we're going to do

36.719

today is we're going to sort of continue

38.32

a little bit in the vein of what we were

40.16

working on before so we've got a little

42.16

if statements and we're going to keep

44

these but we're going to play around a

45.84

little bit more

46.8

this time around so

49.92

what we can do is uh

53.12

first i want to work on some hooray some

55.199

arrays uh and we may not we'll see how

57.44

far we get into this so

58.879

let's go just start here with an array

62.64

so

64

uh we're gonna do

68.159

here is an array example

72.88

so we're gonna do uh my array

77.2

equals and we're just gonna do one two

80.96

three four five six seven

84.84

eight nine

87.759

ten well let's do this let's do eight

89.84

nine

90.799

ten and actually we're going to call

93.52

this

94.56

morning array because we're gonna work

96.079

with this for a little bit

98.96

and this is going to be day array which

101.84

will be 11

103.28

12 13 14 15 16

106.399

and 17.

110.479

and then we're going to do evening array

116.32

which is going to be 18 19

121.6

20 21 2

125.36

33 24 because i'm gonna play around with

128.08

these a little bit

130

so if we look at the let's move these it

131.84

probably doesn't like the

133.04

space there

136.64

so let's just do this first we're gonna

138.08

do print

140.48

uh morning array

143.84

and we'll just look at that first

150.16

where is there we go ah let's do this

154.959

let's get rid of some stuff here we're

156.56

gonna do python three i think it's a ray

158.56

there we go

161.12

so array vector uh didn't see i don't

163.12

think we have the input

164.56

yeah so we get uh oh we did put the

168.4

input first okay i'm sorry so if we do

170.4

that

171.12

so we can see that it prints out the

172.959

array much as we had it

174.959

so printing an array pretty

176.319

straightforward

178.08

now one of the things we can do is we

180.159

can actually

181.36

check for a value so instead of doing

185.599

the morning here we can actually say if

187.599

it is in

190.319

morning array

196.84

then

200.239

oh let's do it uh something like eight

203.28

or seven so here note that it's saying

206.879

good morning because

208

seven is in the morning array so we can

211.12

actually

212.239

you know building an array pretty simple

215.68

we can

218.959

check what's in it we can also do

222.319

what's called slicing and so i could

225.36

actually do

226.239

a print morning array i'm just gonna do

230

let's just print like the first three

233.519

i think this is the slicing let's see if

235.439

this works out

238.84

yep and so it pretty and it's a

241.84

so a slice i'm just saying i want to do

245.599

the index uh the zero index up to

248.959

three which is zero one two three but

251.76

it's not inclusive as you see here so

253.76

that's 0 1

254.56

2 and here it's not going to include

258.32

so that's item 3. if i go to the whole

261.04

thing is what zero one two three

262.8

four five six seven eight nine so

264.8

there's nine there

267.04

if i go to 10

270.08

because there's not 10 items

273.28

then it's going to go all the way out

277.12

if i do 12.

282.88

it's going to go all the way out so see

284.639

when you're doing and

286.96

when you're doing a slice it's not going

289.759

to let you know

291.28

if so if it's essentially if it runs off

293.68

the list

294.639

so if i were to do uh slicing this array

296.96

10 to 12 actually let's do like

298.4

something crazy like 15 to 20.

303.84

and then we're going to see that that's

304.8

an empty array because there is i'm

306.16

sorry

306.88

make sure i don't so there it's an empty

308.88

array it's getting no items back

310.639

in the 15 to 20. so this is a great way

313.84

to check an index and not have it you

317.28

know throw on an

318.32

issue or something like that because you

319.52

know that if it comes back empty

322.8

then we have we've gone out we have an

325.039

invalid

326.16

index within the array otherwise

329.68

i could do if i do 20.

334.8

in that case now notice i'm not doing a

337.36

slice here

338

i'm actually trying to grab this

339.199

specific item

341.199

but in this case i am going to get an

344.16

index out of range

345.759

so if i slice

349.759

if i do a slice and i don't think it

351.12

likes it i'm trying to remember if it'll

352.16

like that we'll check that real quick oh

353.6

it does

354.32

so it's going to go out to this range

356.72

this is as far as it'll go

358.639

or i can start at this item

364.88

and so you can see that if i don't have

366.24

something on the front and let's do this

368.319

let's actually keep

369.199

a couple of these before i go two nuts

371.28

with them

372.319

so here's a couple things

375.36

uh and let's put so there's there are

378.24

three ways we can do this now these

380.479

let's go actually let's do this this

384.08

we're actually grabbing the index we

386.16

need the indexed

387.36

item within the array and so we will get

389.52

an error

390.56

if this is an invalid index

394.24

if we put that colon in there

397.68

then it's going to treat it as a slice

400.96

whatever is to the left is where we

402.88

start whatever is to the right

404.96

is where we end if there's nothing there

407.28

it takes it all the way to the end

409.36

and if the start if there's nothing

411.84

there that means it's going to start at

413.28

the very first

414.08

so this so zero so if we go to four

418

and we go zero to four what we're gonna

421.199

see

423.039

is that those two are identical this is

425.039

zero to four

426.319

i'm sorry this one is a blank to four

429.68

this one is a zero to four you can go

432.319

either way with those

434.639

so that allows you to

438

within here you could actually and let's

441.199

say

441.52

let's do a all array let's take this

445.039

well let's just do this um all right

448

equals let's

448.8

add these together morning

451.919

array plus day array

456.319

plus eve array

460.24

and then print all right let's see what

463.759

happens when we try to add those

464.96

together

468.319

notice that it appends them all together

472.72

and it is in a pen so if i were if i

474.72

change this to

477.84

day so let's just do this

484.479

then notice it's it appends it each of

486.879

these isn't a pen so we can append a

488.56

raise

489.36

let me go back where i was and so now

491.84

what we have

494.16

is i can actually change around this i

497.199

said

497.52

if int value in morning array i can

499.84

actually do

500.56

if it is in

503.599

all array morning is going to be

507.759

0 to 10 i think is what we did

512.479

and i can say if that is in

516.959

all array and so 12 is actually going to

519.68

be 13.

522.56

and then this one greater is going to be

524.8

in

525.839

all array

529.279

and what did i say i said greater than

532.24

17. so i'm gonna do that

535.519

so actually i can do it this way that's

537.519

going to be less than 11

539.12

greater than 17. and then i'm going to

542.88

do that so let's go play with that for a

544.16

second

546.48

um yeah we can keep the rest of those in

548.959

there

549.68

so now if i do that whoops i didn't like

552.48

that okay so now if i do enter so if i

554

do a sit a five

556

it says good morning if i do

559.36

uh 11 it's still good morning

564

because i did it up to 11. oh because i

567.36

don't have a zero

568.959

my mistake i got to move that i'm sorry

571.279

so if i go up to 10

573.04

so that is going to be 12.

580.32

uh is that right yeah i think that's

582.32

right so let me see and then that's

583.44

going to be

586.08

uh 16 on

590.16

no because that's four so that should be

591.2

right okay let's try this

594.08

so here's our array and so if we do

597.839

um let's do this so let's do 10

602.64

it's gonna be good morning because it's

604

gonna say that it showed up

606.16

here uh if we do 12

613.12

then it's going to blow up oh

616.56

did i miss something there

622.399

argument of type is not iterable

627.519

oh i'm sorry because it's not it's going

629.44

to have to be equals

631.76

my mistake because i can't do an n for a

633.92

single cell

635.12

that makes sense my mistake

638.72

and so now it's saying it's not so it is

640.64

off okay so let's look at this

642.079

so i can go out to 0 to

646.88

here so that should be 13

652.24

and that starts at 15 and that goes out

655.04

to 10. so let's try that again

659.279

so if i do 10 it's still good morning

662

which is right

663.12

if i do 11 it's good evening

667.6

so i'm not getting that one right

671.12

oh because i ended up oh i'm sorry

672.959

because this one is actually this isn't

674.72

evening it up sorry

675.76

sorry sorry sorry let's fix those up a

679.279

little bit

680.64

so now the fault will be day so now if i

683.6

go in the morning

685.2

it says good morning

689.279

if i go at uh 10 am

693.519

oh sorry still morning

697.2

but if i go to 11 it's going to be the

700.079

day

701.36

if i go at noon

706.88

let's do

711.2

let's make sure i check that real quick

713.6

let's print all right

720.24

13 because i think i need oh i'm not

722.24

going the wrong direction i don't need

723.44

13 i need 11

724.88

i think is what i need

728.88

so now there we go good noon i do

732.399

um one o'clock oh

736.079

i did one o'clock it's still during the

738.24

day

740.399

now if i do five o'clock just during the

744.32

day because that's going to be the last

745.76

one here

749.44

because this actually starts at oh

752.56

this starts at five o'clock for the

755.279

evening

756.639

so now five o'clock is 17 if i do 17.

759.92

good evening which is 5 o'clock well

763.2

if i do 4 o'clock

767.279

then it's still during the day so now i

770.24

can do this instead

771.519

i could actually have which is great if

773.6

i want to do things like

775.92

checking for items within a weekend or

779.04

like i can do here i can slice stuff up

781.279

i can look for

782.8

ranges of colors and things like that

786.959

this really makes it easy for me to play

790.399

around with

791.839

ranges and arrays it is key

795.2

to understand these as we are

799.44

as we're looking at certification now

801.36

one other thing i

802.56

failed to mention before i wanted to

804.079

touch on

805.92

is when we're building out in

808.24

particularly

810.639

else's and ifs and when we get into

812.56

while loops

814.959

sometimes we want to build something out

817.04

and so we're going to say i'm going to

818.079

come back to it basically so i'm going

819.76

to say here

824.72

so i don't have night yet so let's say

826.56

16 to

829.519

we'll do to 9 o'clock so which is what

832.32

21

833.839

which is actually 20. i'm sorry

837.04

and then we're going to say something

838.32

here so we're going to say if it's 20 or

840.839

beyond

843.36

uh let's say 21 and beyond then i'm

846

gonna do something else

847.279

now i'm what i mean to do at some point

849.36

is i'm probably gonna say you know good

850.8

night

851.519

but i can say i don't really know what

853.519

i'm gonna do here yet

854.959

and so instead i'm going to do a pass

858.079

and what that does is it just if i don't

860.399

do anything let's do that first if i

861.92

don't do anything

864.72

then it blows up right away it doesn't

866.56

it's not going to run at all because it

867.92

needs

868.48

something after that i need that

871.12

indented block

872.399

but if i do pass that basically says i'm

874.72

going to come back to this later

876.32

and now it allows me to run it

880.88

and i'm going to be fine and now i'll

882.48

actually get um

884.16

so if i do something late at night

887.519

i'm going to get actually nothing

889.199

because it actually fell all the way

890.72

through it came to here

891.839

it said hey i hit this and it doesn't

894.48

fall the hat

895.44

to else instead it says hey i'm here i'm

897.279

just going to do a pass on that i'm not

898.639

going to do anything

900.399

so you can use that to put placeholders

902.16

in and still allow yourself to run

904.24

through

904.959

and you may want to do like a you know

907.12

may want to do like a pass

909.199

and then you know to do

912.32

let's

916

fill in this code you know something

917.519

like that so you may do something like

919.44

that at some point

921.12

pass is not used a whole lot um but it's

923.92

really probably especially in a

926.24

a running system but it is a way for you

929.04

to

929.6

you know put some placeholders in and

931.519

have something that sort of

932.8

flows through and even to slice and dice

935.68

your programs a little bit so outside of

937.12

the fact that you need to

938.56

you need to at least know that it exists

940.32

for the certification point of view

942.24

it is something that can prove helpful

944.639

in the long run

947.92

so that's your basics of arrays now they

950.32

do have

951.44

vectors within python but that's a

954.88

separate package um i'll probably

959.12

swing back around and use that at some

960.8

point so

962.72

although there is uh and we'll probably

964.959

swing back around as we're getting into

966

the while loop we're gonna throw some

967.04

vector stuff in

968.72

there's really there's not a whole lot

971.68

of difference between it other than once

973.12

you get in vectors you can do

975.199

things that are a little bit different

976.48

than the way arrays work

979.04

but you can still do multi-dimensional

981.04

arrays much like vectors so i could do

984.72

um i can do some other i'm not going to

986.88

stop i'm not going to step into that

988.079

because i

988.639

probably need more than a few minutes i

989.92

don't want to go too long on this

991.92

so i'm going to wrap this one up and

993.759

actually hold this as just

996.32

really originally you know i'm going to

999.36

say

1001.839

whoops i'm going to say part 1 is array

1006.079

part 2 is vector so we'll sort of get

1009.44

back to that later

1011.36

and we'll call this one a day um this

1013.839

will as always it'll be out there

1015.44

in the github repository

1018.8

take a look you can check out some of

1020.24

these examples you can play around with

1021.6

this especially get comfortable with the

1024.24

with the rays with their indexing and

1025.76

what they're slicing because you will

1027.679

see some of that and it'll

1029.039

probably be most likely you know you're

1030.88

going to see things like on a test

1033.36

what this number is making sure that it

1035.28

is the right one so that you get the

1036.799

correct

1037.6

elements in the array i would play with

1039.039

it a bit and be really

1040.959

comfortable with those before i get into

1042.72

the certification

1044.24

that being said uh we'll let you get

1046

back to it so go out there have yourself

1047.76

a great day

1048.4

a great week and we will talk to you

1051.039

next time

1052.31

[Music]

1067.84

you