# 2.4 Python Logical Escapes - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or

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