2 hours 57 minutes
Hello, everyone. And welcome back to enter the python here on Cyber eri on demand I, as always in your instructor Joe Perry.
And in this lesson today, we're going to be learning about if statements that lesson three programming basics. So lesson three, our objectives are gonna be understand and define the term flow control understanding. Apply if statements and learn the components of if statements. Now this has a slightly python bent. The inclusion of the lift, which we'll talk about in a minute,
is something that Python uses that not all of the programming Lee, which is used
but this isn't going to be actual pipe on code yet. It's just going to be bent toward
so Flo Control. All flow control, actually is Is execution decisions made based upon some internal logic.
I want to say that again, and I'm gonna say it again. Throughout this course, it is an execution decision made based upon some internal logic
program. For example, my perform differ operations based on the keys you press. Almost all programs do award processors going to print the letter of the key you press or the symbol if you use shifted in front of capital
things like that.
That's a great example of flow control, because it's not something people generally think of as flow control, flow control, something that and it's it's a weird phrase to keep saying over and over again. But it's something that people often get kind of wrapped around the axle with. Get a little bit confused, don't really understand.
And really, it's because it's being overcomplicated.
The simple fact of the matter is that any decision your program makes based on some internal logic,
you know, some some evaluation of truth or falsehood
that's all flow control, that's all it is. And so don't you know, don't get too far into the weeds on it, just understand that it is a decision your program is making.
So if statement specifically in this lesson going back to the Boolean logic we talked about in our earlier lesson,
if is implied in all of those statements, right, all of those statements can be framed and generally are framed as if that
if aid was one and B equals one, then eh and vehicles one
or if a equals one or B equals one,
then A or B was one
It's really easy to understand. It's not really even understand. That's not a fair thing to say. It's more. It's very straightforward. It's all your doing with If is you're just using to evaluate a condition,
you'll often hear this referred to as a conditional and in the in the help sheets and help documentation that we provide.
It is referred to as a conditional state.
So some examples, if sort of from everyday life. If it is raining where a raincoat. If the subways late yell at the sign, that doesn't help. But if you've never lived in Washington, D. C. I don't know how to tell you how much better it makes you feel to shout at. A subway sign has done nothing wrong,
but it gets out a little bit of your aggression. And in D. C. That's very, very important.
If the Number X is divisible by three print yes to the screen. That's amore programmatic example. That's something that's going often show up in different in different
program, different tools
having menus that are based on a control number that is actually a number and in danger of some kind.
It's a very, very common paradigm used to control a program.
You'll see the graphic above me is a floater flow Church are a great way of trying, of learning to understand if statements, I'm aware that's probably pretty hard to read. That's OK. It's actually just an ex K seedy comic I mentioned before and I'll mention again. You're going to see a lot of those in this series and then basically everything I do because X K. C. D is glorious.
e lift. The next statement is part of the if paradigm right we have. If 11 else Elif is a little bit less common like I mentioned, it's It's used in python. It's using some other languages, but not used in all of
the way he lived. Works is it is actually its own. If statement, it's sort of ah, hybrid of else. And it
if the first condition. So whatever you're If statement WAAS evaluates to false, then consider this condition and evaluated.
We're just to say that, for example, if
for the menu example I gave him a moment ago,
if the number is one
states calculator, if the number is one, perform the ad operation.
If l. If the number is to perform this attract operation,
you lift the number is three so on and so forth. The idea here is just that Elif is an if statement that on Lee gets checked in the case that the proceeding, if statement was or the preceding the lift statement was false. Now you can have many, many E live statements in a row. There's not really a serious upper limit on it, though. Generally speaking, you're gonna want to avoid
too many of them just for code readability.
The final pair of the final part of this paradigm is the L statement. So else on Lee ever exists in the context of and if you live
it exists also for a type of Luke, but we'll talk about that later. The point is, it only exists in the context of evaluation of truth or falsehood, determination, right of flow control.
If the previous valuation was false, then performed this operation. Now, unlike Elif, there is no new conditional being evaluated with else. It's simply if a
perform his behavior
else in all other cases, performing different behavior.
So that's that's really all there is to if you live in else. But we're gonna take a couple of examples here and make sure that we kind of understand it can break this down.
So here are the facts of the situation.
X is a number.
If that number is less than 10 you will print small
e lift. That number is greater than 100. You will print large else you will print medium.
We're gonna try it out with a view. Example x. In this first case, we're going to set equal to 999. Now, if that number is less than 10 if is going to the first day when we evaluate always in most programs within certain contexts,
your statements are going to be evaluated from top to bottom.
Functions will mess without a little bit, and we'll talk about why and how later. But generally speaking, you're going to evaluate from top to bottom. But even in cases where that might be messed up for some reason, you know you got complex go twos or whatever. If Elif and else will always be evaluated in the order of if the living else And if you don't have an if you can't have a Neil if Orrin Els. So if
that number is less than 10 print small Well, 999
course is larger than 10 so we're not going to print small that has evaluated to false. Therefore, we're going to consider the second statement
Hell, if that number is greater than 100. Print large. Well, the last time I checked, 999 is larger than 100. Therefore,
we print lurch.
Now what happens if X equals 15?
Well X. If that number is less than 10 we print small 15. It's still larger than 10
he lived. That number is greater than 100. Print large.
Well, it's smaller than 100 so we can't perform that operation either.
Else. Print medium.
Now again, there's no new evaluation, We're saying if it's some set of numbers, if it's within somewhere, simply if neither of the previous two conditions are met,
just print medium.
We have X equals one.
If the number is less than 10 print small. Of course, that's gonna be our first evaluation because that's the first line we check. And sure enough, it is small.
So that's all there is for less than three in this lesson. Again, we talked about the concept of flow control, which is which is an execution decision based upon some internal logic. Then we went ahead and we talked about if statements if you live in else and we understood the way that those three statements fit together and how they work,
join me in the next lesson, which is going to be discussing four loops that'll be less than four kind of worked out did not mean to do that, actually did not realize that that worked out that way until just now. I have been recording this course for two weeks, and I just realized the unintentional pump
I'll see you back in less than 44 loops. As always. I'm your instructor, Joe Perry, and thank you for joining us on Cyber eri on demand
This intermediate Python course uses and builds upon the materials from Intro to Python, using ...
2 CEU/CPE Hours Available
Certificate of Completion Offered
Introduction to Programming Using Python
Introduction to Programming Using Python is a practice exam that will test basic programming functions ...