10. Logic with if, elif, and else in python
Oct 8, 2023
We need to make decision based off of logical conditions when coding. If, elif, and else statements along with comparison and other logical operators allow us to make these decisions. In this video we show some examples of complex and nested if statements.
In coding very often you need to make a decision something happens under one scenario, but under a different scenario something else happens
So you need to be able to ask if this then do that
Otherwise do this other thing right and so these logical conditions that we need to set up we can accomplish them using if statements, right?
So there's lots of different logical operators that we saw in the last video right equals not equal less than greater than equal to all these
Different things we already know how to apply those operators
We can apply those to individual variables or to conditions such as
Is something in this list if that's true then take this action, right? So that's a membership operator
So lots of different ways we can use these but what's the syntax for actually creating the if statement?
Well, we begin our if statement with the if keyword, right?
Then we put the condition and then we end it with our colon, right?
When you hit enter it will automatically use whitespace to denote that this block of text corresponds to that statement
Let's show you what this looks like in the code. So here in spider
Let's declare a couple variables. First. Let's say that X is equal to four. All right now, let's come down here make an if statement
we're gonna say if X is
Greater than zero, right? We want a certain action to play take place
We're gonna do the colon and hit enter
Notice that it did the four spaces of whitespace and now we can go ahead and say what the action is
Maybe let's print it. Let's say print
Let's see we needed this to be a string. So let's say the number is larger than zero
So we could end our statement right there. That could be all that we have if we run it. Let's see what happens
We're gonna run this it stores the value 4 into X and sure enough we get this thing spit out
The number is larger than 0 great. So what's gonna happen if our number is equal to negative 2?
Well, we're gonna run it
Nothing's gonna happen down here. There's nothing that gets output, right? It's just storing a negative 2 here
So it asked if this was true and since it was false it didn't enter this if statement, right?
And so it never performed this action which corresponds to one condition the condition where that's greater than 0
Something that's cool about these if statements is we can now we can add other things we can say
Elif right that's a new keyword that means else if or if that's not the case else we can make another condition
Let's say that X is less than 0, right?
So that's our new condition if it's less than 0 we want it to do something else
Maybe we're going to print the opposite of this. We're gonna say print. Oops
We got to make sure that we only have 4 see how it did that it took the 4 from before and it included those
But I already had 4 that would be a problem because remember the way that we denote what's happening
And what is it with this white space? We have to be conscientious about what we're doing. So we're gonna say print the number is
Less than 0 right?
So now when we run that
What's gonna happen? It's gonna say oh the number is less than 0 great
So what will happen now if we set X equal to 0 right?
It's a note to do again because now neither of these conditions are met
What's great is we can add this other scenario we can say else and that just means if anything else happens, right?
No matter what else it is
This is what we want it to do it, right? So we're just gonna say now it must be
The number is 0 right if it's not greater than 0 and it's not less than 0 then it must be equal to 0
Okay, so there we go
and we could add as many of these else ifs as possible as you want right you can just keep on adding them and
What happens if more than one is true, well, let's try that out. Let's take this one and let's make another scenario
All right. So this time we're going to do another L if statement this time
we're gonna say if it's greater than 0 and
X is less than 10, right? So how about that one?
so
Sorry needs to be greater. So we're asking first off
It's saying is it greater than 0 in this first block? And if so, we say yes, it's greater than 0
Here we're going to say the number is greater than 0
But less than 10, okay, and then it's the same as before. So what's gonna happen? Let's run and see so first off
We didn't change our value. It's still set to equal 0. So let's set this to equal 4
So now it satisfies these first two conditions
But it doesn't satisfy that one and since it's gonna satisfy one of these it shouldn't flag this one because else is only if it
Doesn't work for any of these other prior ones. So let's try and see what happens
So it says the number is greater than 0 you see that even though this other condition is also true
It doesn't go into it because it takes the first one where it is accepted, right?
So we could change this one. We could say this one we could say and X is
Let's see less than 3
So now we're saying is the number greater than 0 but less than 3 and 4 doesn't fit that criteria
But it does fit the second one
So when we run it, we're going to get that second answer coming out the one where it says greater than 0 but less than
Well less than 10 is the response there. So those are if statements you can see how this work now
Just like you can have nested dictionaries and lists you can have nested if statements
So let's see what that looks like. So let's try and modify this one. Okay, so
Previously we said if it's greater than 0 we wanted it to take an action
Let's make that a little more clear what the action is
Okay, we're gonna make a nested if by tabbing this over right so now that that's part of it
It's going to instead of printing this number. We're going to ask it a question
This thing needs to be part of the else if statement
So now we're going to say all right
If you are greater than 0 and less and less than 10, then we're going to say the numbers greater than 0 but less than 10
but
So we're gonna do another L if oh, sorry, this should be an if statement right here
Now this L if is gonna say
What's our condition here? If you are greater than 0 and you're greater than 10, that's a different scenario
Now we're gonna have it print something else. We're gonna have a print
That must be a big number
So let's try this now with our number being 4 so we enter this first loop
It says is it greater than 0 and is it less than 10?
Well, first off is it greater than 0 it is now it's gonna ask is it greater than 0 and less than 10
So it should flag that so let's try it and it says sure enough the numbers greater than 0 but less than 10
Now let's change this to 14. So it's gonna enter this first loop
So it's greater than 0 this first if statement now, it's gonna ask this next if statement
It says is it greater than 0 but less than 10?
It's not so it's gonna skip to this one and it's going to say it is greater
So we should get this that must be a big number coming out and that's what we get
Okay, so if statements are pretty slick and you can just keep on stacking these if we wanted to do another one
We could just keep on doing these but those are your if statements
It's possible to put the else on the same line. If you have something really short like over here
Let's comment all of this out. All right, so let's let's consider this scenario
Let's just comment this whole thing out for a minute and let's do this
we're gonna say that we have a fruit equal to Apple and now we're going to say is
Apple the variable is Apple equals true if
The fruit is equal to Apple and otherwise, it's going to be false
so let's run this and
we're gonna have a new variable one for fruit which says Apple and then is Apple is now set to true because it did a
It did a logical condition, right?
I asked it said if fruit which is we know stored it to equal Apple as the string equal to Apple
Then it's true else. We're gonna put out false. So that's it can put these onto one line
If you are coding you can't have nothing in an if statement, right? So let's go back to our first example
Let's comment this one out and we will uncomment these ones
So grab all that control one
let's say that we got to here and
If X is less than zero, we want it to do something, but we don't know what we want it to do yet
You can't just leave it blank if we try and run this let's make sure this a negative number and see what happens
So it says unexpected indented block, right?
So it's not liking that even if we get rid of that
It's not liking it. So we need to have something there
So let's we can use the keyword pass pass just means move along. Keep going. Don't do anything now
That should at least work
Okay
So it runs without an error so you can use pass if you don't know what you're gonna put and you can decide to come
Back later and finish it, but this at least won't throw an error. Okay?
Now with your if statements, we've done it where we've shown two
but you could have as many of those as you want right you could have lots of conditions that has to meet right and
Fruit has to be equal to
Apple right? Oh, we forgot to define our fruit. So we're gonna say fruit equals
Apple up here. We'll go ahead and run that and there we go. So that is how you use if statements in Python

