Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

Video Transcription

00:00
Hello, everyone. And welcome back to intrude a python here on Siiri on demand. This is gonna be less in one of our second module. And in this lesson, we're going to be focusing on if Elif and else as they apply to the discipline of Python programming.
00:13
So in this lesson, we've got two primary objectives. We're gonna learn how to apply Boolean logic, how to apply truth and falsehood to Python, and they're gonna learn how to use a properly formatted if statement in python.
00:25
Nevertheless, we watch the other videos. You may notice a difference here, which is that my face is no longer in the bottom right of your screen there. Two excellent reasons for that one. It actually takes a shockingly long time to do my hair in the morning and to a little bit more seriously, because we're doing some actual onscreen programming right now.
00:43
We want to make sure we have all the screen real estate we possibly can. So we're not gonna have I'm not gonna be on screen and very much, and we're just gonna be focusing on the actual code
00:51
on the screen.
00:52
So slightly different format, hopefully doesn't trip you up. If you really, really need to see my face on this, I will. You know, you can photo shop it on and feel better.
00:59
Um,
01:00
but if you really need to see my face in the videos I'm making, I guess. Hi, Mom. I'm glad you're proud of me. That's probably about the only person.
01:07
So like I mentioned in this video, we're gonna be focused on if the lift and else So if l if else is a dynamic as a paradigm that we actually learned in the last module in where we were talking about the concept of flow control which you may remember is an execution decision based upon some internal logic.
01:23
So we're gonna crack into R V M here again, we're going to bun to 64 bit VM. I'm working in Terminator right now, and we're just going to actually start out working in the interpreter. We're not even gonna start with a script.
01:34
What we're doing here when we're using an if statement, what we're trying to do is we're trying to evaluate that
01:40
sort of, you know, that internal logic we're trying to set some important value are trying to make some decision based upon.
01:47
So we're going to set a couple of initial value, so we have something to work with.
02:02
All right, so we have three initial values that X equals tests during y equals 10 and Z equals negative 15. Now, the way we're going to structure this if statement
02:12
is, there's no parentheses, wrong language, eyes by writing the word if all lower case in Python, that's a key word. It's not like a foot.
02:20
It's not a function or anything like that. It's just a key word.
02:23
Excuse me, Hiccup. There
02:24
eso we've got our if statement and then we're going to apply
02:29
the conditional statement.
02:30
And this here, in these in these ah brackets is just is
02:34
a standing sort of a fake variable.
02:37
So we're actually gonna replace it with if why
02:39
is greater than two?
02:42
I remember because this statement is subordinate to our If this is a subordinate clause Dourif statement, we're going to tab in or four spaces, whatever your style is, so long as it's consistent. Focus on that.
02:53
So why is greater than two
02:55
print
02:59
why minus
03:00
two?
03:02
So, in our interpreter, you may have noticed it still hasn't printed anything out. The rebel hasn't actually performed a print. And that's because of the fact that if statements and other statements that have subordinate code
03:13
actually give the interpreter context and say it's not actually time to execute yet get the rest of the code, get the rest of the context and then perform the operation.
03:23
And here you can see that it prints the number eight, which is why minus two
03:29
fantastic.
03:30
So now we can do, for example,
03:34
why is greater than Z
03:38
print Why?
03:39
But if we want to handle the case where, perhaps
03:42
why is less than Z? That's where we apply the life that we learned about. Now I'll remind you that he lives
03:47
is else if it is an if statement that only potentially evaluates if the proceeding, if statement turns out to be false. If Z is greater than why
03:58
Print
03:59
Z
04:00
and what's great here is because we have sort of a by motile situation where it is
04:05
problem. Well, actually, it's not entirely fair, but in general you have by motel situations. You don't necessarily have to have it else. We have to have one here because of the fact that it is possible for why to be greater than see you to be greater than why or Z and y to be equal.
04:27
And we run that. And we see, of course, that 10 is greater than negative 15. But that code gives a great demonstration. Of the three types are the three components of oven. If statement you have If, with your conditional evaluation
04:39
you have the subordinate code that stabbed in, you have else If for Elif with the second possible condition, then you have that subordinate code tabbed in. And then you have your l statement. And again your else statement is if none of the proceeding if statements evaluated troop, if all of the proceeding conditions turned out to be false than just execute this coat
04:57
Now this actually right here is a simple implementation of what's called a max function or a function to find the larger number and one of your exercises in the lab that's gonna be coming up about half time of this module is actually going to be creating this this sort of code. So it's worth looking at and understanding exactly how that works. Now we're gonna step out of our interpreter here
05:16
for just a minute.
05:17
We're actually gonna write ourselves a little bit of, uh, a little bit of a strip
05:23
them and we're gonna do
05:25
if then
05:26
dot
05:30
We're gonna put our bang line in here.
05:35
This is there's also something else that I want to mention that actually, I did not bring up in the syntax video because we didn't have a good cause for it. But Python does have the option of commenting or writing things in your script that don't get executed or interpreted. And the way that's done is by using this pound sign hashtag whatever you wanna call it.
05:53
And with that, you can just right. You know, this is a
05:59
python common,
06:01
and you can see that if you were to write it by itself in Vin because I have contacts on it,
06:06
it's going to give it different colors.
06:10
But that's how you can put comments in your code. Remember when we talked about sort of the design philosophy of python? It's very important to write readable code. So if you're going to create what may have to be a complex function, it's important to have your comments and there's other people understand what that functions supposed to do.
06:25
So this first function, we're going to evaluate
06:29
two
06:30
strings,
06:32
and the first string is going to s one.
06:38
And the second story is gonna be s too.
06:45
And what we're gonna do here is if s one
06:48
equals s too.
06:50
And you could see that I have auto and done and I have some contacts built into my event. So it tab for me if one equals s you or if s one is the same as s too
07:00
print
07:01
s one
07:02
else
07:05
Prince
07:08
s one
07:09
plus as to
07:11
That's two.
07:15
And we mostly stepping out of the interpreter there just so that you could see writing it into a script and so you could get a little familiar with that.
07:20
So again, we're going to give this permissions to allow it to execute.
07:30
No,
07:33
There you can see that it printed those two lines out together because again the conditional evaluated now over to the side, you will see a document says, if assignment, that's just gonna be a whole bunch of example. If statements and you can feel free to peruse that and see if you can figure those out and write some code yourself in the lab that is going to be at the end or at the midpoint of module two.
07:54
We're gonna examine if statements as well as the other lessons that are coming down the road.
07:57
So if you want to get more practice with those, feel free to crack that open and started on those first and then come back and watch for the next videos before you get to that lab,
08:05
that's gonna be the end of this lesson in this lesson. Again, we talked about how Billy and Logic applies to Python the idea of evaluating truth with an if statement. And then we learned how to properly formatted in a statement.
08:16
I as always I'm your instructor, Joe Perry, and I'm really glad to have you all here. I look forward to having you in our next lesson, which is going to be discussing four loops in python. If you have any questions, of course, please feel free to reach out to me or one of your course mentors if you have them.
08:31
Thank you, As always were watching this intruder python 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