In this tutorial we are going to be learning about the conditional statements available to you in Python. So, we're going to be learning about the if-else statement, and this is basically provides us a way to evaluate if something is true or false, and then perform something whether or not the condition is met. Let's write an if-else statement.
check = True
if check == False: print("It is false") else: print("It is actually equal to True")
This is going to print out "It is actually equal to True" in the console. First, Python is going to check if the variable is equal to false. Else, it's going to continue through the script and print, or perform, whatever we tell it to do. This is great if we only have two conditions, but what if we want to use more than two? Between the if and else we can use an elif statement, and here we can supply an additional condition.
check = Hamburger
if check == False: print("It is false") elif check == "Hamburger": print("Yummm, hamburgers") else: print("It is actually equal to True")
When we run this script it's going check to see if the first condition is true, if not it will check the next condition, if not it will print the else statement. So, that's what an if-statement is, and these are necessary for the project we are about to begin working on.
Alright guys, now we're going to be learning about some conditional
statements in Python. So we're going to be learning about the if-else statement,
and this basically provides a way to evaluate if something is true or false,
or if something is something, or it's not something and can do something different
based on which condition is true. So let's go ahead and let's set "check = False"
and what we're going to do with that is we're gonna say "if check ==
false:" "print("It is false")" and i'm going to save this and this is the minimal you
need for an if statement.
We're going to go ahead and run this and it's going to say it is false because it
is false. Now if we were to change this to true, and run the same code, it's not
going to do anything, and that's because we haven't provided and else. So if it's
equal to false it will do this otherwise it's just going to do nothing.
So let's go ahead and drop back four spaces to where the if statement
began, I guess, and that type "else:" and we're going to "print("It is actually
equal to True")" So let's go ahead and let's run the script and you'll see that
now where this is not the case, check is not equal to false, so then it's going to
do this instead.
Now, what we're going to do is this is good if you want two conditions, but what
if you want more the two conditions? Between the if and else you can actually
use something called "elif:" and here we actually need to supply a condition to
the elif. So think of it like another level to the if statement, and think of the
else as a catch-all like if none of the above are true than just do this. So if
check equals false
it's gonna do that. "elif_check == "Hamburger":" we are going to "print
("Yummm, hamburgers")" and let's throw another "elif_check == "Yo":" and say "print("Hello")"
So let's go ahead and save this, we're going to run it again, and as you can see check is
equal to true. So it says is this true?
Nope, move on. Is this true? Nope, move on.
Is this true? No, move on. Okay, well the else is going to say well if none of the
above is true them we're just going to do this. So what we're going to do right
now is we're going to set this to "check = Hamburger" and we're gonna run the script,
and you'll see what's happening is it's checking this one first,
that's not true, so it moves on its like okay this one's true so let's just do
this and then let's get out of the if statement, so it doesn't check any
further. So that's what an if statement is in Python and that's necessary for
what we are about to do.