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.
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.
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.
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
on the screen.
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.
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.
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.
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.
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
sort of, you know, that internal logic we're trying to set some important value are trying to make some decision based upon.
So we're going to set a couple of initial value, so we have something to work with.
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
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.
It's not a function or anything like that. It's just a key word.
Excuse me, Hiccup. There
eso we've got our if statement and then we're going to apply
the conditional statement.
And this here, in these in these ah brackets is just is
a standing sort of a fake variable.
So we're actually gonna replace it with if why
is greater than two?
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.
So why is greater than two
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
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.
And here you can see that it prints the number eight, which is why minus two
So now we can do, for example,
why is greater than Z
But if we want to handle the case where, perhaps
why is less than Z? That's where we apply the life that we learned about. Now I'll remind you that he lives
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
and what's great here is because we have sort of a by motile situation where it is
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.
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
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
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
for just a minute.
We're actually gonna write ourselves a little bit of, uh, a little bit of a strip
them and we're gonna do
We're gonna put our bang line in here.
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.
And with that, you can just right. You know, this is a
and you can see that if you were to write it by itself in Vin because I have contacts on it,
it's going to give it different colors.
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.
So this first function, we're going to evaluate
and the first string is going to s one.
And the second story is gonna be s too.
And what we're gonna do here is if s one
equals s too.
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
plus as to
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.
So again, we're going to give this permissions to allow it to execute.
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.
We're gonna examine if statements as well as the other lessons that are coming down the road.
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,
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.
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.
Thank you, As always were watching this intruder python on cyber Eri on demand