 Welcome back, everyone. My name is Brian. Let's continue our journey with Python three. So in this video, we're going to talk about if else and Ella, what are these three things? Well, it is simple flow control. And when we talk about flow, we're talking about the flow of an application. And this is the basic building blocks for that flow control. Before we dive in, we need to talk about some basic theory. Don't worry, I'm going to keep it super short. We have a variable. Let's call it X. And X is true. A very simple bull. We can turn it on, we can turn it off. Now we're talking about flow control. Think of flow control like when you were a child and your parent looked at you and said, if you do this, then this is going to happen. That's essentially what we're talking about. It's a decision. It's not super hard to wrap your head around, but it does take some explanation. So X equals true. We've got our if statement. And if is going to function on some sort of condition, the condition most of the time will evaluate to true or false. So we're going to say if X equals true, then do something, we'll have some code that'll run if it's true, and we'll have some code that'll run if it's false. Now, this is not always the case. What I mean by that is sometimes this over here simply doesn't exist. You're only going to evaluate for true, but you can do things like say, if it's true, do this, else do that. And it can read like a book. You can literally say, if Bob equals happy, then do something. If Bob is not happy, do something else. It's really that simple. However, you need to understand this is the fundamental building block of almost all applications. This is called flow control, you are controlling the flow of the applications logic. All right, enough theory. Let's dive right in. We're going to look at if conditions. So we're going to create a variable called X. And we're going to say it is true. This is just simply a bull. We've seen this before. Now I'm going to say if you notice how in VS code, it gives us these little templates, we're just going to do normal if we're going to say if X colon. And now we want to print something out. Let's go ahead and run this and see what happens. And it says yes. So our programming logic is X is true. And if X notice how I just said if X, we could have said if X equals true, then it's going to print this out. But it knows it's smart enough to know you don't really need that. Also notice this indent, we've talked about this a little bit before and this colon. So if you're coming from another programming language, this is really what it looks like under the hood. You have these brackets as everybody loves and, you know, C plus plus C sharp JavaScript, all the C style languages, and the code's pretty much indebted. Well, Python does away with that. So all of this gets converted into that and it's much shorter, much easier to read. You don't have all these weird squiggly lines, you got to worry about. Now, you can continue to execute as long as it's on the same indent. For example, X is true if X print yes and again. See, yes and again. Now we are going to say else. And this is where we pop back. You need that right there to tell Python that we've got more coming. And we're going to just simply print. Oh, what's going to happen when we run this? Well, X is true. So if X, and then it's going to run this else, it will run that else is funny because else says, Hey, if this didn't happen, then instead run this code here. Whenever you see these end ends, think of this as a block of code. It's actually called scope. So this would be a scope and this would be a scope. It's a much more complex topic that will cover in a future video, but I want you to be aware that exists. Let's go ahead and run. You see, yes. And again, it never printed. No, I'm going to say help. I mean, just to make sure this really stands out on the screen. Run it again. We never see it. Why? Because this is true. If X then print this out, it never jumps to else. It never jumps to else. So let's change this to false. And you'll get a feeling for how programming logic takes place. X is now false. It's going to say if X print these else print that. So what's going to happen here is it's going to get to X and say, Nope, X is not true. So ignore this block code and instead jump down to else and run this block of code. See, help works as expected. So what we've really been talking about is condition evaluations. And they're pretty much going to be true or false, meaning true, run it or false, don't run it or just do something completely different. Let's take a look at some condition evaluations here. So I'm going to say X equals 100. Why equals 25. So I'm just creating two little variables here and say if, and we're going to say why equals X. We want to tell Python there's more coming. And we're going to print this out. Notice I'm doing this all on one line. You don't necessarily need to do these tab indents. You can do everything on one line if you want to. So we're saying if Y is equal to X notice there's two equal signs. If you do this you're saying make Y the value of X and we don't want that. We want to test for equality here. We're going to say equal to and we can just through the magic of copy and paste speed this up just a little bit here. And we're going to look at our condition evaluators here. So I'm going to say if Y is not equal to and then we can say less than. So if Y is less than X. And I want to say greater than so if Y is greater than what we're really getting at here is you can do some really complex decision making based off values. And they're very simple very easy to understand all you need to do is understand the symbols. For example this guy right here looks super confusing but we're saying if Y is less than or equal to notice how you have this compound here so now this gets really complex and you've got it all on one line but it is super convenient to do that. And you can do the exact opposite you can say greater than or equal to let's go ahead and test this out here so we're going to run this and you can see it doesn't print everything. It says not equal to less than and less than or equal to. So what's going on here we're evaluating Y versus X so we're saying is Y equal to X well it's not they're two different values. So it's not going to run this code. This is what I mean by the condition evaluations will go to true run it or false not run it. There are other ways of doing it but we're keeping things super simple and then we're saying if Y is not equal to X which is going to evaluate to true because those two are not equal then print this out and sure enough we see not equal to. I'm not going to go through each line but you can kind of view and see which one of these evaluate to true for example is why greater than X. Well of course it's not 25 is less than 100 so this will never run and we don't see it down here. Now if we change this value you'll notice the output changes as well now it's not equal but it's greater than and greater than or equal to this is extremely cool and it's extremely flexible. Now you start to understand how computers make decisions it's based off simple if logic if this statement or this condition evaluates to true then run some specific code. All right all of this seems pretty cool but look at this it's kind of a jumble mess it's just this big wall of if statements it's very hard to read there's no indentation and you got to kind of read through every single one to find the one you're looking for. Let's talk about Elev which is really a switching solution and when I say switching other languages have a concept called a switch which is just like think of a big panel of light switches you can turn them on and off. Python doesn't really have that concept there's ways around it but it doesn't really have that specific concept. So let's look at a way of getting around this so I'm going to say X is 10 now we want a bank of switches we want to be able to turn code on and off at will. I'm going to say if X equals 25 then we're just going to print out equals 25. Now we want to take this and kind of smash it into something without doing a completely new if statement we want this to stay all in one line because believe it or not these are two different statements and they'll execute separately. So what we're going to do here is L F X equals 50. So really now it's going to read this as one giant line so it's going to say if X is 25 execute this else if X is 50 execute this and we can just take this concept and keep expanding on it. Go ahead and say if X is 75 and let's go ahead and say 100 we can end this and say you know what if we didn't find any of these switches using the L if statement we can simply say else and then make what's called a catch all which is going to execute if none of these fired off. So it's at 10 let's see what's going to happen here. It triggered our catch all see so it went through and evaluated and says is X 25 no is it 50 is it 75 is 100 else fired off let's switch this to 75 see what happens. You can see now it just says X is 75 so what's going on here is it says evaluate this evaluate this evaluate this turn to true run this code now we're done so it'll jump all the way out. Very simple very easy logic and it looks much better than just this giant wall of text super easy to follow as you dive deeper and deeper in the program you're going to find there's always another way of doing it if you didn't like L if. There is another way and it's called nested or nestled statements and this is basically a statement and a statement and a statement and statement and you can go on forever and ever and ever let's go ahead and take a look. So I'm going to say X equals and let's just pick a number random 82 and we're going to say if X is greater than 50 print and say over 50 but wrap that in some quotes here. And because it's over 50 we want to keep going so I want to say X is well greater than 60. Then and we can use the same logic over and over and over and this now becomes a very repetitious pattern. So for example I just say this and we just grab this and keep going. Notice how the indents get bigger and bigger and bigger. You have to do this otherwise Python considers that a new statement. So for example this would actually create a problem here it's not going to execute the way we want it to. So what we need to do is indent indent and and this is what I love about VS code is it puts these lines where the indent is. So now you can see this is a statement. This is a statement and so on and so on and it gets very very cool very quickly. Now sometimes pasting betrays you and you just simply got to tab it over again the lines will be your guide and literally tell you where you go. So let's say if it's over 90 and I don't want to take this too ridiculous. Let's go ahead and say if X is greater than or equal to 100 and we're just going to end it here print. Let's just say that X stood for some sort of progress. Now we've got some issues here right off the bat. You know saw this little squiggly line here it's saying unexpected indent. So we've got to back this out here and fix our indentations. Oh didn't like that so we can just grab this whole thing and indent it should fix it. There it goes. Save and run. We have another indentation error. Let's see what's going on here. So this is the part of Python that will drive you absolutely bonkers. If you don't have your indentation right it will let you know and you'll get an indentation error. Now that all my indentations are fine. It should run as expected. So which one would you use. Well it depends. So for example in this scenario we have what's called fall through meaning X is 82. So it's going to evaluate true. Jump to the next one true jump to the next one true and jump to the next one true and then it's suddenly fault so it stops executing here. Even though there's more of the statement and you have these lovely little indents love them or hate them that you have to contend with. Remember the indentations tell Python where you are in the programming logic. So some people like L if some people like fall through expect to see both of those out in other people's source code. Choose the solution that works right for you. My personal preference is for L if I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched. And if you want official training I do develop courses out on Udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop courses out on Udemy.com. I'm either working on it or I will actually develop it. I will put a link down below for all three of those and as always help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.