Welcome to the section and conditional statements.
Conditional statements form a very important part of any programming language, and a Python programming
language is no exception to this.
Let's see how conditional statements work.
Say you have a condition.
Now, if this condition is verified, we can take an action.
Say E.
If this condition is not verified, you can take another action.
B Suppose we have several conditions that is.
N Different conditions.
If the first condition, condition one is verified and all the other conditions as Condition I where
I is different from one that is condition to right up to condition and unverified.
Then we can take an action to e one else if this condition two is verified and condition one condition
three of the condition and unverified, we can take an action A2 We're going to repeat this right up
to condition n if condition and is verified on condition one right up to condition and minus one unverified,
you can take an action.
N Now if none of those conditions are verified, we can take an action and plus one.
Let's get a more practical example.
Suppose we want to write a computer program which when you input the fact that your do sports is going
to tell you whether you are healthy or not.
We could simply use condition statements.
We have you that if we have spots that is if you are parts of then.
Wish I'd been out healthy.
And if you're not, then we print out unhealthy now.
We have another example where we have this for sports, football, basketball, volleyball and tennis.
Now we want to bring all the action you make whenever you are playing any of those parts.
So if football is very that is if you are a footballer, then you kick before the goal is made else.
If you're a basketball player, you make it true.
If you're a volleyball, you make a hit.
If you are a tennis player, you make a strike.
And if you are none of this, we could see the action is unknown.
So let's go straight away.
It's look at us index of conditional statements and patent.
So yeah, we have a variable sports so we would say sports true now and the session and valuable times.
What didn't make mention of booleans now actually booleans are some sort of variables which take two
values that is true or false.
So booleans can be this true all false all likes in numbers which take values like so and int which
takes values from negative say infinity.
To.
Positive infinity, which are all whole numbers.
We took in an example with Booleans, we just have this two values, so we either have true or false.
Notice that whenever we print out all, whenever we type out as true, it automatically turns green.
And that's because this is a python keyword.
So let's see how to use this in this example.
So we have sports.
True?
I could say that if sports equals true.
We have Prince.
Healthy.
Now we're receiving this error because we haven't used the right operator now for this F statement.
We have to use the right operator for this to work out well.
Instead of the equals, we actually have the double equals sign.
So if we replace this with this w cosine, we shall see how this works out.
So that works out now perfectly.
It's important to note the syntax of the F statement we actually have.
If we have a condition we have this column and then present enter would take us to the next line.
Now let's look at another way of presenting this is that I've put in spots to be equals.
True.
What would you say if supports?
Now if you say f sports, you are doing the same as seen if sports equals true.
Now, if we change this true to false, we'll see that this is not going to be printed.
So doing this we have not in print it out.
Now, if we have this, we could say else.
So for this, where we had if verified and now if not verify it, that is else we're going to have print,
not healthy.
Not healthy.
And then we go see that we have printed out not healthy because sports is false.
And so when it gets here, this condition is not verified.
And if this condition is not verified, we think else.
What else is actually the opposite of this?
Now, the opposite of this being verified is this being not verified?
So we could replace this else with an Aleph would have live sports equals false and we should have the
same output.
So this is the same as just do it else.
So that's it.
Now let's take this next example.
We have.
See.
Part.
Equals a list made up of the different spots a person actually practices.
So we have this football.
We have basketball.
We have volleyball.
And finally, we have Dennis.
Now.
We should see.
If so, we could put this sparks.
If.
Football.
In sports.
What's the print?
This person plays football.
I leave?
I was always advisable to put this in brackets so we could do this.
And we have here.
So leave.
Basketball.
In sports.
Prince.
This person plays basketball.
A live.
Basketball and sports.
Prince.
This person plays volleyball.
Finally leave.
Dennis.
In sports we have.
This person plays tennis.
Now we have an area that's changed to volleyball and that's it.
So let's run this and we have this person plays football.
Now, this person actually has four different sports he or she practices.
But in this case, or using this method, we actually print out football only.
And that's because once we get into this first condition, we print out what we have first.
And if this condition is verified, we print this and all this other conditions, Aaron's going to be
looked again.
So what the program does, it becomes and test this.
If this is verified, it does this and all of this, other conditions are looked at.
So to solve this problem where we may have situations like this where someone may have four different
sports or could change the way we look at this.
So let's take this cup of this right here.
Now, I see if.
Football.
We're going to spin this so we could have print.
We could actually copy this.
Carpet it is out.
We have brand is now instead of going into the next condition that's shifting to the next line, we
could actually dess next condition in this F statement.
And that is because if this is verified, we could have the next.
Now we have.
If so, here we have if football and sports.
If basketball.
In sports.
Print out.
We could use it that for us to move safely.
What use in space lives when you're.
Yeah.
Avoid doing this.
Instead use simply the tap to get to this point.
And once we have this, we can print this pleasant place basketball.
But on another point may arise where you are in playing football, but you're playing basketball.
So suppose we have something like this.
So in this case, if football in sports is not going to be verified, so we're not going to get into
this now we have to make a way that if we have another condition, you're going to have another condition
where if you're not playing football, we can get into this impasse.
If you're playing basketball now, let's make this problem a bit simpler.
We don't have this.
In fact, we have.
Just football and basketball.
So, yeah, we have the owls.
Prince.
This person, all right?
I don't even print anything, so.
Yeah, we just test if basketball in sports print.
This person plays basketball.
So there we go.
And we run this.
We have this person plays football and pleasant basketball.
So let's take it with basketball.
We have this person plays football.
Let's take a football.
You have this pleasant place, basketball.
But the issue is, if we increase this number of sports, we could have it becomes very difficult to
work with it manually like this.
Well, I would say volleyball.
Volleyball would have to add this if and in this if we have to do an L statement again and then print
out for the volleyball and come here again and print for volleyball and so on and so forth.
So as we increase the number of possibilities for the sports, it becomes very complicated at the court.
And that's where Lupe's come in, and that's what we shall see in the next section with Lopez.
We shall see how to solve this problem very easily.
So let's get back over there with this two for now.
Let's remove this reload.
Everything works fine.
Now let's work with some numbers.
So we have this number equals five.
We have B equals seven.
Good test if E equals five prints.
Stillman.
Correct.
Would you say correct.
We could also have if E is less than B prince, correct?
It could also have if is great and b prince.
Not correct.
If E.
You have a model of B, for example, equals one.
Prince.
Not correct.
And we can load the cell.
Loading the cell, we have this first to correct us because equals five and then here we have 11 B,
which is correct.
Now it good and which is not true because is actually less than b we have a modulo B which in this case
is actually equals five and is not equals one.
So bring out not correct.
And since this is now equals one, we have not printed its out so we could test this prin e one of the
b is clearly five which is now equals one.
So we don't get into this now.
What is that?
Would this are a way of writing?
We get into each and every condition and that is because once we have those, if the program looks for
the next.
So if there is C an L statement here, user prints, whatever, no matter what you have here, is never
going to be printed if this condition is verified.
So like this, for example, this whatever is not printed, but when you have this if statement or when
you have this if statement followed by another, if did mean this other estimate is going to be looked
at.
So what a program does is it looks at if it looks at whether it is elseif ELIF or else and if that is
not there, we just move on.
So we'll go to this.
If two must get into this, if that is different from C in this case where we had Elif would leave.
If this is verify, we don't get into this again.
But what if even if this is verified or if it's verified, this other condition is going to be distant.
Now let's look at the lesson I equals condition.
So here we have the lesson equals operation.
So we have this.
Now let's get what we have here.
So we have this correct.
This lesson equals B this is actually correct because this is less than so.
One of the conditions of a verified and this is not correct, this key is not correct.
So now let's suppose this two are the same.
So let's put this two five we have this other one is printed out and that's simply because let's take
this out and that's simply because with a less than equals, we are saying in this case, if is less
on our equals.
Correct.
And this gives this actually equal.
So we have no problem with that.
In this case, it is greater than all equals.
We can do this.
So this is the same as saying if E is greater than B or E is equals B.
So this is the same statement was common this.
So there we go.
Now, we could take this off
and we're on this.
Still have the same output.
We could also have if equals five, replace with if is five.
So we're going to have this and we'll see that it's actually printed out.
Now, let's take this off.
Yeah, we have if is five.
Correct.
So this is another way of writing.
This equals.
So we just does this direct.
Now, if it's not five.
See they're not in the screen yet.
So we could have else prints not in a sprint printed.
So there we go.
Now that we've seen this, we shall go to the lips.