Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

Video Transcription

00:00
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
00:12
in this lesson, we're gonna be talking about three key words. Break, continue and pass. I want to address real fast. That
00:19
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
00:39
don't be confused if you see the reference to something else in a different video or a different lesson, a different course.
00:44
I just wanted you to be aware that that's that's what these are so break continuing past we already saw Break
00:48
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.
01:08
***,
01:08
We're gonna say four I
01:11
in out one
01:14
If I
01:17
modular three
01:22
now, don't get too wrapped around the axle by what I just wrote here I module 03 means
01:26
I divided by three. Give us the remainder s o, for example, and I'll believe this Kodoroff *** and show that to you.
01:36
10 module 03 is one
01:38
11 module. 03
01:41
is two and 12
01:44
modular, 30
01:46
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.
01:56
So we've got our list a 14 I
02:00
I in little one
02:02
If I
02:05
module 030 If that number is evenly divisible by three
02:09
prints, I
02:12
hand
02:14
great
02:15
again. This looks just like some code that I've written before. We're all that's happening here is
02:17
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.
02:29
But what if we wanted to,
02:30
uh,
02:31
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
02:39
in l one
02:42
if I
02:44
module 03
02:52
and that'll run through the code and not break out of it because it's just going to evaluate the truth every time.
02:55
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.
03:05
So have l one here. When you say four I in l one,
03:08
if I module 03
03:12
it is not equal to zero.
03:15
Continue
03:19
else
03:22
print.
03:23
All right,
03:23
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.
03:38
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
03:51
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
04:10
that if you don't use continue
04:12
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.
04:24
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.
04:39
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
04:46
so four I in L one
04:49
if I modelo
04:51
three
04:53
zero
04:55
pass
04:58
print
04:59
and I'm just gonna run this and show you what it does, and we'll talk about it afterward
05:02
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
05:11
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.
05:16
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,
05:35
I'm still working on code for it. Or there's something else going on down the road
05:40
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.
05:46
But past doesn't actually change the execution.
05:50
Change the execution of a code. It just allows you to sort of
05:54
put in a placeholder for what you're gonna add to your code later.
05:58
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
06:10
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,
06:28
and I just want to thank you again for watching Intro the Python here
06:30
on Cyber Eri on demand.

Up Next

Intro to Python

This is an introductory course on Python for cyber security, giving students the ability to understand the basics of the language, solve problems with scripts and identify useful Python modules.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor