📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - While, For, else, break, continue

2021-07-06 •Youtube

Detailed Notes

This episode takes a look at loops using while and for along with the else, continue and break commands.

Helpful links: https://www.w3schools.com/python/python_while_loops.asp https://www.w3schools.com/python/python_for_loops.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
our series actually where we're looking
at
uh working our way towards python
certification
and this episode we are going to focus
on wilds
and fours so we're going to get into
some loops
now we're going to keep moving somewhat
forward with what we had before
uh what as far as the morning the day
and the evening because it's going to
give us some things to work with
we're going to change our greeting over
though now uh and our greeting is going
to be
something because we're gonna actually
start walking through a few of these
and the way we're gonna do this is first
we're gonna do a while and whiles much
like ifs are gonna be while
some expression and then there's gonna
be a semicolon or a colon
and then you're going to end it and
you're going to handle the eye the while
there's no
end while or anything like that um
let's do it from so we can do the input
so we're going to do a little count
such this while count is less than
value then we're going to do
string count
uh well actually let's just do
yeah we don't need this right let's just
do that this will be easy enough so
let's do string
count because we actually are going to
be pulling those values anyways
and we're going to do account equals
count
plus one because we're gonna start at
zero
and so we're gonna then display based on
the hour
and so let's just do that real quick so
if we do that and we do four o'clock
uh oh i'm sorry it's because
uh value is a string so let's do int
and then we're gonna see that it counts
up because while count is less
than whatever the value value is four
it's gonna go one two three four
well at zero is less than four
and so it's going to say so when it says
it's actually count as zero
it's actually here you're going to do
the greeting for one o'clock
and then when we get through to count is
equal to
three three's still less than four
but it's going to count here it's gonna
do four which is here
but now count is four so it's not less
than it's equal to it's not less than
the value then it's just going to bail
out
so that's the first part one of the
things we can do with a while
is they do have an else and this
basically
is going to say um let's just do
here we just say print envelope
and we're going to see this let's do
this first in our first one
so do the same example we did before and
we're going to see that it does
print end of loop so even though it goes
through the while like an if
unlike an if and an if if you have an
else
the if either it gets executed otherwise
the else gets executed in a while the
else
is going to get done anyway so it's
always going to execute
this part so if i come in and i do a
zero as my number it still does end of
loop
even though zero is not less than zero
so
it's going to completely skip that but
else is uh sort of like uh
like a catch-all if you use an exception
or something like that this is something
that's
always going to execute which can be
very valuable with some of the loops
that we use
there are those instances where we still
need to do
something even if we don't run through
it
we see this when we do in particular
like an interface
we may have something where we want to
um
set a value that's going to be you know
based on the values
based on what's built up in the loop or
something like
you know no values returned or something
like that so
while an else is very useful there
and let's see
the thing that's important here is this
is a boolean
so uh if i do the same thing let's just
sort of go with this for a second and
this is going to be an
infinite loop infinite
loop
and let's just do this print
i'm just gonna do a dot
and if i do while true this is always
true
so i'm never gonna see end of loop in
this case i'm gonna have to break out of
this
it's just entering my just printing my
little
periods there if any while falls
then it's never going to do it so it
just gets into oh
end of that loop
and knit it's hard to type fast in the
infinite loop so let's go look at that
again
oh and we'll see here so end of infinite
loop so even though it's false it just
right away jumps to the else
so this is a boolean that we're working
on
now we can too
we can break out
and so what we can do here is even
though it's an infinite loop the first
time through
it hits a break and you see that end of
loop
uh except for notice here i'm sorry
i'll make sure clear so here's that
first look but the infinite loop notice
it prints this period break
actually skips the else so that is the
one situation if you're going through a
loop and you say wait i do want to skip
the else part
i can break out i can also break a loop
at any time
and just jump to basically this next
line
by doing a break
so i can do a break i can also do
let's see let's do
let's go back to this one uh we're going
to reset our count
well counts less than value and then
he's just going to print
count
count equals count plus one
and let's do this um
i'm gonna do another one print
oh i want to do count
and then uh let's do
print is a
big number
and so here i'm going to say if count
less than let's say five
i'm going to do continue which is
another thing we can do
and so let's see how this one works so
let's enter 10 as our number
and what am i missing here uh oh
i don't even know here i got to reset my
account my mistake
so if we come in here notice that here
for one two three four
and five it's not so i get to five so if
it's less than five
it doesn't get that as a big number
because what happens is i do continue
continue just goes right back up to the
while loop so break
gets me totally out continue gets me
back up
and we're going to see this if we use
other loops for example
the 4.
so now i can go back to what i was
originally looking at so i can do
let's do morning array so i can do x
in morning
array and i'm just going to print
x
oops
oops there we go so if i do that one
no matter what the number is is see here
so this is where
my morning array is is it just goes
through and prints
each of these items if i do oh i don't
want to do a break point if i do a day
array
then notice i'm going to do
11 through 17 because it's just grabbing
the items out of that
collection so i can do that
if i do colors um
i think i've got one somewhere where i
had colors
still those
and there we go
let's do colors
and if i do for x and color colors
then it's going to go through each of
the items and prints those out in order
now i can also do
another nice little thing i can do for
x in range and let's do
five let's just do print x
so let's look at our range here let's
see what that does
so if i give it a range of five it does
zero one two
three and then four
if i do um ten
it's gonna go through zero through nine
there you go like that now another thing
we can do with our range
is
we can come in and we can do a
we can actually give it like a range
that's not zero two so let's say
let's go back to five let's just change
this to be
oh
let's do x uh just to make this a little
easier
plus
is in the range we'll do it like that
and so
let's go back to this one so if we look
at it for uh five
just straight up
we get those four numbers but we can
also do
three comma five so in the range from
three to five
and this is going to give that first
number
and then less than so we see here
and it's probably easiest to see that
the second number minus the first number
is how far we're gonna go
so we're gonna have two items come out
of this range it starts with this
and it's going to be less than not less
than or equal to this
so if we were to do 0 to 5
we're going to see it's exactly the same
as doing
just a five but if we do one to five now
we're not going to see that zero in
there
see now we only see one two three four
so we can use this all over the place to
do
uh to walk through strings or arrays or
things like that
there's some other things that we'll
find later on that can be thrown
into this but generally speaking uh
fours
much like wiles very similar
is that we have four it's got that
little expression and it's usually going
to be
you know something to walk through and
it's you're going to get a value out of
it so
some variable in and then whatever that
range or set is give it the colon and
then of course it's indented
and let's go check what happens if you
do an else
and a 4 just to sort of run through this
in a for loop
is we're going to see much like a while
if you do an else it's there
if i do a break
then uh same thing
is it prints that one but then it does
not do
it goes right into that next loop it
does not actually
do the um the else if you do a four else
and
continue does the same thing so if we
want to do something like this
then we can do that within a for loop as
well as
a while loop so there's a lot of
similarities between them but it's
important to remember that a while
is going to be while either true or
false so it's got to be something that
evaluates to a boolean
whereas a 4 is going to be
something in a range and so you have
counting loops if you want to do a
counting loop
if i want to count for what i would do
maybe somewhere else i can a c
if i want to do you know four
you know x less than five x plus plus
blah blah blah which you'd normally see
so it's
it's like in a java or c
where you would do like an x uh
whoops x less than
five x plus plus
if you do something like that
then that's going to be simplified i
guess
it's going to be this in the world of
python
uh we will use these again so you'll
this is not the only time we're going to
see foreign while loops
but i figured it's you know good to get
started we'll focus on these a little
bit
and like i said we will come back around
as always
you can see the code examples you can
see those out in the github repository
see what's in there and
that'll do it for this one so we'll come
back next time around and we're going to
continue moving forward on our
certification
so as always go out there and have
yourself a great day
a great week and we will talk to you
next time
you
Transcript Segments
0.46

