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
[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