Hello guys, welcome to LearnVern. In the last topic, we saw Operators in which we saw multiple operators under Python and how to use them.
Today we will see Conditional statement.
What is Conditional statement?
Conditional statements help in decision making. Meaning if you have to make a decision in coding language, you can take it only & only through Conditional statements.
Suppose you want to check whether your variable’s value matches with your condition or not.
For that, you will have to use Conditional statement.
Conditional statement is one type of Boolean. Meaning it returns either true or false.
We are going to see multiple types of Conditional statements.
First of all, we will see Simple If.
Simple If is a normal If condition where you enter only 1 condition and its statement and the condition ends.
It will get out of condition.
Next option is If Else. What does If Else do? There’s this condition here which has 2 parts.
If it’s true, it will follow the true statement or if it is false, it’ll follow the false statement.
This is how the If Else condition works.
Now if we talk about the If Else Ladder, you can write multiple conditions together.
An else is provided at last where if any condition is not true, it will become true in your last condition. Okay?
Nested If works in the same way. What does nested if mean?
If there is one condition and you put another condition in it, and both the conditions have Else parts.
Nested If works in this way.
Now I’ll explain it to you practically to give you a clear idea about how Conditional statements work.
I’ll open a Jupyter notebook now. It has opened up.
I’ll check Simple If first. I’ll write down Simple If first.
What was the syntax under Simple If? Let’s check it first.
The syntax is only 1 condition & its statement.
Let’s see how to implement it.
Suppose I have a variable named Var here and I assigned it a value of 10.
Now I want to check If condition that If my variable is == 10 or not.
Let's check it.
Here, I chose == sign and the variable is == 10 or not.
Now I’ll print Value of the variable is 10.
Otherwise we will write a simple message of Bye outside.
I clicked on run and it showed me that the value is true.
So our condition is true.
Your condition is true.
As it is true, our condition is going inside and printing our message.
In simple If, there is only this much that you have to put only 1 condition & work along with it.
I’ll give you one more example.
For instance, your variable...suppose a = 25, assigned the value to the variable.
I wrote an If condition, If a >= 30. I wrote a condition here that A’s value is greater than or equal to 30.
I want to get the result False to let you know what happens in this Simple If statement when the result is False.
I’ll write down “Value of the variable is 25”.
If the condition matches and it is true, the value of the variable should print 25.
Or else print only Bye.
Now I'll run it. It resulted in Bye. Why?
Because our condition is getting false.
So it came outside.
So this is how you can work with a Simple If in Conditional statement.
Moving onward to the next Conditional statement which is If else.
How does If Else work?
Now you have True as well as False.
If it is true then the true statement will get called, if it is false then false statement will get called.
Let’s see its Syntax. There is a condition, its statement. Else and its statement.
But remember one thing: Indentation is compulsory.
Let’s see how.
I have a variable A...I’ll make this program Dynamic.
Dynamic meaning we will take User Input in this program.
We have to take User input & solely Integer input.
Input Int. I will ask the value of A from the user.
Let’s write a simple message: Enter the value of A.
I’ll take another variable here B.
I have 2 inputs here: A & B.
Now I have to check the condition. Check what condition?
If the value of A is greater than B, then the condition which is false, that should be the statement call.
If B is greater than A, then the condition which has come true, that statement should be called.
Let’s make one condition.
We applied the condition with a round bracket.
Python gives the permission to apply the condition even without the round bracket.
How? If A < B. Print B is greater.
Otherwise Else. Do you remember Indentation?
Indentation means our Else should be exactly below our If.
I wrote Else here and wrote Print. A is greater. Time to run it.
Let’s enter a value. A 10 and B 20.
Run it & the result is B is greater.
So which condition became true? This one.
Condition is getting True. clear?
Now if we run another time after changing A to a greater value than B 10.
A is greater. Here, Else part got called. Else part call.
So this is how the If Else works.
I hope you are understanding so far.
We did this with User input.
Suppose we have entered a static value.
We will take a variable value 25 and Y as 30.
Now I want to check if x < y.
Now we will print Y is greater.
Else. It is very simple like we’ve already seen above.
The only difference is that our value will be static.
And we will write here X is greater.
Now we’ll run it.
It won’t ask us for the input during run time, it will directly print the answer.
Which is Y is greater. Which eventually means our condition is True.
So in this way Simple If condition & If Else condition work.
Now moving ahead to If Else Ladder.
See what? If Else Ladder. We will make a condition first.
What condition? That our percentage is greater than 70 then == distinction.
This is our 1st point.
What is the 2nd point? If the percentage greater than or equal to 65 and less than 70 == then 1st Class message should be printed.
I’ll copy this to save time. 2nd class. What condition will we apply here? That percentage is greater than or equal to 60 and less than 65. Greater than or equal to 60 and less than 65. Then we want to print 2nd class.
Same for 3rd class. Percentage should be greater than or equal to 55 and less than 60.
3rd class should be printed.
5th should be if your percentage is less than 55 then the print should be Fail.
So we made these questions into comments.
We made them into multiline comments.
First we will take an input of percentage. I’ll take a variable p for percentage.
Int input. I’ll write Enter your percentage.
What did I ask from the user here? I asked for the percentage.
And stored it into P.
So what does our 1st condition say?
Our 1st condition says if our percentage is greater than 70 then distinction will be printed.
Our 1st condition is a percentage greater than 70.
Or we will put greater than or equal to 70.
Then what do you want to print? Distinction.
We have the 2nd condition as well.
We will write elif. Why? Because we have to work with multiple conditions.
See the syntax of If Else Ladder.
If..our main condition..
After that, elif condition. 2nd condition.
If there is another condition then again elif.
Our ladders are created in this way.
What will be our 2nd condition here? P percentage > = 65 and…
You can see there’s an and here. If you remember, we learned about And in Operators.
We used that operator here.
And another condition we gave here is < 70.
Now we will print 1st class here.
Why 1st class? Because we printed our message according to the condition given above.
Now what does the 3rd condition say?
It says percentage should be greater than or equal to 60 and percentage should be less than 65.
What message should be seen? 2nd class.
Same like that...elif P. 4th condition.
What is that? It is P that is percentage should be > or = 55.
And percentage less than 60.
Then our message should be 3rd class.
Otherwise Else. Meaning if your percentage are less than 55, then it is Fail.
What do you have to do? Fail.
Now do you have to apply any condition to that Fail? No.
Why not? Because we don’t have any condition left.
So I’ll write Else. And I’ll print Fail.
So all of our conditions have been fulfilled here.
Now we will run it.
Suppose we entered the percentage here, 70, it printed Distinction.
Now I’ll write 45 and it is showing Fail.
In this way, you can merge or arrange the conditions on the ladders and use If Else Ladder in Python.
Moving onto the last condition that is Nested If condition.
Now what does Nested If say? We will see its syntax first.
Nested If says that you have 1 condition. Under that, there is another condition.
And there is an Else after every 2 conditions.
That is called a Nested If condition.
Let’s go with the practical.
I have another question here.
Let’s create a Blood Donation Application with Nested If.
For Blood Donation, the 1st criteria is that the age should be > or = 18.
2nd is your weight should be > or = 50. Weight should be > or = 50.
3rd criteria is if both age & weight match with criteria then the user can donate blood.
What will be the 4th criteria? If age is < 18 then show one message under age.
And the last is if weight is < 50 show one message underweight.
We will add one more criteria here that if age does not match then it should not ask for weight.
So these are the 6 points and this is one type of question.
Now what will we do here..we will give a condition here to set our criterias.
First of all we will take the age input from the users.
Enter your age. I’ll take the same input for weight here.
Enter your weight. And weg for weight.
Now what does our first & foremost condition say?
That our age should be > or = 18.
We have to give the 1st condition to be If age > or = 18.
What is the 2nd condition? If weight > or = 50.
So what are we doing here?
We are taking this point & this point and working parallely through them.
Why? Because if our age is matching then only the weight should be asked, otherwise not.
If both the conditions are matching, then we have to write a message saying Blood Donate.
As you know, both the conditions have an Else part.
What do we have to write in Else?
If your weight is not matching the criteria, we have to write Print underweight.
Same for this, Print underage.
Now all the criterias are matching.
Let’s run it. What if there’s some error right?
Let’s check it.
I’ll write 14 here. 14 does not match our criteria so it should not ask me for weight.
What should it not ask me for? Weight.
Let’s run it. It is asking us for weight.
That means our program is a little bit wrong.
Let’s think about what went wrong.
What was wrong? It now showed us underage but it did ask us about weight.
Which means our program that is our code is wrong...but why did it get wrong?
Because Python works with Interpreters and we took weight’s input already.
When did we need it? We needed it only and only after our age matched the criteria.
We will cut it.
And put it here. And now run it.
Now when I write 14, it will directly show me Underage.
Let’s run it whole now.
I’ll enter the age 19.
I entered weight 65. It gave me the permission to donate blood.
So this is how Nested If works in a Conditional statement.
I hope all of you understood the concept clearly.
So what did we see today? We saw how Simple If works, If Else works, how If Else Ladder works and how Nested If works.
In the next video we will learn about Looping statement.
Decision-making statements are another name for conditional statements. When we wish to run a block of code based on whether a condition is true or false, we use those statements. Type of condition statement in Python: If statement. If Else statement.
There are 4 basic types of conditionals: zero, first, second, and third.
Conditional statements are used to control the execution flow based on certain conditions. If a condition is true, you can do one action; if it is false, you can take a different action.
A conditional statement is one that looks like this: "If P, then Q," with P and Q being sentences. P stands for hypothesis, and Q stands for conclusion in this conditional statement. "If P, then Q," on the surface, implies that Q must be true whenever P is true.
Best of all beginners
Good learning app
SN gateway creation
when write code show this error - OperationalError: (1115, "Unknown character set: 'utf8mb4'")
how to solve it
if Else ladder,
input data from user this not taking all numbers values like 45.5, 75.05
kindly give the idea ...
This website is amazing, and helping us to beginners to improve yourself skills. I appreciate this website, and website owner work is excellent. Thanks
Respected LearnVern Team Course content not working in the Python course ( video not playing)
other course and overall platform is very good Thank you.
Shubham Ramesh Sonwane
It was a very fantastic course. But I have to suggest something To your team, when I complete the assignment how can I check if my My solution is correct or not. thank you