[Music]

26.32

well hello and welcome back

27.76

we are continuing our season where we're

29.679

our series actually where we're looking

31.439

at

32.239

uh working our way towards python

34.16

certification

35.6

and this episode we are going to focus

37.52

on wilds

38.96

and fours so we're going to get into

40.8

some loops

42

now we're going to keep moving somewhat

43.76

forward with what we had before

45.92

uh what as far as the morning the day

48.079

and the evening because it's going to

49.36

give us some things to work with

51.52

we're going to change our greeting over

52.879

though now uh and our greeting is going

54.719

to be

56.32

something because we're gonna actually

57.44

start walking through a few of these

59.68

and the way we're gonna do this is first

63.039

we're gonna do a while and whiles much

64.799

like ifs are gonna be while

66.88

some expression and then there's gonna

69.36

be a semicolon or a colon

70.799

and then you're going to end it and

72.24

you're going to handle the eye the while

74

there's no

74.72

end while or anything like that um

77.92

let's do it from so we can do the input

80.72

so we're going to do a little count

83.28

such this while count is less than

86.4

value then we're going to do

92

string count

97.6

uh well actually let's just do

102

yeah we don't need this right let's just

103.2

do that this will be easy enough so

104.399

let's do string

105.759

count because we actually are going to

107.28

be pulling those values anyways

110.399

and we're going to do account equals

112.88

count

113.6

plus one because we're gonna start at

116.399

zero

116.88

and so we're gonna then display based on

119.84

the hour

121.119

and so let's just do that real quick so

123.119

if we do that and we do four o'clock

125.92

uh oh i'm sorry it's because

129.759

uh value is a string so let's do int

