Hello, everyone. And welcome back to intro to Python here on Cyber Eri on demand I, as always in your instructor Joe Perry. And if you're watching this video, that means that you're in less than four logical escapes
in this lesson, we're gonna be talking about three key words. Break, continue and pass. I want to address real fast. That
logical escape is my term for these key words. You will see it referenced in other places a lot of times, right? See it referenced as advanced flow control or is loop control. I call. I call them logical escapes because that's what they really are, is they're They're logical at their logical additions to your code that will allow you to perform some additional functionality. But
don't be confused if you see the reference to something else in a different video or a different lesson, a different course.
I just wanted you to be aware that that's that's what these are so break continuing past we already saw Break
in our foreign are wild loop videos because of the fact that break is such an important key word that it's really impossible to talk about those loops effectively without understanding how break fits in. But we're gonna go over it again here just to make sure that it's kind of some kid. So to do that first, we gonna construct our list. L one We're gonna give it all the numbers between one and 10.
We're gonna say four I
in out one
now, don't get too wrapped around the axle by what I just wrote here I module 03 means
I divided by three. Give us the remainder s o, for example, and I'll believe this Kodoroff *** and show that to you.
10 module 03 is one
11 module. 03
is two and 12
All you're doing with module is you're dividing and then you're giving back the remainder. So one is 12 is 230 because three is evenly divisible by three.
So we've got our list a 14 I
I in little one
module 030 If that number is evenly divisible by three
again. This looks just like some code that I've written before. We're 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 gonna break out your loop. Sure enough, of course, is gonna happen because three is divisible by three. Therefore, that's where we're gonna reach it.
But what if we wanted to,
find all of the numbers divisible by three and we don't want to break out of our code When we did that now, alternative for that, we could perhaps do for I
in l one
and 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 gonna use it. It's a slightly contrived example, but we're going to use it to demonstrate the second key word, which is continue.
So have l one here. When you say four I in l one,
if I module 03
it is not equal to zero.
So this implementation we're using what we were doing years were 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.
And 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
so continue is a really great way of sort of short handing out a lot of code and saying, OK, 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 l statement that follows it, but it's totally possible. And for a lot of four loops, it will be the case. But there could be, you know, 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. It will slow down your program, so continue is a great way of sort 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 breaks 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, and I could see that demonstrated here where we get all three of the numbers between one and 10 which are divisible by three.
So it's breaking continue. The last keyword is a little bit wonky. It's not actually used for escaping or controlling loops. The last keyword is past
so four I in L one
if I modelo
and I'm just gonna run this and show you what it does, and we'll talk about it afterward
so you can see it's printing every single number between one and 10 even though we have a conditional in place I module 030
past 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 passes as a stand in for other code so you could use it. For example, if we were building out a menu for all of the items divisible by two, multiply them by some number for all the editors divisible by five. Do something else and we didn't know yet. What we wanted to with a specific condition passes a great way of telling the program. Ignore this conditional for now,
I'm still working on code for it. Or there's something else going on down the road
so you'll see pass and a lot of code because there are implementations that are put into it that are, you know, sort of potential extensions, but actually written yet.
But past doesn't actually change the execution.
Change the execution of a code. It just allows you to sort of
put in a placeholder for what you're gonna add to your code later.
Those are the three key words of logical escapes for Python. Luke's got break, continue and pats. That's all we're gonna really be talking about in this lesson. Hopefully, you understand them a little bit better, and you're able to really implement these four and wild loops
the next lesson was gonna 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 the Python here
on Cyber Eri on demand.