Day-5 Python + MYQL FREE Sessions

Learn Python conditionals: if, else, elif, and logical operators. The instructor walks through syntax, indentation, and nested conditions with live code examples, then has you build a grading system that converts numeric scores to letter grades.
Chapters
Hello everyone, how are you all? Yeah, am I audible to you?
Hello.
Yeah, am I audible to you all? Do you guys have any question related to the topics that we have covered so far in the sessions? Why no one is speaking? All right, so let's begin with the next topic, that is conditional statement. Now conditional statements are used when you wanted to work with a multiple statement or you want something to happen whenever the condition goes true. So what conditions are— as we have already covered the operators, that there are multiple operators in Python: arithmetical operator, logical operator, identity, membership. Comparison. So you have— whenever you wanted to write down the condition, you have to use it. So if the condition says that a is equal to b, so this is the condition. If I'm saying a is greater than equal to b, then this is again a condition. So whenever you wanted to deal with these kinds of condition, then you have to use conditional statements. Now there are different types of conditional statements. So the number one type of conditional statement is if statement.
If statement.
Am I audible to you now, Sushant?
Yes, okay, all right.
Yeah, so first type of conditional statement is if statement. So for if statement, the syntax is you have to write the if keyword. If is a reserved keyword in Python, so you cannot use if as a variable anywhere. So if is a keyword, so you have to write if, then you have to use the brackets, then you have to add a colon in it, and then you have to write down whatever you wanted to do. Suppose I wanted it to print something. So I can simply say if bracket open, and then in the next line print. Now what is this bracket for? The bracket is for the condition. So here you have to write down the condition. So suppose your condition is a == b, and you wanted to say that the value of a and b are same. So within this print statement you can write that the value of a and b are same. So simply we have written a == b, which is our condition. So this can be anything. So if I'm writing, let's say, a 20 and b a. So simply I have written like a is— I have directly assigned the value 20, but for variable b, I am reassigning the value which is stored in variable a in B, and then I am writing a condition that if A is equal to equal to B, then print this. So this is if statement. Practically you can say that suppose if I'm writing age is equal to— I'm going to take the input from the user and I want enter your age, okay? And then obviously the output will be in string, so I have to convert it into integer. As we have discussed earlier also, this concept that you have to convert your string data type into integer data type or float data type according to your need so that you can perform something on it. So I've written age is equal to int input, enter your age. Then I wanted to check if age is greater than or equal to 18, then I can print that 'You can vote.' 'You are eligible to vote.' 'You are eligible to vote.' So if this is the case, that means I have written a condition that if age is greater than equal to 18, then only that person can vote. So I have written a print statement notifying the user. I have to run this, and then above I have to enter the age as it says. So if I'm writing 20, then 20 will be stored in the variable age, and we are checking from the condition, and the condition goes true. That is why we are getting this print statement. Now let's talk about when the condition is not true. So if I'm writing, let's say, 17, so it won't print anything because this is the simplest conditional statement, which is if statement. When you are going with the if statement, with the if condition, you are writing one single condition and you are writing one single print statement. Then like there is no case that you have defined for when the condition will go false. Any question up till this? Yeah, anyone, any question? In this type of conditional statements, in loops, in functions, there is one more concept which is called indentation. Indentation. Indentation is in every programming language. So in Python, what like I'm going to show you is basically when I have written if, then I have written the condition, then I have added a colon. And whenever after colon you will enter, you will get these space in this beginning. You can see my cursor is not in the beginning, right? It has certain space and then this. So basically, whenever this space is coming up, it means this is indentation. If I'm going to remove this space, let's say I have just removed those space, and if I will run, you will get indentation error. It says expected an indented block after if statement on line number 2. So here it is expecting me to add 4 tab space. 1, 2, 3, 4. At least 4 tab space should be there to maintain a good indentation in if conditions. If conditions requires indentation. Up till now we were defining the variables, we were writing, printing it directly, right? But we were not using it within if. You have to write it because for this particular condition, this print statement is— if I am going to copy this print statement and I will just enter, I will remove the space and then I will write it. And now if I will run this, no matter what condition I'm going to write, let's say I'm writing 16, then also I will get that particular statement, like because I have copied the same line and I'm getting the second print statement printed for me. So let's say I'm writing 2.this And now I'm going to again say 16. You can see you are getting the second print statement printed but not the first one. Why you are not getting the first one? Because as per the condition, as per the condition, the condition is true— sorry, condition is false. That is why you are not getting this print statement. But the second print statement is outside of the if statement. This is not the part of the if statement. That is why you are getting it printed. If you want the second statement to be a part of if statement, then you have to add a 4 tab space and then only it will work. Like this particular print statement will be printed only if this condition will go true. So if I will run, I will write 20, then both of the print statement will be printed because now the second print statement is also the part of the if statement. Is this concept of indentation clear to everyone? Indentation is important. Whenever you are going to remove it, you will encounter indentation error. So indentation is nothing, just a white space at the beginning of the line, and it defines the scope that how long is your if statement how many print statements you want within if. Like, whenever the condition is true, how many print statements you want it to. Now let's talk about— for now I was just having one single condition, and whenever the condition is true, I want something to happen. But now there is a different scenario. Let's say I want two things to happen. First is, when I— when the condition is true, do this, and when the condition is false, do that. It means now I want two things to happen. With the single condition, single condition, but two things to happen depending on true and false of the condition. So for this you have to use if-else statement. So second one is if-else statement. So if-else statement syntax is you will write if, then you will mention the condition, then you will add a colon, you will enter, you will write, add a 4 tab space, then you will write whatever you want to print or any calculation anything it could be. And then you will come out of it, then you will write else. Again you will add a colon, but this time you need not to mention any condition because else is just for when the condition will go false, then only the else part will be executed. Okay, so simple. You can write it like this. So let's re-understand this. We have written if condition. Along with if, you have to write down the condition. Your condition can be anything depending on the scenario or the piece of code that you are trying to write. Then you have to add a colon, which is must. If you will add a colon and if you will press Enter after colon, you will automatically get these 4 tab space. If you are not getting it, you have to manually add it, and then you have to write down any print statement or any further calculation that you wanted to make within it, and If this condition is false, in that case the else part will be executed. For else part, you need not to mention any condition. You simply have to say else colon print. Now I will do the same above code, this one, with if-else. Okay, so simply I'm writing this over here. I'm going to write else and I'm going to say you are 'Not eligible to vote.' Now with a single else, I am able to use true and false, both of the conditions. So if I'm writing 17, it says you are not eligible to vote. If I'm going to run this and I will say 20, then it will say you are eligible to vote, because this is according to this condition that we have written, which is age is greater than or equal to 18, then this is how it should behave. Any question in this? Yeah, any question? Anyone?
Yeah.
No one, no questions? All right, this is when you have the single condition, but when you wanted to utilize both, uh, true part and the false part, then you have to use it. Now let's say you have two conditions. You don't have one condition, but you now have two conditions, and according to the conditions you want something to happen. So at that time, you have to utilize the next conditional statement, which is elif.
Elif statement.
Okay, now what is elif statement? Elif statement is used when you have more than one condition. It can be 2, 3, 4, 5, 6, 7, 8, 9, 10, n number of conditions you can write using elif. Elif keyword catches anything which is not caught by preceding conditions. So you have to— you can use, uh, sorry, this is for else basically. So with else you can also write else, uh, sorry, with elif you can write else, sorry. So with elif you can write else, and that else will carry like that, and else will catch anything which is not covered in elif. Okay, so with else elif, you can also write else. So the syntax goes like, you have to write if, obviously, because these are the conditional statements, so it will begin with if only. So first condition goes with if, you have to write down the condition, then you have to add a colon, then you have to add 4 tab space, and then let's say I wanted to print something, enter. I have a second condition, so I will write elif, then I will again open— like, you can write it without opening a bracket, I'm just mentioning the bracket. You write down the condition, you come up in the next line, add a tab space, write down the print statement, come in the next line, elif again. Let's say I have another condition, which is my third condition, so I have to continue writing elif, and then I can write the print statement adding indentation in the beginning. And suppose I was just having 3 conditions, so now I am done. I just wanted to write that if none of the conditions are true out of these 3 conditions. If none of the conditions are true, then go with the else part and then print something. Got it?
Yes, ma'am.
So this is how elif statement works. Why we say elif? Elif is a short form of else if. Earlier, in the previous version of Python, it was else if, but in short, now you have to write it elif. So now let's see how to implement it with an example. Okay, so let's say I'm just going to use two different variables. So let's say x is equal to 100, y is equal to 50. Now let's say if x is greater than 50, print x is greater than 50.
Now let's say 40.
Else y is greater than 50, print same thing.
I'm going to say is greater than 50.
You have more conditions.
Let's say I'm writing x == y or y == 40.
So this time I'm writing two conditions, and I want that if any of these two conditions are true Then you have to print y is equal to 40. Else, I wanted to say None of the condition is true. Now I will just run it. Now you can see it says that 100 is greater than 40. Now first condition is true. So how it goes basically, so if you are writing it line by line, right, this is first condition, then this is second condition, this is third condition, and then comes the else part. Now when it was the first condition, which is x is greater than 50, so x is 100, which is greater than 50, which is correct. That is why we are getting this first statement printed. Now your Python is not going to check for other conditions because out of these conditions, only if one condition is true, it will print that and the work is done. So it took that— it, it has like taken that condition, it checks that condition. Okay, that is true, then the print statement will behave. Otherwise it will move to the next condition. Now which is y is greater than 50? So y is not greater than 50, so obviously this condition is going to be false. Now when it will be third condition, so x == y, which is not because 140 are not equal, so this condition is wrong. But we are using OR logical operator, so your Python kernel will check for the second condition. When it will come to the second condition, y == 40, so yes, it is correct that y is 40. So it means out of these two conditions, this y == 40 was correct, so this print statement will be printed. Now I'm just going to make a little bit modification so that first condition won't go correct, and then it will go to the next one, which is y is equal to 40. Now you can see first condition was wrong, second condition was wrong, third condition you were having this y is equal to 40 correct. That is why we are getting this print statement printed for us. If I'm going to make it not equal to instead of equal to, so I can simply Write it like this, and now you can see you are getting the last print statement printed for you. So you have to write it in the correct sequence, or if you have multiple conditions, try to utilize the logical operator like AND and OR, which will help you execute it in the single time, and then only your print statement or the exact execution will happen. Any question in this?
No, ma'am. No, ma'am.
Simple. When you will utilize AND and OR, right, these two logical operators, then you can combine multiple conditional statements or multiple arithmetical oper— arithmetical statement together, and that will basically help you to write down it in the correct format. Okay, so these were the basic types of conditional statement. First was if, then we have if-else, then we have elif, and then we are combining elif with else, right? Now there comes nested if. When you have multiple conditions, like within one condition you have another condition and then you have another condition, so you are going to create a nest of it. So that is what we call nested if So the syntax of nested if is you will write if—
so this is nested if—
so you will write if, then comes the condition, then adding tab space, then you will write, suppose, let's say, print, and you have the next condition within, which is Like, let's say you want that if this condition is true, then print this, and then check whether this condition is true again or not. Then you write print again, and you have another condition to check, so you can write it like this. This is what we call as a— suppose for this if you are not having any print, now you have to close everything. Like, if, if you are using the single if, then it's okay. Otherwise, if you have something for else, you can write it like this. So this else is for this one, else is for this, if this one, okay, the third if which we have written. Now you have to come back. Now you have to follow the pattern of indentation. So you have, you have added 4 indentation space, and if you're writing something, uh, like by clearing those 4 indentation space, then that goes to the last one. Now for this, if you want to write, you have to go to this cursor, and then you will write else print this. Now for the first if, again you have to follow the same indentation pattern and then you have to write it like this. Any question up till this? So simply, a pattern is that you have to write when you wanted to check multiple conditions within one condition, there is another condition, then you have to go with the Nested if. Okay, now let's say there are some cases maybe you don't want it to— like, I just wanted to check it, okay, that how exactly I'm just practicing. Then that time you can use a pass statement. There is a pass keyword that you have to just mention if statements cannot be empty. Suppose if I'm writing if a is greater than 20 and I am— I don't know what to write next. I just wanted to check whether this single line of code is correct or not. If I'm going to run this, it will say syntax error, that you have to try— like, you have to write completely, write it. If you're not going to complete it, it will give you syntax error. But if you will just write one word, which is pass, then you can easily run this. If there is no error in line number 1, then it will give you like no output will be there, but you will be able to run it. Okay, so this is how you can use the pass statement to just check it. Wherever you wanted to write pass, you can write it. And pass is just a simple statement. If your if statement cannot be empty, if you have some reason like if, if I have with no content you want, then you can go with the pass statement. To avoid any error. Got it?
Yes, ma'am.
Now let's see how to write this nested if practically.
Okay, so I'm going to just copy the same thing and I will write it in the new code cell.
Okay, now let's N is equal to 45. Now let's say first condition, let's say a— sorry, N is greater than 10.
I wanted to say it's greater than 10.
Then comes the next, next condition. So n is greater than, let's say, 20.
Like, it is greater than 10, but now I wanted to say it's greater than 20.
Then let's say the next condition is I wanted to check whether that n is greater than 50. It is greater than 50, it is less than 50, and here I'm going to write it is less than 20.
And for this else, I'm going to write it is less than 10.
Now you can see it says that it is greater than 10, it is greater than 20, but it is less than 50. So it means wherever the conditions were true— first condition was true, that is why you are getting this first print statement. Second condition was true, that is why you are getting the second print statement. But third condition was not true, that is why you are not getting this one print statement. It passed to the else part that, okay, if this condition is not true, go with this one. It says it is less than 50. These two print statements have been neglected because of these print statement and the conditions goes true.
Got it?
Yes, ma'am. Yes, ma'am.
Anyone any questions in these statements or anything?
No, ma'am.
Okay.
Now I am going to provide you one problem statement, okay? And you have to try to do it using any of these conditional statements right now. I hope everyone have joined using the laptop only. Yes. Yeah, no one is saying anything. Have you guys joined using your laptop?
Uh, no, ma'am, it's on mobile.
Okay, what about others?
You can still send. Try to solve it on, uh, mobile also if possible.
Others also have not joined using the laptop. Okay, then let's solve it together on my screen itself. Okay, so you have— everyone have to give some input from their end.
Okay, so let me just write down the question here itself.
Or those who have laptop, those who have joined using laptop can solve it on their laptop itself. Okay, so I'm writing the program in the chat and on my screen as well. So this is a simple problem statement that you need to solve using conditional statements. Yeah, what should be the first line of code? Just first, let's discuss the step-by-step method to solve this problem statement. So basically, here you need to write down a program that takes the student marks as an input from 0 to 100 and prints their grade based on the following criteria. If marks is greater than equal to 90, then you have to print a grade A. If it is in between 80 to 90, then grade B. 70 to 80, C. 60 to 70, D. And if it is less than 60, then it is pay. So how do we need to solve this, or what are the steps that we need to follow?
Uh, we will use if-else statement.
Okay, like what's the first statement we see? First, what is the first step that we need to follow over here?
We have to first input, we have to take user's input. So we will say like student is equal to—
write it in chat.
Okay, yeah, it will be good.
And what about others?
Yes, it's my first class, so I'm joining for the first time, so I'm not very aware of what is going on here now.
Oh, okay, okay, no worries.
What's all the recorded sessions and then I can contribute.
All right, no worries.
Although I do have a doubt in this nested if question, uh, like whatever you were teaching, it was just one print that we were getting, like considering all the if and elif, but in this particular thing we are getting twice or thrice, whatever the criteria is.
Like it is greater than 10, 20, and so on, so on and so forth.
So what's the— like, how many times? Okay, is that something that I'm missing on, on earlier classes, or it's all—
no, no, basically with the if or elif, right? With if or elif, the pattern goes like your Python kernel will behave like, okay, you wanted to store 100 in variable x. 14 variable y. Now this is just a variable, correct? You have just assigned some values in the variable. Now next, the third line of the code is that if this condition is true, do this. Now this behaves like a block of code, okay? This is like a block. So if you are writing it in this manner with the elif— elif means these are multiple conditions and one by one you have to check the conditions, and once you will find a condition which is true, then print whatever is just after that and then stop. Okay, so this is just like a complete block. If it is— it's starting from the first condition and it found that, okay, first condition is true, then first print statement will be printed and then it will stop. No other testing will be done or nothing will be executed further. Okay, these executions will not happen.
Okay, it's only in the case of nested if that the condition goes on.
Yeah, why it goes on? Because we are using indentations over there multiple times. So first we have written if this is the condition, print this, correct? After print, within this particular if condition only, within this single condition, we have written that, okay, print this first. First you have to print this, second you have to check this for the condition. That once this condition is true, print this, then go with the second condition. You have to check the second condition. If the second condition is true, go with this print statement. And again, within this particular second print— second condition, I have written again if. So it means basically this if is the part of the first one. If this condition is true, this will be printed, then the second condition will be tested. If the second condition is true, then this print statement will be printed, and the third condition will be tested. So basically we have created a loop of the conditions. First, second, third— all three conditions will have to be tested if the first condition is true. If let's say the first condition is not true, n is not greater than 10, let's say, so at that time then this print statement makes no sense out of these, like, after this current code cell, this piece of code will not be executed, it will be passed directly to the else and will— it will print like it is less than 10. Okay, okay. So it goes indentation by indentation. So it starts from the first condition. If this is true, then only these condi— these conditions will be evaluated and these print statements will be printed. Otherwise, everything out of these will be neglected. And if is false, so it will be passed directly to else and print statement will be printed.
And what else to the other conditions? There's only else to the, uh, the main thing that we have written, like if n is greater than 10, and there's no else to these, the other two.
No, it is not.
This, for this if, we have written this else. Like if we don't want to add, now that's why, like, you can ignore it, you can ignore it.
Like there's no particular compulsion.
No, for else there is no particular compulsion because else is the extra additional part, you can say, if you want to get something in the output, right? So some coders are like output-oriented.
If there is no output, they feels like, okay, nothing is happening in the backend, right?
So they add else that, okay, now we will get some output. If none of the conditions are true, then this particular statement will obviously will be printed.
Like for the question that you mentioned at this time, so we can like for the paragraph that we'll write if condition, if this, if that, if this, so we can add just one else, no? Okay, like if marks are less than 60, then we can like considering all the conditions above, we can directly come to the conclusion that it's a fail. Like for everything that we have entered, it's overall it's a fail, right? We can do that, no?
Yeah, you can.
Okay, okay.
Simply, it depends upon you and your part of logic. Okay, so some people can write it with the multiple if conditions, right? Some can write it within a single line of code as well. Okay, so it totally depends.
Okay, ma'am.
Okay, I think someone has pasted the solution. Okay, you have written if marks is greater than— first you have to take the input, right, Sanket? Okay, you're taking it above, but you are taking it in the different variable and you are testing it with the different variable.
How?
Sorry, ma'am. Actually, it was on mobile, no? So I was just applying the computation. It was the thing which syntax, like, this will be the proper answer, you can see.
Correct, correct.
There is no point of writing.
Yeah, I have copied multiple answers.
Okay, so now this is how you have to write it. Marks is equal to— you are taking input of marks. Obviously in the question if it is mentioned that you have to give it a range, that you have to enter the marks from 0 to 100, then you have to write it in this manner. Then you have written—
yeah, as you are using in the conditions, you have to make mark M capital.
Then you have written print P should be in the lowercase, elif E should be in the lowercase. Again, print p, and indentation should be there.
Okay, so you have to correct these things that you have to use the variables.
If you are using the logical operators, then also you write down the variable again.
Simple else, and then you have to correct this indentation and the print.
Okay, now let's test the logic. So you have written if marks is greater than equal to 90, which is correct. Okay, marks is greater than equal to 80 and marks is less than 90, then it should be Grade B, C, D, M. Now let's say I'm writing 77, so it's saying Grade C, which is between 70 to 80.
Correct.
Yes, Keshav, your answer is correct. Only you need to correct the P of print statement because every P in print statement should be in the lowercase. And in the last elif, you can keep it as else as well. If you are keeping it elif, that too is fine, but for conditional checking, go with the else.
All right, okay.
So do anyone have any questions related to the conditional statement?
So far, okay.
So I'm dropping some questions related to the conditional statement right away in the Discord channel, so you have to try to solve all of them, okay?
Yes, ma'am.
All right, so we'll connect in the next session at the same time, so make sure you guys join with the laptop now onwards.
Okay, okay.
Oh, ma'am, actually I do have—
yes, you simply have to download the Discord one, right? Because application, right? Once you will download the Discord application, right, simply click on the link which I have shared of Discord. Then it will directly redirect you to the Discord application. Okay, okay, okay, man. Okay, yeah, I think you were asking something.
Everything else is covered in the previous lectures. No, I'll go through them and then I'll get back to you tomorrow if anything I need to ask. Yeah, sure.
Okay, yeah.
All right, guys, bye-bye, take care.
Bye-bye.










