1.9 Hands-on Python program | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav walks through three Python programs using conditional statements: checking if a number is odd or even with the modulo operator, then using if-else to print the result, then mapping a numeric score to a letter grade with nested if-elif-else.
Chapters
Ladies and gentlemen, in this video we will see practically how our conditional statements are implemented in any Python program. So we will implement few programs on which we will be using the conditional statements. So let's write a Python program to check whether number is odd. So let's write down the code for the same. So first I want the number. So for that I am going to take an input of the number. So I want a number which is going to be a whole number, so I'm using int for that purpose. Then continuing with the input, because I wanted to take that number from the user, so int input Enter, enter the number. Once I have the number from the user, I have to check whether it is an odd number or even, just an odd number or not. So I will check it with the condition. So if number which is going to be entered by the user gives us any remainder which is like other than 0, then yes, it is an odd number. That is the condition for odd number. So if I say that I'm going to divide 8 divided by 2, so it's going to give me 4. But same way, if I'm going to divide 9 by 2, it's going to give me 1 as a remainder. It means if it is an odd number and I am going to divide it by 2, it will give me some remainder. But in case if it is an even number and I am going to divide it by 2, it won't be giving me any remainder. The remainder will be— in this case will be 0, and in this case it's going to be 1. Let me give you one more example. I'm going to divide 14 divided by 2. It's going to give me 0 as a remainder because there will be a question which is asked. 7. In place of 14, there is a one odd number which is 15. If I'm going to divide it by 2, it will give me 1 again as a remainder. So this is the concept and this is the condition for an odd number. Odd number condition is if that number is getting divided by 2, there should be a remainder, then only it is a odd number. So how to implement it? I'm going to divide it by 2. And I'm only checking the remainder. For checking the remainder, we have an operator called modulo, divided by 2. Then I need one more operator which is of not equal to 0. It means if there is any number other than 0, then yes, it is an odd number. So this is my condition. So I have written if, then this is my condition, then following up with what I wanted to do. If it is an odd number, I want it to print it's odd number. Let's test it. Here I am prompted with enter the number. So suppose I'm entering 50, it prints nothing for us because there was no other code. Suppose I want it to do print It's an even number. It is an even number. So let's run this. Now if I'm going to enter 50, I will get it is an even number because after if condition, means after the condition which we have written, after the conditional statement, we have the next code which is to print normally. Now let's run this code again. Now I'm going to enter any odd number, let's suppose 15. It says it is an odd number and it is an even number. Why we are getting this, it is an even number? Because that this was not under the conditional statement. This is just a print statement which I have asked it to print it under unconditional, like there is no condition for this particular print statement. I want it to be printed every time. If you don't, then you can comment this line. Now if you will run it and write 15, it's— it is an odd number. So this is how our if condition works. Let's write another program. Let's write a Python program to check whether it is an odd number or even number. So this time I have two statements: odd number or even number. So let's write it down. For this purpose, what I am going to do is I am going to take the same code from the above and going to enter it over here. If this condition is true, it is an odd number. You can also change the condition saying that if it remainder is 0, then it is a even number. You can change the condition like this. Else print it's an odd number. If I'm writing 15, it is an odd number. Now I'm going to run it again. Now writing 16, it's an even number. So this is how you can use if conditions and if-else condition. We have one more condition which is an if, which is nested if condition. So we will write Python program, we will write a Python program to check the grade where I have few conditions, where the conditions are If score is greater than 90, then the grade is A. The grade should be A. If the score is greater than 80, then the grade is B. Third condition that I have is if the score is not— like, if the score is greater than 70, let's say, then the grade is— the grade is C. And in case the score is greater than 60, then the grade is D. So in my grading system, there are only 4 grades: A, B, C, D. Apart from that, there is no grade. So in case the student has scored greater than 50 or something below than 60, then below that it is E. I want this to be printed, and then in the end there should be a grade printed. So in the end we will be printing, printing grades. So let's write a program for this. This sounds tricky, but it If you are going to use nested if statement, it won't be. So let us continue with the statement. So if— first we need a score, so we will take an input of score. Score is equal to int input, enter your score. I will print grade for you. You enter your score, I will print grade for you. Next, I have to check whether the score is as per the mark or not. So I have a variable score, so I'm going to use the same variable. Greater than equal to 90, grade is equal to A. Elif score greater than equal to 80, grade is equal to B. Lf score is greater than equal to 70, grade is C. Elif score is greater than equal to 60, grade is D. Else, if it is below 60, it should be grade is equal to E. In the end, I have to print those grades which I have checked in the conditional statement, nested if statement. So I will print 'Your grade is,' comma, grade. So let's run this. And you enter your score, I will print grade for you. Let's suppose my grade is 50, printing your grade is E. My score is, let's suppose, 80, the grade is B. Now my score is 92, your grade is A. My score is 85, your score is B, which should be greater than 80. It is B. Yeah, correct. So this is how you can use nested if for this purpose. Not even this, if you wanted to add more codes under this if and elif statements, yes, you can do that. So after this if condition, when we have the indentation, you can write down the code. Make sure you are adding up the indentation. As many lines of code you wanted to add, you can. You just need indentation for this purpose. Once this concept is clear, then only watch the next video, else just rewatch this video.










