>> Hello everyone and welcome back to intro
to Python here on Cybrary on Demand.
I as always I'm your instructor Joe Perry.
If you're watching this video,
that means that you are in Lesson 4, Logical Escapes.
In this lesson, we're going to be
talking about three keywords,
break, continue and pass.
I want to address real fast that
logical escapes is my term for these keywords.
You will see it referenced
in other places a lot of times,
you might see it referenced as
advanced flow control or as loop control.
I call them logical
escapes because that's what they really are.
They're logical additions to
your code that will allow you to
perform some additional functionality.
But don't be confused if you see them referenced as
something else in a different video or
a different lesson or different course.
I just wanted you to be aware that that's what these are.
Break, continue and pass.
We already saw break in
our For and our While loop videos.
Because of the fact that break is
such an important keyword
that it's really impossible to talk
about those loops effectively
without understanding how break fits in.
But we're going to go over it again here just
to make sure that it's sunk in.
To do that, first we're going to construct our list l1.
We're going to give it all the numbers between 1 and 10.
We're going to say for i in l1,
if I modulo 3.
Now, don't get too wrapped around
the axle by what I just wrote here;
i modulo 3 means i divided by 3,
give us the remainder.
For example, and I'll
delete this code real fast and show that to you.
Ten modulo 3 is 1,
11 modulo 3 is 2,
and 12 modulo 3 is 0.
All you're doing with modulo is you're
dividing and then you're giving back the remainder.
So 1 is 1, 2 is 2,
3 is 0 because 3 is evenly divisible by 3.
We've got our list, l1 for i,
i in l1 if i modulo 3 is 0,
if that number is evenly divisible by 3,
print i and break.
Now again, this looks just like some code
that we've written before where all that's
happening here is if
you reach the condition that you're looking for,
if that if statement ever evaluates to true,
you're going to print
that value and you're going to break out of your loop,
which sure enough, of course, is going to happen
because 3 is divisible by 3.
Therefore, that's where we're going to reach it.
But what if we wanted
to find all of the numbers divisible by 3,
and we didn't want to break out
of our code when we did that.
Now alternative, for that we could perhaps do for
i in l1 if i modulo 3.
That'll run through the code and
not break out of it because
it's just going to evaluate the truth every time.
But we could also do it a slightly
different way and we're going to use it.
It's a slightly contrived example,
but we're going to use it to demonstrate
the second keyword, which is continue.
We have l1 here, and we're going to say for i in l1,
if i modulo 3 is not equal to 0,
continue, else print i.
This implementation we're using.
What we're doing here is we're saying for
any situation in which i is not divisible by three,
we're going to continue and what continue means
is skip the rest of this loop.
Don't worry about it. This is
not the value we're looking for.
What you would use continue
for is when you're doing something like this,
where you're trying to apply logic
to all of the items in a set,
so long as they meet a certain condition,
so long as they are what you want them to
be for that particular piece of logic.
Continue is a really great way of sort of
short-handing out a lot of code and saying, "Okay,
this isn't the value we're looking for,
just skip on to the next loop and
don't evaluate the rest of this code."
Here we only have an else statement that follows it,
but it's totally possible and for a lot of for-loops,
it will be the case.
But there could be tens or
even hundreds of lines of code beneath that,
that if you don't use continue,
even though you're not
actually going to be using any of that code,
it will still get evaluated
and it will slow down your program.
Continue is a great way of short-cutting
that and saying, "No,
you don't meet the condition I'm looking for,
skip this loop and start the next one."
It's similar to break in the way that
break stops the execution.
It's different in the sense that it
only stops it for that loop.
It lets you keep trying with the rest of the numbers.
We can see that demonstrated here,
where we get all three of the numbers between 1 and 10,
which are divisible by 3.
Let's break and continue.
The last keyword is a little bit wonky.
It's not actually used for escaping or controlling loops.
The last keyword is pass.
For i in l1,
if i modulo 3 is 0,
pass, print i. I'm
just going to run this and show you what it
does and we'll talk about it afterward.
Here you can see it's printing
every single number between 1 and 10.
Even though we have a conditional in place,
i modulo 3 is 0,
pass says, "Don't worry about the code right here.
Just move on to the rest of this function,
move on to the rest of this loop".
The reason why you use pass is
as a stand-in for other code.
You could use it, for example,
if we were building out a menu
for all of the items divisible by 2,
multiply them by some number for
all the edits divisible by 5,
do something else, and we didn't know
yet what we want to do with a specific condition.
Pass is a great way of telling
the program ignore this condition,
for now, I'm still working on
code for it or there's something
else going on down the road.
You'll see pass in a lot of
code because there are implementations that are
put into it that are sort of
potential extensions but aren't actually written yet.
But pass doesn't actually change
the execution of the code.
It just allows you to put in
a placeholder for what you're
going to add to your code later.
Those are the three keywords of
Logical Escapes for Python loops,
we've got break, continue and pass.
That's all we're going to really be
talking about in this lesson.
Hopefully, you understand
them a little bit better and you're
able to really implement these for and while loops.
The next lesson, which is going to be
the last lesson before the first lab,
is going to be Python Functions.
We've talked about Python functions,
we've alluded to them, I've used them in front of you.
Now finally, you're going to
get to learn how they're made,
how they're used, and how incredibly powerful they are.
Thank you all for watching this.
As always, I have been your instructor Joe Perry,
and I just want to thank you again for watching Intro to
Python here on Cybrary on Demand.