138.64

and then we're gonna see that it counts

140.239

up because while count is less

142.08

than whatever the value value is four

144.8

it's gonna go one two three four

146.64

well at zero is less than four

151.2

and so it's going to say so when it says

152.959

it's actually count as zero

154.48

it's actually here you're going to do

155.519

the greeting for one o'clock

159.12

and then when we get through to count is

161.44

equal to

162.4

three three's still less than four

165.44

but it's going to count here it's gonna

166.64

do four which is here

168.48

but now count is four so it's not less

170.48

than it's equal to it's not less than

173.2

the value then it's just going to bail

175.44

out

177.12

so that's the first part one of the

179.12

things we can do with a while

180.959

is they do have an else and this

183.519

basically

184

is going to say um let's just do

187.599

here we just say print envelope

196.4

and we're going to see this let's do

198.56

this first in our first one

200.319

so do the same example we did before and

202.4

we're going to see that it does

203.519

print end of loop so even though it goes

206.159

through the while like an if

208.64

unlike an if and an if if you have an

211.04

else

212.4

the if either it gets executed otherwise

215.68

the else gets executed in a while the

218.4

else

218.879

is going to get done anyway so it's

220.72

always going to execute

222.159

this part so if i come in and i do a

225.36

zero as my number it still does end of

229.599

loop

230.08

even though zero is not less than zero

233.04

so

233.28

it's going to completely skip that but

234.879

else is uh sort of like uh

238.72

like a catch-all if you use an exception

240.64

or something like that this is something

242.08

that's

242.4

always going to execute which can be

245.28

very valuable with some of the loops

246.72

that we use

247.68

there are those instances where we still

249.92

need to do

250.799

something even if we don't run through

253.599

it

256.16

we see this when we do in particular

258.239

like an interface

259.44

we may have something where we want to

262.079

um

263.6

set a value that's going to be you know

266.479

based on the values

267.68

based on what's built up in the loop or

270.16

something like

270.96

you know no values returned or something

272.72

like that so

274.16

while an else is very useful there

280.84

and let's see

284.08

the thing that's important here is this

286.479

is a boolean

287.759

so uh if i do the same thing let's just

291.919

sort of go with this for a second and

294.96

this is going to be an

295.759

infinite loop infinite

298.8

loop

305.36

and let's just do this print

310.479

i'm just gonna do a dot

314.56

and if i do while true this is always

317.759

true

318.8

so i'm never gonna see end of loop in

320.72

this case i'm gonna have to break out of

322.84

this

324.4

it's just entering my just printing my

327.68

little

328.08

periods there if any while falls

334.32

then it's never going to do it so it

336.72

just gets into oh

338.24

end of that loop

341.36

and knit it's hard to type fast in the

345.759

infinite loop so let's go look at that

347.039

again

348.72

oh and we'll see here so end of infinite

351.84

loop so even though it's false it just

353.28

right away jumps to the else

354.96

so this is a boolean that we're working

356.96

on

358.4

now we can too

362.56

we can break out

368.56

and so what we can do here is even

370.319

though it's an infinite loop the first

371.759

time through

372.88

it hits a break and you see that end of

378.84

loop

380.16

uh except for notice here i'm sorry

383.36

i'll make sure clear so here's that

384.8

first look but the infinite loop notice

386.639

it prints this period break

390.16

actually skips the else so that is the

392.88

one situation if you're going through a

394.24

loop and you say wait i do want to skip

396

the else part

397.68

i can break out i can also break a loop

400.8

at any time

402.24

and just jump to basically this next

404

line

405.28

by doing a break

408.4

so i can do a break i can also do

416.24

let's see let's do

421.36

let's go back to this one uh we're going

423.759

to reset our count

428.8

well counts less than value and then

432.08

he's just going to print

435.12

count

438.4

count equals count plus one

444.639

and let's do this um

449.199

i'm gonna do another one print

455.36

oh i want to do count

459.52

and then uh let's do

462.84

print is a

465.84

big number

470.8

and so here i'm going to say if count

474.24

less than let's say five

478

i'm going to do continue which is

479.36

another thing we can do

483.199

and so let's see how this one works so

484.8

let's enter 10 as our number

489.52

and what am i missing here uh oh

494.8

i don't even know here i got to reset my

496.24

account my mistake

499.28

so if we come in here notice that here

502.319

for one two three four

503.84

and five it's not so i get to five so if

506.72

it's less than five

508.879

it doesn't get that as a big number

510.319

because what happens is i do continue

512.32

continue just goes right back up to the

514

while loop so break

516

gets me totally out continue gets me

519.36

back up

520.24

and we're going to see this if we use

521.76

other loops for example

524.32

the 4.

527.839

so now i can go back to what i was

529.6

originally looking at so i can do

532.16

let's do morning array so i can do x

535.36

in morning

538.48

array and i'm just going to print

541.68

x

546.839

oops

549.279

oops there we go so if i do that one

553.519

no matter what the number is is see here

555.68

so this is where

556.64

my morning array is is it just goes

558.16

through and prints

560.399

each of these items if i do oh i don't

563.04

want to do a break point if i do a day

564.399

array

567.68

then notice i'm going to do

570.72

11 through 17 because it's just grabbing

572.8

the items out of that

574.88

collection so i can do that

578.24

if i do colors um

581.68

i think i've got one somewhere where i

583.44

had colors

585.68

still those

590.48

and there we go

594.959

let's do colors

598.32

and if i do for x and color colors

605.519

then it's going to go through each of

606.72

the items and prints those out in order

610

now i can also do

613.519

another nice little thing i can do for

617.12

x in range and let's do

620.399

five let's just do print x

628.24

so let's look at our range here let's

629.68

see what that does

631.519

so if i give it a range of five it does

634.8

zero one two

635.92

three and then four

639.2

if i do um ten

643.04

it's gonna go through zero through nine

646.24

there you go like that now another thing

648.72

we can do with our range

650.48

is

653.519

we can come in and we can do a

656.56

we can actually give it like a range

658.56

that's not zero two so let's say

660.8

let's go back to five let's just change

663.68

this to be

667.2

oh

670.32

let's do x uh just to make this a little

674.88

easier

676.56

plus

679.92

is in the range we'll do it like that

682.8

and so

683.44

let's go back to this one so if we look

684.72

at it for uh five

686.64

just straight up

689.68

we get those four numbers but we can

692.079

also do

693.2

three comma five so in the range from

695.04

three to five

697.04

and this is going to give that first

698.959

number

701.44

and then less than so we see here

705.279

and it's probably easiest to see that

707.04

the second number minus the first number

708.72

is how far we're gonna go

710.959

so we're gonna have two items come out

712.32

of this range it starts with this

715.44

and it's going to be less than not less

717.44

than or equal to this

719.519

so if we were to do 0 to 5

725.68

we're going to see it's exactly the same

728.24

as doing

729.519

just a five but if we do one to five now

732.48

we're not going to see that zero in

733.68

there

734.88

see now we only see one two three four

738.959

so we can use this all over the place to

740.88

do

742

uh to walk through strings or arrays or

744.399

things like that

745.44

there's some other things that we'll

746.48

find later on that can be thrown

748.48

into this but generally speaking uh

751.04

fours

751.6

much like wiles very similar

754.639

is that we have four it's got that

756.8

little expression and it's usually going

758.32

to be

758.88

you know something to walk through and

761.04

it's you're going to get a value out of

762.399

it so

763.2

some variable in and then whatever that

766.24

range or set is give it the colon and

769.279

then of course it's indented

771.519

and let's go check what happens if you

773.92

do an else

777.76

and a 4 just to sort of run through this

782.32

in a for loop

789.12

is we're going to see much like a while

791.279

if you do an else it's there

793.2

if i do a break

798.839

then uh same thing

802.16

is it prints that one but then it does

804.72

not do

805.519

it goes right into that next loop it

806.88

does not actually

809.68

do the um the else if you do a four else

813.92

and

815.12

continue does the same thing so if we

816.8

want to do something like this

818.32

then we can do that within a for loop as

821.6

well as

823.12

a while loop so there's a lot of

824.24

similarities between them but it's

826.399

important to remember that a while

828.32

is going to be while either true or

831.199

false so it's got to be something that

832.72

evaluates to a boolean

834.32

whereas a 4 is going to be

837.76

something in a range and so you have

839.92

counting loops if you want to do a

841.279

counting loop

842.959

if i want to count for what i would do

845.04

maybe somewhere else i can a c

846.48

if i want to do you know four

849.6

you know x less than five x plus plus

853.44

blah blah blah which you'd normally see

856.32

so it's

856.8

it's like in a java or c

860.079

where you would do like an x uh

863.6

whoops x less than

866.639

five x plus plus

875.36

if you do something like that

878.48

then that's going to be simplified i

882.32

guess

882.72

it's going to be this in the world of

884.88

python

886.16

uh we will use these again so you'll

888.24

this is not the only time we're going to

889.36

see foreign while loops

890.56

but i figured it's you know good to get

892.24

started we'll focus on these a little

893.44

bit

894.16

and like i said we will come back around

896.8

as always

897.68

you can see the code examples you can

899.279

see those out in the github repository

901.92

see what's in there and

906.399

that'll do it for this one so we'll come

907.68

back next time around and we're going to

908.8

continue moving forward on our

910.16

certification

911.44

so as always go out there and have

913.6

yourself a great day

915.04

a great week and we will talk to you

917.839

next time

934.959

you