1.8 Conditional statement in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav teaches Python's conditional statements: if, if-else, and elif for multiple conditions. She covers required syntax like colons and four-space indentation, how Python evaluates conditions as true or false, and common mistakes like incorrect indentation that break the code.
Chapters
Ladies and gentlemen, this video we will discuss about some conditional statements which we use in Python. In other programming languages also, there are certain conditional statements. Conditional statements are basically used for making decisions. So for helping you make decisions in Python, there are 3 conditional statements which you can use. Number 1 is the if statement. Number 2nd is if-else statement. Number 3rd is the nested if statement. If statement, if-else statement, and nested if statements. You can use as per your need. So let's discuss the first one, which is if statement. Now, what is if statement and how do we write it? So how to write it is you have to write— I will write it in the code cell. It's— you have to write the keyword if, which is a keyword in Python. Then you can define the condition if you have any. You have to write down the condition following up with the colon. Once you have written the colon, move on to the next line. Once you will press Enter after colon, there will be a 3 tab space over here. It's usually the 4, and like, it depends on the environment that you are using. So there are 4 tab space over here. Which is automatically generated. So this 4-tab space is also known as indentation, and which is must in writing these conditional statements. So you have to make sure whether— whenever you are writing this if conditions and moving on to the next line, there should be a 4-tab space. You can use 2 or 3, but yeah, by default, if you are going to press Enter The spaces will be 4 tab space. Once you have the 4 tab space, you have to continue with the statement that you wanted to print. Let me tell you how it works. So basically over here, when you have written if— this is a keyword— so here in the syntax I told you that in the starting it should be if. If is a keyword over here. Keywords are those words which are reserved in Python. These are the reserved words in Python. Then the next is the condition. There is a condition. Suppose this value is greater than 50. When we are trying to evaluate these things, like this is an equation, if this value is actually greater than 50, then it will return true to us. If it is not, it will return false to us. So in case it is true, it will continue with the statement. It means this condition is true, following up onto the next line, whatever you wanted to do, it will do that for you. In case the condition goes false In case it is false, it will stop doing anything and it will just continue with the rest of the code that you have written outside of the indentation. It means with where there is no indentation. This indentation is to tell our Python that, okay, this statement is under this condition. Only if the condition is true, do this, else continue with the rest of the code. If-else statement is the keyword if, then the condition, then the statement. It is same like the if condition. If condition is basically used— if statement is used When you have one condition and one statement, how do you write if-else? Is if condition, then the statement, then press Enter, remove the indentation space, else following up with the colon, then the statement. Now let me tell you how it works. So here we have if, again this is the keyword, then we have a condition which is going to be your defined condition. This value greater than this, or this and this, whatever condition you wanted to write, you can write it. If this condition is true and if this condition is false, what to do? In case it is true it will continue with the statement, this one, if this is true. And if statement also, we were doing the same thing, but there was nothing if the condition goes false. But in this case of if-else, we have if the condition is false, it will continue with the else statement, else statement. So this is how your if-else statement works. If-else can be used When you have two statements with one condition, nested if. How do we write it? Is same way we will write the if statements. If condition Following up with a colon, then the statement. Then again you will write if. In this case you can write else-if, following up with the condition again, then the statement. Follow up with another condition that you have, then the condition. Following up with the statement, then else, and then if you have any statement, you can write it. So I have written it as a draft, like a syntax only. So over here we have if condition statement, if-else condition statement, elif condition statement, condition statement else. So this else if, else if can be written as el of else if. We call it in short elif. So this is the word which you have to write in case of else if. How it works is basically, it works like if condition is true, it will continue with this. If this condition goes false, then it will continue with the next condition. It will check this condition. If this condition is again false, it will continue with the next condition. It will do that. In case the condition is true at any point of time in this conditional statement, it will stop. It will just print that statement, whatever you have written, you have asked it to do, else it will stop. It won't be doing the same thing for the rest of the conditions because it finds it one stop solution, which is the condition which was true. So this is how you can use elif or nested if condition. So if you can see over here, we are getting some red color line which shows that it is incorrect. It means you have written something incorrect. So if I will correct it with the elif, it will go away. So this is the correct syntax of writing the nested if statements in Python. This can be used— nested if can be used when there are multiple conditions to deal with. When you have multiple conditions to deal with, you can use nested if. When you have one condition with two statements, it means you want— if something is true, do this, something is false, do that. Then in that case, you can use if-else. If you wanted to just check whether this condition is true or false with— and do something with the statement or the code that you wanted to write, then you can use normal IF statements.










