### Intro to Python

Course
Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

### Video Transcription

00:00
Hello, everyone. Welcome back to intrude a python here on Cyber Eri on demand. If you're watching this video, you're watching lesson two
00:08
four and else in Python.
00:10
Now we're gonna have two main objectives in this. We're gonna learn how to use for looks and python. We're going to learn about the very special and very unique relationship between four and else in Python, which is something a little bit different. That's not really something that you'll have seen in a lot of other languages.
00:24
So we're gonna just demonstrate that as before with our if statements were going to open up our interpreter and we're gonna work from that at first,
00:30
I have I have it written in the script that we're gonna I'm gonna show you in a little bit
00:35
for now, we're just gonna try it out of the way.
00:37
So the way if you'll recall we constructed an if statement was it
00:40
conditional?
00:42
And then our code
00:43
before looking into something pretty similar is one before condition
00:49
00:50
So, for example, one of those commonly used types of python Luke
00:54
four eye in range, 10
00:57
print.
00:58
All right,
00:59
now, what's gonna happen here is gonna increment zero through 10 but and this is important. I want to address it. Range in Python
01:06
is non inclusive.
01:07
Which is to say, it's going to be the number 012345678 and nine, but not 10. The reason for that is because we're giving it the argument of 10. The assumption is that you want 10 numbers. Not that you want to actually get to the specific number of 10.
01:21
So we're gonna print, I hear
01:23
gonna see.
01:23
Now you'll recall from our lesson on four loops in the abstract when we're not looking at Python, all that's happening here is that it's setting one. I equal to zero, and then it's just running through that series of numbers until it reaches the condition.
01:36
So here the condition is going to be 10. And of course it doesn't pretend because the actual four loop, if you were to write it into two code would be constructed.
01:44
Four I
01:46
less then 10
01:49
print.
01:51
That's sort of the pseudo code implementation is happening here, the general idea of it. So what we're doing is we're just going through all of those numbers and printing.
01:57
That's the first type, of course. But there is another type that we addressed in our for video in our in our four video in module one.
02:05
And we're gonna clear a screen here just a little bit and clean that up. And the best way to demonstrate that is by creating a list we're gonna say L one
02:12
is a
02:13
be
02:15
see
02:16
d e
02:20
uh,
02:21
she
02:23
and then we're going to perform a four loop against that. So four items in L one
02:28
Now I want to note that item right here is actually not a mandatory word. You can use pretty much any word right there as long as it's not.
02:34
Ah, variable. That already exists in your coat or a python keyword.
02:38
So it could be four.
02:50
Anything like that, Some ridiculous variable name. Generally speaking, you're gonna want to use a simple one just for the sake of
02:57
easy code readability and and understanding what's happening. So usually you'll see, for example, for letter in sentence is a common one
03:06
for four
03:08
number in list. Whatever the idea here, we're just gonna do for item in one
03:15
prints
03:17
item.
03:19
Now, this is very similar for two are four I in range. Blink.
03:23
What's happening here is that we're just saying, OK, this list right here exists. It is some kind of irritable object of list a dictionary. What have you
03:30
and I just want you to go through all of the characters and all the values in that list and perform some logic, which in this case, is just printing that value.
03:38
So we're gonna run that list. We're gonna run that for Look, we're going to see we print A, B, C, D, E, f and G.
03:45
That's the basics of how a four loop works in Python. It's pretty straightforward. It's exactly like what we talked about when we're talking about four loops in the abstract and module one.
03:53
I mentioned, however, that our other objective is learning how to apply else tau four loops and python, and that one's a little bit trickier. For that to work, you need to understand what's called a break statement.
04:02
So again, we're gonna be using L one, which will remember is a B C D E f g.
04:06
We're gonna say four items
04:09
in l one.
04:11
If item is
04:15
well say for this one d
04:17
are you single course just to keep from getting confusing? If item is deep
04:23
print
04:25
found
04:28
sure enough, you can see that it's gonna go through our lists. Going to say is a d know is be No, it's see No is D It sure is. It's worth noting that this key word in Python is works very similarly to the equal sign. But it's not identical so that Luke could also have been constructed as l one
04:46
four item in l one.
04:50
If item equals equals D
04:55
Brink
04:56
found it.
04:57
That's gonna work
04:59
exactly the same way. We're not really gonna talk about the main difference between is and a double equal sign in this video. It's a slightly more advanced concept in Python, so we're not gonna spend any time on it. Anyway, The point is that it will ran through this list
05:14
until it found the character was looking for.
05:16
However, there might be a condition in which all you want to do is find the character you're looking for
05:21
and stop execution. A soon as you found it.
05:25
Four items in l one.
05:27
If
05:28
item is C
05:30
prince
05:32
item
05:34
Great.
05:35
And what break does is kind of what it sounds like. It breaks out of the loop. If you reach a break statement in the loop, it's going to say OK, we're done executing. We have reached our objective. You don't need to examine the rest of this list. You don't need to examine the rest of the possible options.
05:50
So here you can see it returns seat. Now,
05:54
this list isn't large enough for me to demonstrate to you exactly how useful that can be. But understand, For example, if you had a list of items that was, ah, 1000 items long or 10,000 items long, which is actually possible,
06:05
using this break statement would make your code to substantially faster and more efficient.
06:12
Because in fact, you stop execution once you reach what you're looking,
06:15
what you're looking for.
06:16
It's another. We understand what a break Sam it is and what it's used for. We can start to apply, and else
06:28
that's interesting for item
06:30
in l one.
06:33
If item
06:34
it is
06:35
Z
06:38
Prince
06:40
and break
06:42
else, you'll note here, that else is on the same level of indentation is four in the same way that when you use elf what else with an if statement. It goes on that level of indentation.
06:50
So if we were to tab this in,
06:54
it would now be in else that is, evaluating whatever happened in this if statement
06:57
since it, since it is where it is, it's going to evaluate against the four loop.
07:00
Remember that pipe Python is white space delineated
07:04
else? Prince
07:05
No said
07:09
now what's happening here? What else actually does when you use it with a four loop instead of with an If where it would evaluate, Did that condition evaluate? Too true.
07:16
It's that instead, it's going to say was a break statement reached. Did At some point in the four loop did the code reach a break statement? So note that this code code will only reach a break if it finds the letter Z in the list, which, of course, it isn't there.
07:30
So it's going to activate our else,
07:33
and sure enough knows that was found. So for item in a one, it goes a B C D E f g. None of those recede. Therefore, we never break. Therefore, we run our l statement.
07:44
That's all there is to it. That's four loops in python. You can see the relationship between four and else You can see how for looks were used. I also just for real quick demonstration. Do have that written out in this? Let me check the yes
08:00
in four else Fine.
08:01
And you can see here exactly what I just demonstrated that this is actually the letters A through H.
08:07
So
08:09
we do have some exercises for this. If you look over the side, you'll see a list of four loops that you could try and implement yourself. Additionally, if you have access to our insider pro program, you will be able to perform some of these four loops in our next tech lab. If you don't have that, we'll still have these. Simon's there for you to do on your own,
08:26
But I do highly, highly recommend you make use of that lab because it's just
08:30
absolutely spectacular and very, very easy to use. That's gonna be all there is for this video. As always. I'm your instructor, Joe Perry. You've been watching intrude a python here on Cyber Eri on demand

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

Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor