Hello guys.
So we are going to continue the discussion with respect to Python.
And in our previous video we have already completed operators.
And if you remember we have completed one complete section with respect to Python basics, uh, we understood
about variables, data types and operators.
Right now we are going to go towards an amazing new topic in Python, which is called as control Flow.
And in Control Flow, we are going to discuss about conditional statement and loops.
In this video we are going to focus more on conditional statement.
So here is the entire video outline what all things we are going to discuss.
So first of all we are going to see introduction to Conditional statements.
Then we'll go and understand what is if statement how to write if statement with the help of Python.
Then you have if statement then you have else if statement.
Then you have nested conditional statements.
Along with this we will be implementing a lot of practical examples.
We I will be showing you multiple examples of real world use case examples, and then we'll try to solve
that.
Apart from that, the final thing is that we'll also see what are the common errors and what is the
best practices that we should follow whenever we are using conditional statement with respect to if
Elif and else.
Now let's go ahead and let's start our coding.
First of all, let's understand what is if statement right.
And this is really important for you all to understand.
And as I have always said before you start any Jupyter notebook, make sure you select the kernel with
respect to the virtual environment that you have created.
Now, if statement evaluates a condition and executes the block of code within it, if the condition
is true, okay, what does this basically mean?
Let's say that I am writing age is equal to 20, right?
And now if I am writing an if statement after this, if we will be putting any conditions over here,
let's say if I write, if age is greater than or equal to 20.
So this is one of the condition.
Right.
So this if statement is going to evaluate this particular condition.
And if it is true right it is going to go inside this particular block.
Right.
And obviously you know about indentation directly.
You will be seeing that we'll be able to write the code inside the block.
So here I will go ahead and write, print.
Um, let's say that uh, I will go ahead and write age 18.
And if the age is greater than or equal to 18, okay.
And here I will go ahead and write 20.
Okay.
Then I will say that.
Hey, uh, you are allowed to vote in the elections, right?
Something like this.
Vote in the elections.
So here you can probably see the output.
You are allowed to vote in the elections.
What does this basically mean?
When I started my if statement, first of all the condition that I have written it, this will get evaluated.
And obviously here you can see the age is greater than or equal to 80 because I have assigned the age
variable is equal to 20 right now.
Since this is greater than or equal to 18, you will be able to see that this entire condition will
become true.
This entire condition will become true.
So it will go ahead and execute this particular statement.
Right.
And this is a very simple way of understanding if statement.
Let me repeat again, A if statement evaluates a condition.
So this is my condition and it executes the block of code within it if the condition is true.
So right now this condition is true.
So that is why it is basically getting executed.
How do I say this condition is true or not.
If I just go and execute in my new cell and if I see age is greater than or equal to 18, this is true.
So if true, I am going to go inside this particular block.
So this was about if statement.
Don't worry we will be seeing a lot of example.
Now let me go ahead and show you the another uh conditional statement which we basically say is else
right now.
What exactly is else okay.
Uh, to define else, I would like to, uh, probably talk about this.
Okay.
And let me just go ahead and write a comment over here again, the else statement executes a block of
code if the condition in the if statement is false.
What does this mean?
That if this statement is false, it is not going to go inside this block, right?
So is there a way that I can probably move it into another block?
Right.
So for that we will be specifically using else statement.
So let us go ahead and see one more example.
So guys now let's go ahead and see this else statement.
And how does it work okay.
Let me just go ahead and write.
Age is equal to 16 okay.
And now you know the condition if you really want to vote.
Right.
So what is the condition that I have to write.
If age is greater than or equal to 18 right.
Then I can go ahead and print something like you are.
You are eligible for voting.
Okay, so this is my statement that I have written, right.
Now let's let's go ahead and execute this.
Right now.
When I'm executing this, you can see that nothing is getting printed.
The reason is very simple because this statement has become false.
Right.
And so you are actually coming outside the block.
Nothing is getting printed.
But let's say I want to handle for this age in a separate way within this if clause.
Right.
So what I can do, I can again go ahead and write else clause right now inside this else clause I will
probably say print you are a minor okay.
So this will be my else block.
Now what will happen if I execute this?
You'll be seeing that if this is false, what it is going to do by default, it is going to jump into
this particular block.
Right.
Because this is what my else condition is, right?
The default condition at the end.
Right?
If nothing is getting satisfied, just go and jump to the last else block and display this.
So once I'm executing this, you'll be able to see that here you are getting an answer.
You are a minor.
So this became false after this basically became false.
It is going to the else block.
Again.
Let me repeat the else statement executes a block of code if the condition in the if statement is false
okay so here you get probably see this okay.
Now let me just go ahead and go ahead with the next conditional statement which is called as Elif statement.
Okay.
Now with respect to the Elif statement, um, if I really want to give a smaller definition for this.
What?
Okay, what exactly is an if statement?
Now, this Elif statement allows you to check multiple condition.
It stands for else if.
Okay, that is the reason we say Elif.
Elif statement is there.
But if we go ahead and see the full form, it is nothing but Elif, okay?
And it allows you to check for multiple condition.
Okay, now let me just go ahead and probably put some, uh, points over here.
Let's say I'll show you some example.
If I go ahead and write age is equal to 20.
Okay.
Now let me go ahead and write.
If age is less than 13.
Okay.
I'm putting some kind of condition over here.
Any condition you can put, you can go ahead and put an end condition or condition, whatever condition
you want, any conditional statements, you can write it over here.
Um, you know, and obviously I've seen this kind of, uh, statements right where I am actually putting
some kind of conditions over here.
Right?
Uh, anything you can actually put over there.
Now, if I go ahead and write print, if age is less than 13, I will say, hey, you are a child,
okay?
You are a child.
Now I want to check for other condition.
Also lets me go ahead and write Elif.
Now Elif, age is less than 18.
Okay, if the if the age is less than 18, then I will go ahead and say, print.
You are a teenager.
Okay, you are a teenager, right?
And then I will go ahead and write.
Else.
Finally, the else statement.
By default this will get.
If none of this condition is basically becoming true, this will get executed.
Then if I go ahead and write over here print, I'll say, hey, you are an adult, okay?
And now if I go ahead and execute it, what will happen?
Which part will get basically executed, whether this or this or this.
Right.
So here you can probably see if I go ahead and execute, you'll be able to see, hey, you are an adult
right now.
If I go ahead and write 17 as my age.
Right.
So first condition over here you can see age is less than 13, which is obviously false.
It will go to the next one.
Else if it is not going to check multiple condition if else if age is equal to 18.
Now here it is going to be true and this is what it is going to get displayed right.
So here you can see you are a teenager right.
So this actually gives you an idea how this Elif basically works.
Right.
And Elif allows you to check multiple conditions right now.
Uh, there may be scenario where I really want to use nested conditional statement.
And this is what we are going to discuss about okay.
Nested conditional statements.
Conditional statements right now nested conditional statement.
What all things you can basically do okay.
We can use the combination of if else if or else inside the block like.
Let's say inside this if clause I can write another if clause.
Right.
So here let me just go ahead and write this particular comment for you.
You can place one or more if Elif or else statement inside another if Elif or else statement to create
nested conditional statement.
Okay, now let's say, uh, I will just give you some of the examples over here to make you understand,
uh, what uh, like let's say I want to probably I will give you a number number.
You need to tell me whether it is even or odd or negative.
Okay.
Something like this.
These are my three conditions.
Okay.
Let's say I have given my number.
So let me go ahead and write input.
Okay.
So first of all I will define my number I will use int.
And then here I'm going to use input.
And here I'm going to probably say enter the number okay I will go ahead and enter the number.
Now my first condition will be that I will go ahead and check for positive and negative number.
So if I go ahead and write if number is greater than or equal to zero, sorry.
If it is greater than zero, then I can probably.
Okay, let me just go ahead and write.
Then I can probably go ahead and print.
The number is the number is positive right.
So here you'll be able to see that I am printing this okay.
Now the next condition is that I need to check whether this number is even or odd right now.
In order to probably check I may write any numerical conditions over here.
If number, I mean, I will try to use any operators, right.
If number.
Modulus of two.
Modulus of.
Two basically means if the remainder is equal to zero, that basically indicates that.
Obviously this is an even number.
So here I'm going to print the number is even right.
So here you can see the number is even else I'm going to probably print.
The number is odd.
Okay, so here you can see what I have actually done.
If this if this gets solved, that basically means we are going to say that hey, the number is even.
But if this is not true, then it will go to the else block saying that the number is odd.
So what is basically happening inside an if condition?
I have written another if condition along with else block.
So this is what nested nested uh, nested uh conditional statement means right now there is also one
more thing for this if block, also for the outer if block.
Also I can again write an else condition saying that else.
And this time I will go ahead and print the number is the number is negative okay.
Or a better way will be that if I'm entering the number, I will just go ahead and write greater than
zero.
Okay.
And here in the else condition I will go ahead and write is zero or negative.
Okay.
Now see this is what nested conditional statement means.
You can place one or if else or uh if else or else if uh sorry Elif this kind of statements within another
if clause or uh, Lf clause.
Okay.
Similarly you can write another Lf clause over here wherever you specifically want.
But the order will be the same thing right?
You need to probably make sure that you manage all the indentation part.
If you are not managing the indentation part then you may get an error.
Okay, so here you go over here.
And now if I go ahead and execute this you will be able to see it is asking for me for a number.
If I go ahead and write 12, I'll go ahead and press enter.
So here you can see the number is positive and number is also even.
Okay.
Again let me just go ahead and execute this time.
If I give 11 you will be able to see number is positive but the number is odd.
The else part has got executed over here.
Now again if I go ahead and execute it and if I go ahead and write minus one.
So here you can see the number is zero or negative which is basically going to the else block.
Right.
Why this is going to the else block.
Because I have given the number as minus one.
Now this is going to be false and it is just going to jump to the else block.
Right.
So I hope, uh, you are able to understand this in a amazing way.
Uh, you got an idea with respect to if else if else.
And also you got an idea with respect to the nested conditional statement.
Now let us see some very important use cases.
And uh, I'll just say that, uh, some practical examples that we are going to probably discuss.
Okay.
See, the errors can only happen when you are not doing the proper indentation over here.
Right?
If the indentation is basically done in a proper way, then you should be able to write this if else
block in a very efficient way.
Okay.
Now first practical example that I'm actually going to give you Okay.
You need to determine if a year is a leap year.
What does leap year basically mean.
Any year that is divided by four.
If the remainder is zero, that that basically means it is a leap year using nested conditional statement.
Now you can pause the video and you can start writing this right.
And you can see what it will probably give you and what it will not give you.
Okay.
Now let me just go ahead and write this.
So first of all, I will go ahead and write here.
And I will give this as an input okay.
And let me just go ahead and say enter the year okay.
Enter the year okay.
Now next thing if I will start my condition, if your modulus for right is double equal to zero right.
So here see understand this particular condition.
Also if your modular for is equal to zero right then what I will do see here.
Also I can also write like this.
If number percentile uh modulus two is equal to double equal to zero I have used one additional bracket.
So let me just remove this.
If you are getting confused with this okay.
If it is double equal to zero then I will also go ahead and put one more condition.
If here modulo 100 is double equal to zero right.
So here I have used two nested if else clause.
Right.
And if here.
Or modulus 400 double equal to zero okay.
Okay, so see how many nested if else we have actually done.
Okay.
Now if this condition is basically if this all three condition is basically true, which is specifically
for the leap year, what I'm actually going to do, I'm going to go ahead and write print this year,
whatever I've actually given.
Right.
Whatever year I've actually given is a leap year.
Okay is a leap year.
So here is how I'm actually using the print statement.
Okay, I will take one more session with respect to the print formatting.
Okay.
Uh, there are different different ways of print formatting.
So print year is a leap year.
Okay.
Then I will go ahead and write in the else block.
What if this is not true?
What if this two statements are true?
But this is not true right?
Then I will go ahead and print this.
Here is not a leap year, right?
The same year is not a leap year okay.
So here you can probably see this okay.
Then let's say fine for this.
If if it is false I'm handling it over here.
But what about this.
If so for this we will write another else statement okay.
And here I will also give one more condition saying that hey print here.
And if this is also true, if your 100 model is 100 is equal to zero, I will be saying that hey, here
is a leap year.
Okay is a leap year.
I'm just trying to write it in a complex, uh, if else condition.
And finally, if this condition is also true in the else block, I will go ahead and basically go ahead
and write print, print.
Here.
Is not a leap year.
Okay.
Done.
So here you can basically see that if this condition is true then also we know that it is a leap year.
So for that further conditions we are applying this one.
Then again further we are applying this one.
But if this is also not true then we also need to handle in the else block.
Right now let me just go ahead and try it out.
So here I'm just going to execute it.
And let's say I go ahead and write 2022.
Now obviously this will be uh not a leap year, but let's execute this.
And here you can see 2022 is not a leap year, right.
If I go ahead and execute it again.
So let's go ahead and see 2024.
If I go ahead and execute over here, 2024 is a leap year right now.
So this is one of the good, amazing, practical example.
Now what I'm going to do is that I'm going to give you more assignments as we go ahead.
Right.
Uh, we need to also understand what are the common errors and mistakes that we usually do.
If you have not written colon, if the indentation part is not done proper properly, then obviously
this will be a problem right now.
Uh, let me give you some of the assignments, uh, which you should definitely do.
Okay.
One is, uh, please try to create a simple calculator.
A simple calculator calculator.
And inside a simple calculator program, you need to take an input from the user, perform different,
different operations, what kind of operations you need to do.
If it is plus, then by using if else condition, you really need to do it.
So here I will just be.
Please make sure that first of all, before I show you the solution, you try to write the code from
your side.
Okay, so here you can see, uh, I will just go ahead and update this just a second.
Okay.
so here you can probably see I have used float input.
Enter the first number.
Second number operation is either you can upload plus minus star or division.
You can also use modulus if you want.
And then I have written all this condition.
It's operation is equal to plus.
Then result num plus one this this this else you'll be also able to see the nested elsif is also there.
Else if if number is not equal to zero right.
Just go ahead and see this.
Whatever things we have discussed till now, I think you should be able to understand it.
Okay.
So let me just go ahead and execute it.
Let's say 12.
I'll go ahead and press 24.
I'll go ahead and press enter.
And let's say I want to do plus operation.
So here you can see 36.0 okay.
So uh try to do this.
Uh one more example that I really want to give it to you okay.
Determine the ticket price based on the age.
Okay.
So this will be another one example.
So I have multiple examples.
But definitely do try it from your side.
So I'm just putting so that you will be able to practice it okay.
So here you can see enter your age.
Are you a student yes or no.
So if you are probably saying yes then you will be seeing yes over here.
So here are some of the condition.
If age is less than five, price is free, else if is age is less than 12, price is $10.
If age is less than 17, and if the student if it is a student, then price is $12.
Else if $15 then just try to go through this okay.
And then you will be able to understand it.
So I hope you were able to understand this video.
Uh, this was it from my side.
I will see you all in the next video.
Thank you.