 Welcome back everyone. Today we're going to be talking about conditional statements in Python. So just like before, I have my notepad up. This is just a type of notepad basic text editor. You don't need anything fancy. And then I have a command prompt also open. So command prompt or terminal depending on what you're using. So in the command prompt, we will run our Python program. And then we will write our Python program in notepad in this case. So if you haven't seen the first video, I had a Python script already written and we talked about assigning variables. So that was the first lesson. And we created a file with a dot py file extension. So this is just a normal text file with a dot py file extension. And that just tells us that this is a Python script. Okay, so let's go ahead and I'll take a look at the first lesson again. So we can kind of review. So the first thing we did is use the print command. And then we printed everything inside the quotes. So this is printing a string Hello Python. So this would display on the screen whenever I run the program. The next thing we're doing is assigning a variable. So we have name and name equals, this tells us that this is a variable called name. So whatever is on the left side is the variable, whatever is on the right side is the contents that we are putting into the variable. Okay, so we are putting a string Joshua inside the variable name. So now name equals Joshua. Okay, so then we're doing another print command, and we're printing a string Hello. And then we are concatenating with this plus sign, the variable name, notice that there's no quotes around name, if there were quotes around name, then that would also be a string and it would say Hello name. However, since there's no quotes around it, and we have a variable called name, then whatever the value of name is will show up here. So this would say Hello, Joshua. And then we would concatenate again, and then add an exclamation point at the end. Notice that there's double quotes around the exclamation point. So quotation in programming really makes a difference. Make sure you're watching your quotes. Normally when I have bugs in my code, it's because I forgot a quote somewhere. And then it takes a long time to find the quote. So just be aware about quoting anything that has quotes needs a quote at the beginning and end. Don't leave one of them off. And then anything inside the quote, at least for now, we'll say anything inside the quote is a string. Okay, now notice we have another variable. So we have x equals, we have x equals one here. Well, one is not in quotes. So x equals one, this would be considered a integer. Okay, so x is equal to an integer. And the difference is with, we talked about this a little bit, whenever I use the plus symbol with two strings, I combine the strings together and kind of like a sentence. So I just put the two, you can think of it like putting two words together. So I'm putting hello and Joshua together. So you get hello, Joshua, it doesn't do math on it, it just does concatenation. But if I have x equals one, and I have y equals two, and these are integers, not strings, then I would actually do math on them. So I would have two, for example, two plus one, or two times one, and then I can, I can use that to do math. And then we can also print straight integers. But if you combine strings and integers while printing, you have to do something called casting, and we'll talk more about that later. So that's what we talked about last week. So let me clear that. And this week, we're going to talk about conditional statements. And in basically almost every programming language that I know of, conditional statements are how you start to do logic, how you make decisions in your code. So it's a really powerful concept that you need to get, and you can apply it to pretty much every programming language. So the first thing I want to do, or the most basic kind of conditional statement, is an if statement. So if, sorry, I need to talk about this, we've already set a variable x equals 10. Notice 10 is not in quotes, so it is an integer. So we're treating this as a number, number 10. So we go back to our if statement. So let's say we want to check if x equals 10. So if x equals 10, then I need to do some action. So if x equals 10, I need to do some action. If 10 is x is less than 10, maybe I need to do a different action. Or if it's greater than 10, I need to do something else. So this is really common where we're looking for the states of variables. So in this case, we can say if x equals 10, and I'll talk about that in a second, then do something. And I'll just do prints because we've already talked about prints. x is 10. So what do we have here? The first thing you might notice is a little bit strange. We have if, well, that makes sense, just if something is true. So whenever you say if it's raining, then I will use an umbrella. So if raining equals true, then use umbrella. Well, here I'm doing and then we have our condition. So if this condition is true, then do the next line. Now Python is a little bit strange in the fact that it really cares about the spacing and the lining and the line positions. So normally, in other programming languages, you might have something like if x equals 10, then do something. And then there might be brackets around it and things like that. For Python, you do not do that. So we just do if x equals 10 and then the two dots, and then print x equals 10. So we have our if, which is just saying, I want this conditional, I'm going to check something. We have our variable that we're checking and we have two equal signs. And I'll come back to that in a second. And then we have the state that we're checking the variable is in. If this line is true, then we go to the next line. If this line is false, we skip this next line. And that's the basis of all of the logic, right? So we're checking something. If it's true, do the next action. If it's not true, skip over the next action. And that seems very simple, but it's a very powerful concept. So let's go back to these double equal signs. Remember before, I said x equals 10 is a variable assignment. So if we had, for example, x equals 10 here with one equal sign, then this would also be, or it would be confusing because it would look like a variable assignment. So whenever we're doing conditional checks, we do equal equal. So this evaluates to true or false. It doesn't assign 10 to x. But if you just have one equal sign, then it does try to do, assign the value of 10 to x. So whenever you're doing conditional statements, make sure that you use the double equal sign. If you want to check if something is equal. Now, there's lots of other ways or lots of other different checks that we can use. And I'll talk about those in a second. But first, let's go on to something else. So let's say if x equals equals 10, print x is 10. So let's go ahead and try that. So again, I'm going down to command line and I'm typing Python because I have Python installed. And then I'm going to do zero zero two conditional. Now, x equals 10. And we're checking if x equals 10. So what should happen? Well, we should get some text out that says x is 10. And that's what we get. So x is 10. Okay, now what if we say x equals equals 11. Now this check is false because x equals 10, we're checking if it's 11. So we check and then nothing prints. Well, why does nothing print? This was false. So we do not go and work on the next line, we actually skip to the line after that. Okay, so if this is false, skip to the next line. It's basically the idea. Okay, so let's go ahead and go x equals 11, keep that the same. Now, it's very often. All right, so what happens if I want to know if x is 11. And if it's not 11, then I want to do something all the time. So kind of the catch all, if you always need to do something except in a particular circumstance, you can use if else. Okay, and what this means, let's say x is not 11, because that's the only thing we're checking for. So here we have if x equals 11, which it doesn't, so that would be false, we're going to skip this line. And then if else like anything else that's true, like if we didn't, if we did skip the this line, so this was false, then print x is not 11. So let me go over that again. If x equals 11 is false, we skip the next line and we go to else. And then we print x is not 11. So let me show you that. So we should get x is not 11. Okay, x is not 11 because we skipped the print x is 11 line and we went to x is not 11. Now what happens if x is 11? What do you think would happen? Do we print this line x is 11, because that would be true. And we also print x is not 11. Well, that doesn't seem right. Right. So let's go ahead and try that. x is 11. I changed this variable here. So now x is 11. And this was true. So we printed this line, and then we skip everything else. Right. So with if statements, the first thing that is true is what runs. So if you have, I'll show you another another case here in a second, but if you have many if statements put together, the first if statement that's true, the next line will run and then it will forget it won't run everything else. And then that's how you can build the logic into your program. Okay. So since we're already here, let's say I have if x equals 11, then print x is 11. If not, x is not 11. Okay. Let's change this a little bit. x is something else. Okay. So let's say I want to check for more than just 11. Maybe I want to also check if x is 20. Okay. Well, I can add an additional statement in here instead of rewriting, let's say, instead of doing something like this, where I just have if statements all over the place. Well, that looks really bad and it's a lot of extra code that we don't need. So what we can do instead that minimizes the code a little bit more is using an else if. So notice we have if to start the conditional statement and then if that's false, else is the catch all. If everything else was false, then else will always run. Okay. But I want to do another check for some condition. I want to check if x equals 20. So I can do else if and that looks like L if L if and then we just give our conditional statement like we did before. So L if x equals equals 20. Remember, I'm doing a conditional statement. So I have to use the double equal sign. And then I need to hit enter and then at least give a little bit of space. So that way, Python knows that this is the second line. So if you have everything lined up at the beginning, Python won't like this because it can't tell what should run it thinks that these two statements are equal. But we're trying to say that this print statement is underneath this if statement. Okay. So we have these spaces here in the space at the beginning is very important. So L if x equals 2020. So then I would print x is 20. Okay. And then we get to our L statement. So now let's run through this. So x currently equals 11. If we run this, what should we get x is 11 x is 20 or x is something else? Well, let's run it and see. So of course, like we expect x is 11 because this is true. And so we print x is 11 and then we do not run anything else. We just skip over everything else because we've already met our condition. Okay, so now let's do x equals 20. So what do we expect to happen? x is 20 because I went to the first if statement x equals equals 11. That's false. So I skip over this print statement. And then Lf x equals equals 20. This is true. So I print the next statement. Okay, so x equals 20. And then since that was true, I don't do a check for this last elf statement. Okay. So now let's set x equals 21. And what happens? x is something else because I went to x equals 11. This was false. So I skip over the next line. Lf x equals 20. That's false. So I skip over the print. And then I got down to the last statement and that's everything else. So x is something else. It's not 11 and it's not 20. So it's something else. Okay. So these L statements catch everything. Okay. If we remove this L statement, both of those would be false. So we would get no output. There's no text showing. Okay. All right. So that's about if statements. So let's move this back a little bit just to make it a little bit easier. The most basic form of conditional check would be if and then one condition and then no else statement. Usually you have some sort of else statement, but it really depends on what you're trying to do. Okay. So the first thing I want to talk about here, we have if and then we've already talked about if. This is else if. It always comes after an if statement. And then at the end, the catch all is else. So if everything else was false, else will always run. Okay. Okay. So there's a couple different things that we can do in terms of our conditional checks. There's a lot of them. I'll just talk about the basic ones today. One of them is we're right now we're saying x equals equals 11. So we're saying that x, we're checking that x actually equals exactly 11. But what if we don't care if x exactly equals 11? What if we want to know if x is greater than 11? Well, we can just use the greater than sign. Because in this case, we're just doing math, right? So because we have an integer, we can actually check if x is greater than 11. Okay. So I'm just going to change this so it makes sense. True. And then I'll put an l statement. Okay. So you can kind of see what's going on. If our statement is true, it'll print true. If our statement is not true, then it'll print false. Okay. Hopefully that makes a little bit more sense whenever we're running this code. So here I have x, the checking if x is greater than 11. Well, x equals 21. So it is greater than 11. So we should get true. And that's what we get. Okay. Now, what if x equals 11? So x is greater than 11. Is that true? No, that's false, right? Because x is not greater than 11. It is 11 is equal to 11. So if we want to know if something is greater than or equal to, we can use the greater than symbol with an equal symbol. And then we get this x greater than equal to 11. And that makes a really big difference because let's say you're scripting something and then you're looking for anything above 10, including 10. Well, you could just say greater than nine, or you could say greater than equal to 10. So just be aware that if it's greater than it really means greater than it doesn't mean equal to you have to tell it exactly what you want to do. Okay. So x greater than equal to 11, and x is currently 11. So let's run that. And we get true, because of course that's true, because it's equal to 11. Okay. Now, if I do 10, that it's not greater than or equal to 11. So we get a false. Okay. Right. So, so far, we've talked about just equals. I want to know if x is exactly this number. We have just greater than, which is just everything above 11, we have greater than or equal to, which is 11 or above. We can also do less than. So right now x is 10. So we can say is x less than 11. And if we run that, we get a true because it is less than 11. If we do less than equal to 11, we get a true because it's we're checking for 10. So less than or equal to 11 is still 10. 10 is still less than or equal to 11. Okay. So you can do greater than equal to you can do greater than less than equal to less than. And then another one that I like quite often is not equal to. So just like we had equal equal, if you use a exclamation point or in programming, they usually call it a bang, we can do x not equal to 11. So in this case, x is 10 is x not equal to 11. Well, that should be true. And we get true, right? So x not equal to 11 is true. So I use not equal to actually quite often whenever I'm doing different types of programs, but you can use anyone that you're comfortable with. It's much easier to start with equals at the beginning, but it really depends on what the condition is checking for. Remember, the condition is what you're checking, but then you do some action afterwards, right? So maybe if x equals 11, then I want to, you know, move Bitcoin from one account to another account or something like that, right? So I want to do some action when that state is true. Right now, we're just printing. But this is where we'll put code later that actually does a lot of actions. Okay. So those are the different types of checks. And I'll just kind of write them out here for you. I am adding a comment. So I have the hash or the pound sign at the beginning, this pound sign will not run as code, it's skipped over by Python, just kind of ignores it. So I can say, this is my note, right? Let me show you what this means for a second. So let me remove the pound sign. You can see that the color changed from blue to black. So if I run this now, I'm going to get an error. So I get an error and the error as at line three, this is my note. Well, what we can do if we want to add notes to ourselves is put a pound sign at the beginning. And then this becomes a comment. So if I run this code again, I get no error. Okay, so we use comments a lot to give notes to ourselves to remember what's going on. So I'm going to give a note about the different types of conditional checks. So this would be less than greater than equal, less than or equal to greater than or equal to and not equal. So these are some of the main types of checks that I use commonly. There's lots of other ways that you can do checking. But at least you should know these first. Okay, if you put a pound before anything, before you're the line of code, then this will be a comment and it will not be ran as code. See, I still don't get any errors. Okay. Okay. I'm going to move that off a little bit. Right. So sometimes you want to check multiple things at the same time. So we've already talked about if and then L if right. So L if x equals 12, do something else. Okay. But sometimes you want to check to like the state of a variable, two conditions of a state of a variable at the same time. Now this gets a little bit complicated. But the basic idea is I want to know if x is say x is 10 now. So I'll say is x greater than nine. And then I also want to know if it's less than 11. Right. Now it's very easy to write that I could just say x equals 10, but just go with me go with me for this example. So let's say x is greater than nine. But I also want to say x is less than 11. Okay. Now the easy thing to do would just say x equals 10. But we can also do multiple checks at the same time. Now what I'm trying to do here is say, I want to know when x is greater than nine and when x is less than 11. So what do you think I can use in the middle of this to do that? Well, just and. Okay. Now what this does is it takes two conditionals at the same time to be true. Right. So this side has to be true and this side has to be true. So x has to be greater than nine, which is true. And x has to be less than 11, which is true. Okay. So let's test that we should get true. Good. Okay. So that means that both of those things were true. Okay. And then we can actually stack these up pretty much as much as we want. So x is not equal to test. Okay. So x is not equal to some string value called test. Let's check that. So I can stack as many ands as I want, but make sure you keep your code simple. In this case, like I said, these two can just break down to x equals 10. So x equals 10 and x is not equal to test. You could probably even just remove this just x equals 10. So make sure that whenever you're writing these conditional statements, instead of stacking all these things up as much as possible, write the simplest conditional check you possibly can that covers all of your cases. Don't get, don't just keep adding more and more. Think about what conditional checks you're doing. Now that being said, you can add as many as you want. Just don't get carried away with it is my point. Okay. Now we have another one. So and is a very popular one that I use quite often. And then another would be maybe I only care if one of these checks are true. So if x is greater than nine, or x is less than 11, I want either one of those things to be true. Well, we can just use four. So let's say x, I'm going to move this from nine to 10. So I have x is greater than 10, but x is 10 right now. It's not greater than equals, it's just greater than. So this is actually false. So if x is greater than 10, or x is less than 11, this is true. But this is false. So what should we get whenever we run the program? True, right? Because one of these this one was was true. Now if we do x is less than 10, then both of those are true are false. So we get false. Okay. So using and you have multiple checks and you need both sides to be true using or if you want one of the conditional checks to be true. Okay. And then whenever you're trying to do something like testing if something is within a particular range. So here we're actually doing a check of a range of x, you can write it like this. So Python has a short code for let's say the range that I want. So in this case, I'm saying, I'm using x one time, but this is a shorthand way to write a range check, or multiple condition checks. So x less than or equal to, sorry, greater than basically it's greater than 10, but less than 20. So x is greater than 10, but less than 20. So that means that anything 10 to 20 would work. So here we have x equals 10. So it's true. If x equals 21, it's false, right? Because it's outside of this range. So if you are doing ranges, you can use a shorthand, Python has a lot of ways to reduce the amount of code you need to write. It has a lot of these kind of shorthands. But it just takes some practice to get used to them. For now, don't really worry about it. If this is your first time working with variables, go ahead and just keep something like x equals 10, or x greater than equal to 10. Okay. So just keep it simple for now. Get used to when something is true and when something is false. Always consider this logic check as true or false. That's how we're evaluating it. Now, if this is true or false, then we can do something fun in the if statements. And we'll talk more about that as we go on. Okay. So that's pretty much it for if statements today. Just build your own if statements and do a couple of different logic checks. Just use printing true and printing false to see how things are working. So keep it simple and have a good time. Thank you very much.